将图像转换为字节数组的最快方法 [英] Fastest way to convert Image to Byte array

查看:196
本文介绍了将图像转换为字节数组的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做,我捕捉到桌面,并且通讯preSS它的图像并将其发送到接收器远程桌面共享应用程序。对COM preSS的形象,我需要将其转换为一个byte []。

目前我使用这样的:

 字节公众[] imageToByteArray(System.Drawing.Image对象imageIn)
{
    MemoryStream的毫秒=新的MemoryStream();
    imageIn.Save(MS,System.Drawing.Imaging.ImageFormat.Gif);
    返回ms.ToArray();
}公众形象byteArrayToImage(字节[] byteArrayIn)
{
     MemoryStream的毫秒=新的MemoryStream(byteArrayIn);
     图像returnImage = Image.FromStream(毫秒);
     返回returnImage;
}

但我不喜欢它,因为我必须将它保存在一个的imageformat并且还可以使用的资源(慢下来),以及产生不同的COM pression results.I已经阅读使用Marshal.Copy和memcpy但我无法理解他们。

那么,还有没有其他的方法来实现这一目标?


解决方案

  

那么,还有没有其他的方法来实现这一目标?


没有。为了将图像转换为字节数组,你的有无的指定图像的格式 - 就像你当你将文本转换为字节数组指定编码

如果你担心COM pression文物,选择一个无损格式。如果你担心的CPU资源,选择哪些不打扰COM pressing的格式 - 只是原始ARGB像素,例如。不过,当然,这将导致更大的字节数组。

请注意,如果你选择一个格式,它的的包括COM pression,有一个在那么COM $ P $事后pssing字节数组是没有意义的 - 这是几乎可以肯定没有任何有益的影响。

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.

So is there any other method to achieve this goal?

解决方案

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. 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.

这篇关于将图像转换为字节数组的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆