Problem Copying Raw RGB Data to Bitmap
-
Hi All, I am using the following function to copy RAW RGB byte array to Bitmap object. The problem I am facing is when I copy the byte array content through SetPixel method, Image construction is OK and color are proper on the Bitmap. But when I try to use the following function it some how swaps the Red and Blue bytes in the Bitmap causing wrong colors on the Image.
static int WriteBitmapFile(string filename, int width, int height, byte[] imageData)
{
using (Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb))
{
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0,bmp.Width,bmp.Height),ImageLockMode.WriteOnly,bmp.PixelFormat);
Marshal.Copy(imageData, 0, bmpData.Scan0, imageData.Length);
bmp.UnlockBits(bmpData);
bmp.Save(filename);
}
return 1;
}It seems though when I am copying the byte array in one go it swaps the Red and Blue bytes. SetPixel works fine but is too slow. Any help in this regard is highly appreciated. Regards,
-
Hi All, I am using the following function to copy RAW RGB byte array to Bitmap object. The problem I am facing is when I copy the byte array content through SetPixel method, Image construction is OK and color are proper on the Bitmap. But when I try to use the following function it some how swaps the Red and Blue bytes in the Bitmap causing wrong colors on the Image.
static int WriteBitmapFile(string filename, int width, int height, byte[] imageData)
{
using (Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb))
{
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0,bmp.Width,bmp.Height),ImageLockMode.WriteOnly,bmp.PixelFormat);
Marshal.Copy(imageData, 0, bmpData.Scan0, imageData.Length);
bmp.UnlockBits(bmpData);
bmp.Save(filename);
}
return 1;
}It seems though when I am copying the byte array in one go it swaps the Red and Blue bytes. SetPixel works fine but is too slow. Any help in this regard is highly appreciated. Regards,
Indeed,
GetPixel
/SetPixel
is not the way to perform operations on an entire image. Here are 3 ideas that may help: 1. you could write a little loop that swaps the bytes in the byte array before executing the code shown. 2. you could try a Bitmap constructor that takes anIntPtr
to raw data (may get the same result you have now); make sure to read MSDN's remark though. 3. you could apply aColorMatrix
transformation once the bitmap is filled. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Indeed,
GetPixel
/SetPixel
is not the way to perform operations on an entire image. Here are 3 ideas that may help: 1. you could write a little loop that swaps the bytes in the byte array before executing the code shown. 2. you could try a Bitmap constructor that takes anIntPtr
to raw data (may get the same result you have now); make sure to read MSDN's remark though. 3. you could apply aColorMatrix
transformation once the bitmap is filled. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
Thanks for the timely reply Luc. Can you give some pointers/links/example on how to apply ColorMatrix? Thanks and Regards,
-
Hi All, I am using the following function to copy RAW RGB byte array to Bitmap object. The problem I am facing is when I copy the byte array content through SetPixel method, Image construction is OK and color are proper on the Bitmap. But when I try to use the following function it some how swaps the Red and Blue bytes in the Bitmap causing wrong colors on the Image.
static int WriteBitmapFile(string filename, int width, int height, byte[] imageData)
{
using (Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb))
{
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0,bmp.Width,bmp.Height),ImageLockMode.WriteOnly,bmp.PixelFormat);
Marshal.Copy(imageData, 0, bmpData.Scan0, imageData.Length);
bmp.UnlockBits(bmpData);
bmp.Save(filename);
}
return 1;
}It seems though when I am copying the byte array in one go it swaps the Red and Blue bytes. SetPixel works fine but is too slow. Any help in this regard is highly appreciated. Regards,
This is because the pixel format expects the bytes in the other order from what you're providing (BGR, I think. The 4 byte one is BGRA iirc). I would transform the bytes on the way in:
byte[] newImageData = new byte[imageData.Length];
for(int i = 0; i < newImageData.Length; i += 3){
newImageData[i] = imageData[i + 2];
newImageData[i + 1] = imageData[i + 1];
newImageData[i + 2] = imageData[i];
}Unless the image is really large, that should be negligible in time and memory terms and easier than post-processing the image with a ColorFilter.
-
This is because the pixel format expects the bytes in the other order from what you're providing (BGR, I think. The 4 byte one is BGRA iirc). I would transform the bytes on the way in:
byte[] newImageData = new byte[imageData.Length];
for(int i = 0; i < newImageData.Length; i += 3){
newImageData[i] = imageData[i + 2];
newImageData[i + 1] = imageData[i + 1];
newImageData[i + 2] = imageData[i];
}Unless the image is really large, that should be negligible in time and memory terms and easier than post-processing the image with a ColorFilter.
You are saying that bitmap is BGR, but I tried using SetPixel with RGB pattern and it worked fine. Does SetPixel works differently than copying RGB data?
-
You are saying that bitmap is BGR, but I tried using SetPixel with RGB pattern and it worked fine. Does SetPixel works differently than copying RGB data?