Asp.net core C#.NET核心图像转换器

Asp.net core C#.NET核心图像转换器,asp.net-core,.net-core,system.drawing,Asp.net Core,.net Core,System.drawing,我正在尝试使用ASP.NET核心项目中的ImageConverter类将图像转换为字节[],但我似乎找不到该类 我已安装System.Drawing.Common软件包,但仍然找不到它 我使用的是.NET Core 3.1。而不是ImageConverter,您可以尝试查看以下内容以提高速度: 将位图保存到流: bitmap.save(stream); 或打开图像文件: FileStream stream = new FileStream(imageFilePath, FileMode.Ope

我正在尝试使用ASP.NET核心项目中的
ImageConverter
类将
图像
转换为
字节[]
,但我似乎找不到该类

我已安装System.Drawing.Common软件包,但仍然找不到它


我使用的是.NET Core 3.1。

而不是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();
}
public byte[]Stream2Bytes(Stream-Stream,int-chunkSize=1024)
{
if(流==null)
{
抛出新的System.ArgumentException(“参数不能为null”,“流”);
}
if(chunkSize<1)
{
抛出新的System.ArgumentException(“参数必须大于零”,“chunkSize”);
}
如果(chunkSize>1024*64)
{
抛出新的System.ArgumentException(String.Format(“参数必须小于或等于{0}”),1024*64,“chunkSize”);
}
列表缓冲区=新列表();
使用(BinaryReader br=新的BinaryReader(流)
{
byte[]chunk=br.ReadBytes(chunkSize);
while(chunk.Length>0)
{
AddRange(块);
chunk=br.ReadBytes(chunkSize);
}
}
返回缓冲区。ToArray();
}

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

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;
    }

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

在.NET 5中,它包含在
系统.Drawing.Common中:


在该字节数组中,您希望找到什么?图像像素数据的原始内容?或以已知格式保存的文件?这些文件只需将其保存为png格式。此外,您也可以立即将其保存到
内存流中,然后调用
.ToArray()
。当然,这意味着没有缓冲。但这将需要更多的资源使用,这可能会影响可扩展性和性能,尤其是对于大型文件。