Reputation: 295
I am trying to convert a bitmap to Format24bppRgb. since I need to have a set of color array for each pixel, I used unsafe code.
the problem is that a bitmap is converted into Jpeg somehow. when I look at the color array, some has a weird color (yellow or blue) among blacks and whites.
is there a way to prevent the color changing ?
//image
mstr = new MemoryStream(msgSet.VMSPage2Image);
bitmapFile = new Bitmap(mstr);
data = bitmapFile.LockBits(new Rectangle(0, 0, bitmapFile.Width, bitmapFile.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
colors = new Color[data.Height, data.Width];
unsafe
{
byte* imgPtr = (byte*)(data.Scan0);
for (int i = 0; i < data.Height; i++)
{
for (int j = 0; j < data.Width; j++)
{
// write the logic implementation here
imgPtr += 1;
int G = *imgPtr;
imgPtr += 1;
int R = *imgPtr;
imgPtr += 1;
int B = *imgPtr;
colors[i, j] = Color.FromArgb(R, G, B);
}
imgPtr += data.Stride - data.Width * 3;
}
}
Upvotes: 0
Views: 4409
Reputation: 124800
The first problem that I see is that you are incrementing the pointer before dereferencing it the first time.
Next, you aren't advancing the pointer to the next row properly.
Finally, those pixels are going to be stored in BGR order, not GRB. So, for 24bit rgb...
for (int y = 0; i < data.Height; i++)
{
byte* imgPtr = (byte*)(data.Scan0 + (data.Stride * y));
for (int x = 0; j < data.Width; j++)
{
int b = *imgPtr++;
int g = *imgPtr++;
int r = *imgPtr++;
colors[x, y] = Color.FromArgb(r, g, b);
}
}
Upvotes: 2
Reputation: 38087
Are you sure you are reading the color data correctly? I thought bitmaps used BGR for the layout of the bytes in a 24 bit bitmap.
Upvotes: 0