15

Hi I wanna convert binary array to bitmap and show image in a picturebox. I wrote the following code but I got exception that says that the parameter is not valid .

  public static Bitmap ByteToImage(byte[] blob)
    {
        MemoryStream mStream = new MemoryStream();
        byte[] pData = blob;
        mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
        Bitmap bm = new Bitmap(mStream);
        mStream.Dispose();
        return bm;

    }
4
  • 4
    Why are you copying the byte array? Why not just do using (MemoryStream mStream = new MemoryStream(blob);) {return new Bitmap(mStream);} Commented Dec 17, 2012 at 11:37
  • You want to convert image to stream and store to database as varbinary? Commented Dec 17, 2012 at 11:38
  • no i wanna read it from db and dsplay it in picbox Commented Dec 17, 2012 at 11:42
  • this may help you satindersinght.blogspot.in/2012/03/… Commented Dec 17, 2012 at 11:44

3 Answers 3

20

It really depends on what is in blob. Is it a valid bitmap format (like PNG, BMP, GIF, etc?). If it is raw byte information about the pixels in the bitmap, you can not do it like that.

It may help to rewind the stream to the beginning using mStream.Seek(0, SeekOrigin.Begin) before the line Bitmap bm = new Bitmap(mStream);.

public static Bitmap ByteToImage(byte[] blob)
{
    using (MemoryStream mStream = new MemoryStream())
    {
         mStream.Write(blob, 0, blob.Length);
         mStream.Seek(0, SeekOrigin.Begin);

         Bitmap bm = new Bitmap(mStream);
         return bm;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Please take a look at this rejected edit. From the msdn it looks like he is right.
How can this work when the MS doc is clearly stating "You must keep the stream open for the lifetime of the Bitmap."? Source: learn.microsoft.com/en-us/dotnet/api/… Also, this answer does not work in my case, where I use the Image for Telerik Report. Here I get a rendering error when I dispose the stream.
5

Don't dispose of the MemoryStream. It now belongs to the image object and will be disposed when you dispose the image.

Also consider doing it like this

var ms = new MemoryStream(blob);
var img = Image.FromStream(ms);
.....
img.Dispose(); //once you are done with the image.

7 Comments

i trace my code i get argumanexeptioninvalid at this code Bitmap bm = new Bitmap(mStream);
This is not true! The stream and the byte information do not "belong to the image object". The stream must even be disposed of to not get memory leaks.
@ThorstenDittmar I've worked with this a lot and I can tell you that disposing the memory stream will cause problems. Don't believe me? Try it for yourself.
We're not talking WPF where ;-)
Well, I tried disposing the stream after loading the image from it. It works fine.
|
0
System.IO.MemoryStream mStrm = new System.IO.MemoryStream(your byte array);
Image im = Image.FromStream(mStrm);
im.Save("image.bmp");

Try this. If you still get any error or exception; please post your bytes which you are trying to convert to image. There should be problem in your image stream....

3 Comments

i do it too but i get parameter is not valid exception
Ok friend, can you please post your bytes converted into string here?
How do the image bytes get into the database? Are you really sure that the bytes in the database represent an image in format PNG, GIF, BMP, etc.? If not, you can not use the Bitmap constructor.

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.