11

Can anyone please tell me how an image(.jpg,.gif,.bmp) is converted into a byte array ?

David Basarab
  • 72,212
  • 42
  • 129
  • 156
Riya
  • 529
  • 4
  • 10
  • 18

6 Answers6

12

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[]));
}
santosh singh
  • 27,666
  • 26
  • 83
  • 129
6

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);
MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
6

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.

Community
  • 1
  • 1
Cocowalla
  • 13,822
  • 6
  • 66
  • 112
  • 2
    Why 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
  • Ah yes, it's coming back to me now! – Cocowalla Feb 19 '11 at 16:04
0

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;
Neon Blue
  • 371
  • 6
  • 15
0

to get the bytes from any file try:

byte[] bytes =  File.ReadAllBytes(pathToFile);
Alex Pacurar
  • 5,801
  • 4
  • 26
  • 33
-1

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.

Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79