Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write byte[] to a Bitmap? 1

Status
Not open for further replies.

bledazemi

Programmer
Jul 14, 2005
93
GB
Hi everyone,

I am trying to write an array of bytes to a Bitmap. The array of bytes contains only the bitmap data (no header info, simply the pixels).

I know the PixelFormat and the size of the Bitmap and I have the data. What I want to know is how to put this data into the Bitmap.

Here is how am I trying to do this:

Code:
/// <summary>
/// function CopyDataToBitmap
/// Purpose: Given the pixel data return a bitmap of size [352,288] and with PixelFormat=24RGB 
/// </summary>
/// <param name="data">Byte array with pixel data</param>
public Bitmap CopyDataToBitmap(byte[] data)
{
  //I
  Bitmap bmp = new Bitmap( 352, 288, PixelFormat.Format24bppRgb);  

  //Here the code to put data array into Bitmap 
  //Unfortunatelly i dont know how :(  

  //Return the bitmap 
  return bmp;

}

I would appreciate any help or suggestions from you guys.

Thanks in advance


 
Code:
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(data))
{
	Bitmap bmp = new Bitmap(ms);
}
 
Thanks Shelton, I tried what you suggested but it did not work for me it generated the followin exception:

System.ArgumentException
Message="Parameter is not valid."
Source="System.Drawing"
StackTrace:
at System.Drawing.Bitmap..ctor(Stream stream)


I am guessing that this exception is perhaps due to the fact that the memory Data passed to the memory stream contains only the Bitmap pixel data and no bitmap header information.
 
Hi SHelton ,

I found the solution to my problem and now it works ok. Thank you for your help, it helped me on my investigation to the problem.


Here is how i did it:

Code:
/// <summary>
/// function CopyDataToBitmap
/// Purpose: Given the pixel data return a bitmap of size [352,288],PixelFormat=24RGB 
/// </summary>
/// <param name="data">Byte array with pixel data</param>
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;
}


Solution was based on instructions at :


Once again thanks



"It is in our collective behaviour that we are most mysterious" Lewis Thomas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top