Updated on Kisan Patel
Problem
c# – Convert a bitmap into a byte array
c# – Convert Bitmap to byte
C# convert a BitMap to Byte[]
Bitmap to byte array in C# – .NET
Solution
There are a couple ways.
Using ImageConverter Class
public static byte[] ImageToByte(Image img) { ImageConverter converter = new ImageConverter(); return (byte[])converter.ConvertTo(img, typeof(byte[])); }
This one is convenient because it doesn’t require a lot of code.
Using MemoryStream Class
public static byte[] ImageToByte2(Image img) { byte[] byteArray = new byte[0]; using (MemoryStream stream = new MemoryStream()) { img.Save(stream, System.Drawing.Imaging.ImageFormat.Png); stream.Close(); byteArray = stream.ToArray(); } return byteArray; }