11

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

3
  • Where do you have your image? In a file or in an Image object or some Stream? Commented Feb 19, 2011 at 13:13
  • image is uploaded into a picturebox using an OpenFileDialog Commented Feb 19, 2011 at 13:14
  • Why do you need your image as a byte array? For storage? or are you trying to manipulate the image? Commented Feb 19, 2011 at 13:43

6 Answers 6

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[]));
}
Sign up to request clarification or add additional context in comments.

Comments

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.

7 Comments

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'?
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.
Fair point, thanks for explaining :) I've added a brief note on what the stride is.
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.
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 stackoverflow.com/questions/2185944/… and here stackoverflow.com/questions/1983781/…. My head hurts :)
|
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);

1 Comment

@riya7887: please use my edited version above, since it disposes the stream properly.
0

to get the bytes from any file try:

byte[] bytes =  File.ReadAllBytes(pathToFile);

Comments

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;

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.