none
how to convert byte array to bitmap in c#

Answers

  • Another way exists.

    Code Block

    TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));

    Bitmap bitmap1 = (Bitmap)tc.ConvertFrom(byteArray);

    Or,

    Code Block

    ImageConverter ic = new ImageConverter();

    Image img = (Image)ic.ConvertFrom(byteArray);

    Bitmap bitmap1 = new Bitmap(img);

     

    Tuesday, December 25, 2007 7:31 AM
  • To some extent that would seem to depend on the contents of the array.

     

    One approach would be to wrap an instance of the MemoryStream class around the byte array and then create an instance of the Bitmap class by calling the Bitmap (Stream) constructor.

     

    Tuesday, December 25, 2007 7:15 AM

All replies

  • To some extent that would seem to depend on the contents of the array.

     

    One approach would be to wrap an instance of the MemoryStream class around the byte array and then create an instance of the Bitmap class by calling the Bitmap (Stream) constructor.

     

    Tuesday, December 25, 2007 7:15 AM
  • Another way exists.

    Code Block

    TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));

    Bitmap bitmap1 = (Bitmap)tc.ConvertFrom(byteArray);

    Or,

    Code Block

    ImageConverter ic = new ImageConverter();

    Image img = (Image)ic.ConvertFrom(byteArray);

    Bitmap bitmap1 = new Bitmap(img);

     

    Tuesday, December 25, 2007 7:31 AM
  • The ImageConverter and TypeConverter internally both use streams.

    Code Snippet

    public static Bitmap BytesToBitmap(byte[] byteArray)
    {

    using (MemoryStream ms = new MemoryStream(byteArray))
    {
    Bitmap img = (Bitmap)Image.FromStream(ms);
    return img;

    }

    }




    Tuesday, August 26, 2008 7:21 AM
  • I ran into a "generic error in GDI" exception. Turns out you need to keep the MemoryStream around if you do anything else with the Bitmap.
    Friday, July 16, 2010 8:53 PM
  • Yes, that requirement is documented:

    Bitmap Constructor (Stream) (System.Drawing)

    First sentence under Remarks:

    "You must keep the stream open for the lifetime of the Bitmap."

    Friday, July 16, 2010 9:54 PM
  • Thank you. This was useful.
    Tuesday, April 26, 2011 5:07 PM
  • thanks))), it was really helpful
    Friday, May 18, 2012 6:34 PM
  • "is it possible to convert byte array to bitmap in c#"

    It is if it contains an image or at least the pixels of a image.  Can you contruct a bitmap from the File created by writing the byte array to a file using WirteAllBytes?

    Friday, May 18, 2012 8:38 PM
  • so many thanks its work fine.
    Saturday, December 21, 2013 12:39 PM
  • Yea but no windows forms question more for the C# forums

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=csharpgeneral


    Success
    Cor

    Saturday, December 21, 2013 4:58 PM