小编典典

将位图转换为字节数组

c#

使用C#,有没有更好的方式来在Windows转换Bitmap到一个byte[]比保存到临时文件和读取使用的结果FileStream


阅读 307

收藏
2020-05-19

共1个答案

小编典典

有几种方法。

图像转换器

public static byte[] ImageToByte(Image img)
{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}

这很方便,因为它不需要很多代码。

内存流

public static byte[] ImageToByte2(Image img)
{
    using (var stream = new MemoryStream())
    {
        img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        return stream.ToArray();
    }
}

除了将文件保存到内存而不是磁盘之外,这一步骤与您的工作相同。尽管有更多代码,但是您可以选择ImageFormat,并且可以在保存到内存或磁盘之间轻松地对其进行修改。

资料来源:http :
//www.vcskicks.com/image-to-byte.php

2020-05-19