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

  • 問題


  • is it possible to convert byte array to bitmap in c#
    regards
    ALGATES
    2007年12月25日 上午 05:08

解答

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

     

    2007年12月25日 上午 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.

     

    2007年12月25日 上午 07:15

所有回覆

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

     

    2007年12月25日 上午 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);

     

    2007年12月25日 上午 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;

    }

    }




    2008年8月26日 上午 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.
    2010年7月16日 下午 08: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."

    2010年7月16日 下午 09:54
  • Thank you. This was useful.
    2011年4月26日 下午 05:07
  • thanks))), it was really helpful
    2012年5月18日 下午 06: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?

    2012年5月18日 下午 08:38
  • so many thanks its work fine.
    2013年12月21日 下午 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

    2013年12月21日 下午 04: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


    • 已編輯 Pitambar S 2018年1月16日 上午 10:53
    • 已提議為解答 yg_sunshine 2019年11月14日 上午 02:29
    2018年1月16日 上午 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!
    2019年11月14日 上午 02:30