Answered by:
how to convert byte array to bitmap in c#

Question
-
Answers
-
Another way exists.
Code BlockTypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
Bitmap bitmap1 = (Bitmap)tc.ConvertFrom(byteArray);
Or,
Code BlockImageConverter ic = new ImageConverter();
Image img = (Image)ic.ConvertFrom(byteArray);
Bitmap bitmap1 = new Bitmap(img);
-
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.
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.
-
Another way exists.
Code BlockTypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
Bitmap bitmap1 = (Bitmap)tc.ConvertFrom(byteArray);
Or,
Code BlockImageConverter ic = new ImageConverter();
Image img = (Image)ic.ConvertFrom(byteArray);
Bitmap bitmap1 = new Bitmap(img);
-
-
-
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."
-
-
-
-
-
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 -
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.Drawing2.
System.ArgumentException was unhandled
HResult=-2147024809
Message=Parameter is not valid.
Source=System.DrawingFinally 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
-
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