imagemagick 从内存中读取图像 C#

imagemagick read image from memory C#

所以我们有 public class 向那里传递名为字符串文件名的图像,我需要将 MaigickImage 与 MemoryStream 或 byte[] 一起使用,因为这是一个网络,地址中没有意义到文件。我需要以某种方式将字符串 fileName 传递给 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()
                });
        }

请在 .NET 库上查看官方 documentation

自 2021 年 12 月 27 日起,为了举例起见,这里是有关如何执行此操作的文档示例。希望这对您有所帮助!

// Read from stream.
using (var memStream = LoadMemoryStreamImage())
{
    using (var image = new MagickImage(memStream))
    {
    }
}

// Read from byte array.
var data = LoadImageBytes();
using (var image = new MagickImage(data))
{
}

我建议你这样做:

  1. 将 file/content 加载到字节数组中
  2. 将字节数组传递到流中
  3. 将流传递到 MagicImage 构造函数中。

这是您想要的模拟实现,请检查它是否对您有帮助。 我只是按照 ImageMagick 文档来弄清楚你必须做什么。由于特定于您的服务实施,我无法完全测试。

编辑 #1 说明: 我所做的只是创建一个 MemoryStream 并在进行任何处理之前传递给 ImageMagick。我们只访问内存,而不是访问磁盘上的文件。

private void MockUpdate(ClientFileType type, FileData newFile, int clientId, string fileName)
        {
            // Create settings
            var settings = new MagickReadSettings();

            // Let's use 800x600 with format PNG
            settings.Width = 800;
            settings.Height = 600;
            settings.Format = MagickFormat.Png;

            // Buffer where we are saving the result of saving the image
            byte[] imageBuffer = null;

            // Let's create a memory stream
            using (var memStream = new MemoryStream())
            {
                // Pass the memory stream and settings into MagickImage
                using (var image = new MagickImage(memStream, settings))
                {
                    // Write the image to the memorystream
                    image.Write(memStream);

                    // Get the data into the buffer
                    imageBuffer = 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(),
                    image = imageBuffer // save the raw data here as a blob
                });
        }

        private byte[] MockProcess(ClientFileType type, int clientId, string fileName)
        {
            // Use service to load buffer with clientId and fileName
            byte[] bufferImage = service.GetImageWithClientId(clientId, fileName);

            // Let's create a memory stream
            using (var memStream = new MemoryStream())
            {
                // Pass the memory stream and settings into MagickImage
                using (var image = new MagickImage(memStream))
                {
                    DoMyProcessing(image);
                }
            }
        }


        private byte[] MockLoadImage(ClientFileType type, int clientId, string fileName)
        {
            // Use service to load buffer with clientId and fileName
            byte[] bufferImage = service.GetImageWithClientId(clientId, fileName);

            return bufferImage;
        }