Can anyone please tell me how an image(.jpg,.gif,.bmp) is converted into a byte array ?
-
Where do you have your image? In a file or in an Image object or some Stream? – Albin Sunnanbo Feb 19 '11 at 13:13
-
image is uploaded into a picturebox using an OpenFileDialog – Riya Feb 19 '11 at 13:14
-
Why do you need your image as a byte array? For storage? or are you trying to manipulate the image? – yhw42 Feb 19 '11 at 13:43
6 Answers
The easiest way to convert an image to bytes is to use the ImageConverter class under the System.Drawing namespace
public static byte[] ImageToByte(Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}

- 27,666
- 26
- 83
- 129
If your image is already in the form of a System.Drawing.Image
, then you can do something like this:
public byte[] convertImageToByteArray(System.Drawing.Image image)
{
using (MemoryStream ms = new MemoryStream())
{
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
// or whatever output format you like
return ms.ToArray();
}
}
You would use this function with the image in your picture box control like this:
byte[] imageBytes = convertImageToByteArray(pictureBox1.Image);

- 74,184
- 40
- 190
- 334
-
@riya7887: please use my edited version above, since it disposes the stream properly. – MusiGenesis Feb 19 '11 at 13:53
I've assumed what you want is the pixel values. Assuming bitmap
is a System.Windows.Media.Imaging.BitmapSource
:
int stride = bitmap.PixelWidth * ((bitmap.Format.BitsPerPixel + 7) / 8);
byte[] bmpPixels = new byte[bitmap.PixelHeight * stride];
bitmap.CopyPixels(bmpPixels, stride, 0);
Note that the 'stride' is the number of bytes required for each row of pixel ddata. Some more explanation available here.
-
2Why the downvote? Surely the question can be interpreted as 'I want the image pixel values as a byte array' or 'I want the image file as a byte array'? – Cocowalla Feb 19 '11 at 13:28
-
Your answer didn't originally have the third line, and your first line threw me until I had some coffee. Edit it and I'll undo my down-vote. – MusiGenesis Feb 19 '11 at 13:44
-
Fair point, thanks for explaining :) I've added a brief note on what the stride is. – Cocowalla Feb 19 '11 at 14:22
-
Actually, are you sure your stride calculation is right? The stride needs to be 32-bit aligned, but I think your code just makes it 8-bit aligned. – MusiGenesis Feb 19 '11 at 14:34
-
It was quite a while ago I wrote the code, and I remember spending quite some time on figuring it out. I definately works :) Some more here http://stackoverflow.com/questions/2185944/why-must-stride-in-the-system-drawing-bitmap-constructor-be-a-multiple-of-4 and here http://stackoverflow.com/questions/1983781/why-does-bitmapsource-create-throw-an-argumentexception/1983886#1983886. My head hurts :) – Cocowalla Feb 19 '11 at 15:15
-
Your stride calculation would work for any 32-bit-per-pixel format, since it's already 32-bit aligned. Fortunately, the default Bitmap in .Net is ARGB8888. – MusiGenesis Feb 19 '11 at 15:33
-
Based off of MusiGenesis; helped me a lot but I had many image types. This will save any image type that it can read.
System.Drawing.Imaging.ImageFormat ImageFormat = imageToConvert.RawFormat;
byte[] Ret;
try
{
using (MemoryStream ms = new MemoryStream())
{
imageToConvert.Save(ms, ImageFormat);
Ret = ms.ToArray();
}
}
catch (Exception) { throw; }
return Ret;

- 371
- 6
- 15
to get the bytes from any file try:
byte[] bytes = File.ReadAllBytes(pathToFile);

- 5,801
- 4
- 26
- 33
You can use File.ReadAllBytes method to get bytes
If you are using FileUpload class then you can use FileBytes Property to get the Bytes as Byte Array.

- 18,056
- 9
- 55
- 79