【发布时间】:2021-12-27 12:26:34
【问题描述】:
所以我们有一个公共类,它在那里传递名为字符串文件名的图像,我需要将 MaigickImage 与 MemoryStream 或 byte[] 一起使用,因为这是一个网络,文件的地址没有意义。不知何故,我需要将字符串文件名传递给 MemoryStream,然后将其传递给 MagickImage。当我使用 new MagickImage("fileName") 时,它给了我错误,例如找不到文件或目录。所以我需要它通过 MemoryStream 或 byte[] 而不使用 MagickImage("fileName")
private void Update(ClientFileType type, FileData newFile, int clientId, string fileName)
{
using (var image = new MagickImage("fileName"))
{
// Save frame as jpg
image.Write("fileName");
}
// Write to stream
var settings = new MagickReadSettings();
// Tells the xc: reader the image to create should be 800x600
settings.Width = 800;
settings.Height = 600;
using (var memStream = new MemoryStream())
{
// Create image that is completely purple and 800x600
using (var image = new MagickImage("xc:purple", settings))
{
// Sets the output format to png
image.Format = MagickFormat.Png;
// Write the image to the memorystream
image.Write(memStream);
}
}
// Read image from file
using (var image = new MagickImage("fileName"))
{
// Sets the output format to jpeg
image.Format = MagickFormat.Jpeg;
// Create byte array that contains a jpeg file
byte[] data = image.ToByteArray();
}
var currentFiles =
_clientFileService.Value.GetFilesByClient(clientId).Where(x => x.ClientFileType == type).Select(x => x.Id).ToList();
_clientFileService.Value.MultipleDelete(currentFiles, null);
_clientFileService
.Value
.AddFile(
new ClientFile
{
FileName = fileName,
Guid = Guid.NewGuid()
});
}
【问题讨论】:
标签: c# imagemagick memorystream