Click to See Complete Forum and Search --> : Why do my images seem to be in the format of Bgra instead of Argb?


BigEd781
February 24th, 2009, 12:35 AM
`

EDIT:OK, 0 /2 tonight. Darn those little endians...



So, I am very confused over a quick test that I just ran. I am doing some image processing in C#. Get/SetPixel() have proven to be too slow, so I am using LockBits to get at the raw data.

However, I seem to have hit a situation which I can't figure out. While scanning the image, it
seems that each pixel is laid out as Bgra, that is, blue byte, green byte, red byte, and alpha, in that order. I was under the impression that they would be laid out in Argb order. here is a sample of the code that I am using.


BitmapData baseData =
m_baseImage.LockBits(new Rectangle(new Point(0, 0), m_baseImage.Size),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
Bitmap test = new Bitmap(m_baseImage.Width, m_baseImage.Height);

byte* ptr = (byte*)baseData.Scan0;
for (int y = 0; y < m_baseImage.Height; ++y)
{
for (int x = 0; x < m_baseImage.Width; ++x)
{
// this works, image is copied correctly
Color c1 = Color.FromArgb(*(ptr + 3), *(ptr + 2), *(ptr + 1), *ptr);
// below does not work! Bytes are reversed.
//Color c1 = Color.FromArgb(*ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3));

test.SetPixel(x, y, c1);
ptr += 4;
}
}

m_baseImage.UnlockBits(baseData);
pictureBox1.Image = m_baseImage;
pictureBox2.Image = test;

The first line which grabs the color of the base image works, the second does not. I am pretty sure that I am missing something here.

pavilion
February 28th, 2009, 07:54 AM
the image format in brga is some kind of error in GDI+ (it's the way windows store data internally), I know it via a image processing tutorial from CodeProject. I'm using .NET 3.5 SP1 and this stupid error is still there, itjust made some surprise in the first time but I think there's no problem so far

BigEd781
February 28th, 2009, 06:13 PM
the image format in brga is some kind of error in GDI+ (it's the way windows store data internally), I know it via a image processing tutorial from CodeProject. I'm using .NET 3.5 SP1 and this stupid error is still there, itjust made some surprise in the first time but I think there's no problem so far

Thanks Pavilion, I totally forgot about this thread. After some digging, I found that this is not a bug or error, but is actually the way that Windows stores images internally. It's just one of those things. I read that it came about because of some ancient video card makers. Apparently, storing images in reverse byte (and row) order was faster. That artifact still remains today, though it is no longer needed.