Why do my images seem to be in Bgra format instead of Argb?

So, I am very confused by the quick test that I just ran. I am involved in image processing in C #. Get / SetPixel () turned out to be too slow, so I use LockBits to get raw data.

However, it seems that I got into a situation that I can not understand. When scanning the image, it seems that each pixel is laid out as Bgra, i.e. blue byte, green byte, red byte and alpha in that order. I got the impression that they will be laid out in the Arbb horde. here is an example of the code 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 that captures the color of the base image works, the second does not. I am pretty sure that something very obvious is missing.

+4
source share
2 answers

Not only are the colors inverted by BGRA, but the lines are also reversed - the bottom of the image is the first in memory. This is exactly how Windows has always worked.

A little-drive explanation seems obvious, but I don't think it's true. If you look at the definition of COLORREF in the Windows API , you will notice that Red is the low byte and Blue the higher; if you saved this as a single integer, it will be RGB0.

+6
source

ARGB refers to the byte order in words selected as words. If you select bytes one at a time, you will get em low to hi, since the IBM PC is little-endian

+6
source

All Articles