简体   繁体   English

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

[英]Fastest way to convert Image to Byte array

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[].要压缩图像,我需要将其转换为 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?那么有没有其他方法可以达到这个目的呢?

There is a RawFormat property of Image parameter which returns the file format of the image. Image参数的RawFormat属性返回图像的文件格式。 You might try the following: 您可以尝试以下方法:

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

So is there any other method to achieve this goal? 那么有没有其他方法来实现这一目标?

No. In order to convert an image to a byte array you have to 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. 如果您担心CPU资源,请选择一种不打扰压缩的格式 - 例如,原始ARGB像素。 But of course that will lead to a larger byte array. 但当然这将导致更大的字节数组。

Note that if you pick a format which does include compression, there's no point in then compressing the byte array afterwards - it's almost certain to have no beneficial effect. 请注意,如果你选择一个格式包括压缩,有一个在事后再压缩字节数组是没有意义的-这是几乎可以肯定有没有益处。

I'm not sure if you're going to get any huge gains for reasons Jon Skeet pointed out. 我不确定你是否会因为Jon Skeet指出的原因获得任何巨大收益。 However, you could try and benchmark the TypeConvert.ConvertTo method and see how it compares to using your current method. 但是,您可以尝试对TypeConvert.ConvertTo方法进行基准测试,并查看它与使用当前方法的比较。

ImageConverter converter = new ImageConverter();
byte[] imgArray = (byte[])converter.ConvertTo(imageIn, typeof(byte[]));
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;
    }
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;
    }
}

The fastest way i could find out is this : 我能找到的最快方法是:

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

Hope to be useful 希望有用

Try following Code:尝试以下代码:

public Byte[] ConvertPictureToByte(System.Drawing.Image PictureFile)
{
   using (var MemStrm = new MemoryStream())
   {
      PictureFile.Save(MemStrm,PictureFile.RawFormat);
      return  MemStrm.ToArray();
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM