【问题标题】:imagemagick read image from memory C#imagemagick 从内存中读取图像 C#
【发布时间】: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


    【解决方案1】:

    这是您想要的模拟实现,请检查它是否对您有帮助。 我只是按照 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;
            }
    

    【讨论】:

      【解决方案2】:

      请查看.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. 将文件/内容加载到字节数组中
      2. 将字节数组传递到流中
      3. 将流传递到 MagicImage 构造函数中。

      【讨论】:

      • 我仍然无法得到它。我正在使用字节数组传递它,但它仍然无法正常工作
      • 我找不到 LoadMemoryStreamImage() 和 LoadImageBytes() 方法
      • Here is a direct sample from the documentation on how to do what you desire. 请注意前两种方法,它们是为此示例定制的,因此您可能需要根据需要进行调整。
      • 仍然,有很多东西不适合我。我有字节数组,但是当我尝试实现它时,它不能在 MagicImage 中使用,所以我的字符串文件名是字节数组
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      • 2012-12-03
      • 2015-09-18
      • 2015-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多