C# 将图像转换为字节数组的最快方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17352061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:16:33  来源:igfitidea点击:

Fastest way to convert Image to Byte array

c#memorybitmapbytearray

提问by user2529551

I am making Remote Desktop sharing application in which I capture an image of the Desktop and Compress it and Send it to the receiver. To compress the image I need to convert it to a byte[].

我正在制作远程桌面共享应用程序,在其中我捕获桌面图像并将其压缩并将其发送给接收器。要压缩图像,我需要将其转换为字节 []。

Currently I am using this:

目前我正在使用这个:

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
    return  ms.ToArray();
}

public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}

But I don't like it because I have to save it in a ImageFormat and that may also use up resources (Slow Down) as well as produce different compression results.I have read on using Marshal.Copy and memcpy but I am unable to understand them.

但我不喜欢它,因为我必须将它保存在 ImageFormat 中,这也可能会耗尽资源(慢下来)并产生不同的压缩结果。我已经阅读了使用 Marshal.Copy 和 memcpy 但我无法了解他们。

So is there any other method to achieve this goal?

那么有没有其他方法可以实现这个目标?

采纳答案by Jon Skeet

So is there any other method to achieve this goal?

那么有没有其他方法可以实现这个目标?

No. In order to convert an image to a byte array you haveto specify an image format - just as you have to specify an encoding when you convert text to a byte array.

不。为了将图像转换为字节数组,您必须指定图像格式 - 就像将文本转换为字节数组时必须指定编码一样。

If you're worried about compression artefacts, pick a lossless format. If you're worried about CPU resources, pick a format which doesn't bother compressing - just raw ARGB pixels, for example. But of course that will lead to a larger byte array.

如果您担心压缩伪影,请选择无损格式。如果您担心 CPU 资源,请选择一种无需压缩的格式 - 例如,只需原始 ARGB 像素。但这当然会导致更大的字节数组。

Note that if you pick a format which doesinclude compression, there's no point in then compressing the byte array afterwards - it's almost certain to have no beneficial effect.

请注意,如果您选择了一种包含压缩的格式,之后再压缩字节数组是没有意义的 - 几乎可以肯定没有任何有益效果。

回答by keyboardP

I'm not sure if you're going to get any huge gains for reasons Jon Skeet pointed out. However, you could try and benchmark the TypeConvert.ConvertTomethod and see how it compares to using your current method.

由于 Jon Skeet 指出的原因,我不确定您是否会获得任何巨大的收益。但是,您可以尝试对TypeConvert.ConvertTo方法进行基准测试,并查看它与使用当前方法的对比情况。

ImageConverter converter = new ImageConverter();
byte[] imgArray = (byte[])converter.ConvertTo(imageIn, typeof(byte[]));

回答by bhadresh

public static byte[] ReadImageFile(string imageLocation)
    {
        byte[] imageData = null;
        FileInfo fileInfo = new FileInfo(imageLocation);
        long imageFileLength = fileInfo.Length;
        FileStream fs = new FileStream(imageLocation, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        imageData = br.ReadBytes((int)imageFileLength);
        return imageData;
    }

回答by Newt

There is a RawFormat property of Image parameter which returns the file format of the image. You might try the following:

Image 参数有一个 RawFormat 属性,它返回图像的文件格式。您可以尝试以下操作:

// extension method
public static byte[] imageToByteArray(this System.Drawing.Image image)
{
    using(var ms = new MemoryStream())
    {
        image.Save(ms, image.RawFormat);
        return ms.ToArray();
    }
}

回答by alireza amini

The fastest way i could find out is this :

我能找到的最快方法是:

var myArray = (byte[]) new ImageConverter().ConvertTo(InputImg, typeof(byte[]));

Hope to be useful

希望有用

回答by Ahmad Aghazadeh

public static class HelperExtensions
{
    //Convert Image to byte[] array:
    public static byte[] ToByteArray(this Image imageIn)
    {
        var ms = new MemoryStream();
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        return ms.ToArray();
    }

    //Convert byte[] array to Image:
    public static Image ToImage(this byte[] byteArrayIn)
    {
        var ms = new MemoryStream(byteArrayIn);
        var returnImage = Image.FromStream(ms);
        return returnImage;
    }
}