none
how to convert byte array to bitmap in c# RRS feed

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
  • Yes, it is possible to convert byte array to bitmap in c#.

    public Bitmap CopyDataToBitmap(byte[] data) {   //Here create the Bitmap to the know height, width and format   Bitmap bmp = new Bitmap( 352, 288, PixelFormat.Format24bppRgb);  

      //Create a BitmapData and Lock all pixels to be written   BitmapData bmpData = bmp.LockBits(                        new Rectangle(0, 0, bmp.Width, bmp.Height),                           ImageLockMode.WriteOnly, bmp.PixelFormat);

      //Copy the data from the byte array into BitmapData.Scan0   Marshal.Copy(data, 0, bmpData.Scan0, data.Length);

      //Unlock the pixels   bmp.UnlockBits(bmpData);

      //Return the bitmap   return bmp; }

    This code snippet is working fine.

    My Scenario is that: I have to read image/bitmap information from Memory Mapped file and need to display in UI as Bitmap.

    I tried many approaches to display it , but I could not able to display as mentioned below:

    1. MemoryStream ms = new MemoryStream(newPixelDataArray);            
       Bitmap bitmap = new Bitmap(ms);

    2. MemoryStream ms = new MemoryStream(newPixelDataArray);
       ms.Seek(0, SeekOrigin.Begin);
       Bitmap bitmap = new Bitmap(ms);

    3. MemoryStream ms = new MemoryStream(newPixelDataArray);

    ms.Position = 0;
       Bitmap bitmap = new Bitmap(ms);


    4. // Create copy of byte array.
       byte[] newPixelDataArray = new byte[byteArray.Length];
       byteArray.CopyTo(newPixelDataArray, 0);

      Then performed bitmap creation.

    5. Image.FromStream(MemoryStream) 

    6. Image.FromHbitmap(IntPtr)

    Each tries I was getting exception as:

    1.

    System.ArgumentException was unhandled
      HResult=-2147024809
      Message=Parameter is not valid.
      Source=System.Drawing

    2.

    System.ArgumentException was unhandled
      HResult=-2147024809
      Message=Parameter is not valid.
      Source=System.Drawing

    Finally I have solved my problem by below mentioned link and also the same code also copied above.

    Thanks to you providing nice answer.

    For more details go through the below link: http://www.tek-tips.com/viewthread.cfm?qid=1264492


    • Edited by Pitambar S Tuesday, January 16, 2018 10:53 AM
    • Proposed as answer by yg_sunshine Thursday, November 14, 2019 2:29 AM
    Tuesday, January 16, 2018 10:44 AM
  • Thanks for Pitambar S's answer! I tried other answers, they don't work for me (Parameter is not valid), only this one works!
    • Edited by yg_sunshine Thursday, November 14, 2019 2:31 AM
    Thursday, November 14, 2019 2:30 AM