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

  • Pergunta


  • is it possible to convert byte array to bitmap in c#
    regards
    ALGATES
    terça-feira, 25 de dezembro de 2007 05:08

Respostas

  • 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);

     

    terça-feira, 25 de dezembro de 2007 07:31
  • 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.

     

    terça-feira, 25 de dezembro de 2007 07:15

Todas as Respostas

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

     

    terça-feira, 25 de dezembro de 2007 07:15
  • 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);

     

    terça-feira, 25 de dezembro de 2007 07:31
  • 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;

    }

    }




    terça-feira, 26 de agosto de 2008 07:21
  • 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.
    sexta-feira, 16 de julho de 2010 20:53
  • 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."

    sexta-feira, 16 de julho de 2010 21:54
  • Thank you. This was useful.
    terça-feira, 26 de abril de 2011 17:07
  • thanks))), it was really helpful
    sexta-feira, 18 de maio de 2012 18:34
  • "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?

    sexta-feira, 18 de maio de 2012 20:38
  • so many thanks its work fine.
    sábado, 21 de dezembro de 2013 12:39
  • 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

    sábado, 21 de dezembro de 2013 16:58
  • 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


    • Editado Pitambar S terça-feira, 16 de janeiro de 2018 10:53
    • Sugerido como Resposta yg_sunshine quinta-feira, 14 de novembro de 2019 02:29
    terça-feira, 16 de janeiro de 2018 10:44
  • Thanks for Pitambar S's answer! I tried other answers, they don't work for me (Parameter is not valid), only this one works!
    • Editado yg_sunshine quinta-feira, 14 de novembro de 2019 02:31
    quinta-feira, 14 de novembro de 2019 02:30