Avcrash
August 2nd, 2009, 04:09 AM
Hi there. I have got a question. Here goes the task. I have a byte array needs to be converted to bitmap. Here the code I use.
public Bitmap return_bmp()
{
Bitmap bmp = new Bitmap(size_of_line, size_of_row,PixelFormat.Format24bppRgb);
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
IntPtr ptr = bmpData.Scan0;
byte[] rgbValues = new byte[bmpData.Stride*bmpData.Height];
for (int i = 0; i < number_of_elements_in_array; i++)
{
rgbValues[i] = (byte)vals[i];
rgbValues[2 * i] = (byte)vals[i];
rgbValues[3 * i] = (byte)vals[i];
}
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bmpData.Stride*bmpData.Height);
bmp.UnlockBits(bmpData);
return bmp;
}
The problem is that I get the hodgepodge, not an image I crave for. The rgbValues structure is - the first size_of_line elements are the first line, the next value is the first pixel of the second line and so on. Please, help me. I've cudgeled my brain aboit it. Thanks in advance.
public Bitmap return_bmp()
{
Bitmap bmp = new Bitmap(size_of_line, size_of_row,PixelFormat.Format24bppRgb);
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
IntPtr ptr = bmpData.Scan0;
byte[] rgbValues = new byte[bmpData.Stride*bmpData.Height];
for (int i = 0; i < number_of_elements_in_array; i++)
{
rgbValues[i] = (byte)vals[i];
rgbValues[2 * i] = (byte)vals[i];
rgbValues[3 * i] = (byte)vals[i];
}
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bmpData.Stride*bmpData.Height);
bmp.UnlockBits(bmpData);
return bmp;
}
The problem is that I get the hodgepodge, not an image I crave for. The rgbValues structure is - the first size_of_line elements are the first line, the next value is the first pixel of the second line and so on. Please, help me. I've cudgeled my brain aboit it. Thanks in advance.