The fastest way to convert an image to a byte array

I am making a remote desktop sharing application in which I take a desktop image and compress it and send it to the recipient. To compress the image, I need to convert it to byte [].

I am currently 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 ImageFormat, and also use resources (Slow Down), and also create different compression results. I read about using Marshal.Copy and memcpy, but I can not understand them.

So, is there any other way to achieve this?

+82
c # memory bytearray bitmap
Jun 27 '13 at 19:54
source share
6 answers

So, is there any other way to achieve this?

No. To convert an image to a byte array, you must specify the image format - just as you need to specify the encoding when converting text to an array of bytes.

If you are worried about compression artifacts, choose a lossless format. If you are worried about CPU resources, choose a format that does not require compression β€” for example, only raw ARGB pixels. But of course, this will lead to a larger array of bytes.

Note that if you choose a format that includes compression, then there is no point in compressing an array of bytes in this case - it almost certainly has no beneficial effect.

+30
Jun 27 '13 at 19:57
source share

There is a RawFormat property of the Image parameter, which returns the image file format. You can 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(); } } 
+29
May 19 '14 at 7:05
source share

I’m not sure that you will get huge profits for the reasons that John Skeet pointed out. However, you can try and compare the TypeConvert.ConvertTo method and see how it compares using your current method.

 ImageConverter converter = new ImageConverter(); byte[] imgArray = (byte[])converter.ConvertTo(imageIn, typeof(byte[])); 
+10
Jun 27 '13 at 20:02
source share
 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; } 
+8
May 2 '14 at 8:33
source share
 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; } } 
+3
Mar 30 '17 at 8:10
source share

The quickest way to find out is:

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

Hope to be helpful

+2
Aug 14 '16 at 5:23
source share



All Articles