【问题标题】:C# .NET Core ImageConverterC# .NET Core 图像转换器
【发布时间】:2020-06-25 16:56:30
【问题描述】:

我正在尝试在我的 ASP.NET Core 项目中使用 ImageConverter 类将 Image 转换为 byte[],但我似乎找不到该类。

我已经安装了System.Drawing.Common包,但是还是找不到。

我正在使用 .NET Core 3.1。

【问题讨论】:

  • 期望在该字节数组中找到什么?图像像素数据的原始内容?还是以已知格式保存的文件?

标签: asp.net-core .net-core system.drawing


【解决方案1】:

如果在 .NET Core 3.1 上,ImageConverter 需要 System.Windows.Extensions 包:

https://docs.microsoft.com/en-us/dotnet/api/system.drawing.imageconverter?view=netcore-3.1

在 .NET 5 中,它包含在 System.Drawing.Common

https://docs.microsoft.com/en-us/dotnet/api/system.drawing.imageconverter?view=net-5.0

【讨论】:

    【解决方案2】:

    您可以轻松地将图像转换为字节。

    protected virtual byte[] LoadPictureFromFile(string filePath)
    {
       if (!File.Exists(filePath))
           return new byte[0];
    
       return File.ReadAllBytes(filePath);
    }
    

    额外帮助..

        public byte[] ResizeImage(byte[] pictureBinary,int newWidth, int newHeight)
        {
            byte[] pictureBinaryResized;
            using (var stream = new MemoryStream(pictureBinary))
            {
                Bitmap b = null;
                try
                {
                    //try-catch to ensure that picture binary is really OK. Otherwise, we can get "Parameter is not valid" exception if binary is corrupted for some reasons
                    b = new Bitmap(stream);
                }
                catch (ArgumentException exc)
                {
                  // log error
                }
    
                if (b == null)
                {
                    //bitmap could not be loaded for some reasons
                    return new byte[0];
                }
    
                using (var destStream = new MemoryStream())
                {
    
                    ImageBuilder.Current.Build(b, destStream, new ResizeSettings
                    {
                        Width = newWidth,
                        Height = newHeight,
                        Scale = ScaleMode.Both,
                        Quality = _mediaSettings.DefaultImageQuality
                    });
                    pictureBinaryResized = destStream.ToArray();
                    b.Dispose();
                }
            }
    
            return pictureBinaryResized;
        }
    

    【讨论】:

      【解决方案3】:

      您可以尝试查看这个来加快速度,而不是 ImageConverter:

      将位图保存到流中:

      bitmap.save(stream);
      

      或者打开图片文件:

      FileStream stream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
      

      然后简单地使用 Stream2Bytes:

      byte[] OO7b = Stream2Bytes(stream);
      

      这是 Stream2Bytes 方法:

      public byte[] Stream2Bytes(Stream stream, int chunkSize = 1024)
      {
          if (stream == null)
          {
              throw new System.ArgumentException("Parameter cannot be null", "stream");
          }
      
          if (chunkSize < 1)
          {
              throw new System.ArgumentException("Parameter must be greater than zero", "chunkSize");
          }
      
          if (chunkSize > 1024 * 64)
          {
              throw new System.ArgumentException(String.Format("Parameter must be less or equal {0}", 1024 * 64), "chunkSize");
          }
      
          List<byte> buffers = new List<byte>();
      
          using (BinaryReader br = new BinaryReader(stream))
          {
              byte[] chunk = br.ReadBytes(chunkSize);
      
              while (chunk.Length > 0)
              {
                  buffers.AddRange(chunk);
                  chunk = br.ReadBytes(chunkSize);
              }
           }
      
           return buffers.ToArray();
      }
      

      【讨论】:

      • 这只是将其保存为 png 格式。此外,您也可以立即将其保存到MemoryStream,然后致电.ToArray()
      • 当然,这意味着没有缓冲。但这将需要更多的资源使用,这可能会影响可伸缩性和性能,尤其是对于大文件。
      猜你喜欢
      • 2018-10-12
      • 2022-01-27
      • 2010-09-27
      • 1970-01-01
      • 2019-01-09
      • 1970-01-01
      • 2021-09-08
      • 2017-07-26
      • 2020-08-17
      相关资源
      最近更新 更多