为什么我的图像看起来采用Bgra而不是Argb的格式? [英] Why do my images seem to be in the format of Bgra instead of Argb?

查看:161
本文介绍了为什么我的图像看起来采用Bgra而不是Argb的格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我对我刚刚跑过的快速测试非常困惑。我在C#中进行一些图像处理。 Get / SetPixel()已被证明太慢了,所以我使用LockBits来获取原始数据。

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.

然而,我似乎遇到了一个我无法弄清楚的情况。在扫描图像时,似乎每个像素按照顺序布置为Bgra,即蓝色字节,绿色字节,红色字节和alpha。我的印象是他们将以Argb的顺序排列。这是我正在使用的代码示例。

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 very obvious here.

推荐答案

不仅颜色颠倒BGRA,而且行也是也反过来了 - 图像的底部是内存中的第一个。这就是Windows一直以来的工作方式。

Not only are the colors reversed BGRA, but the rows are reversed as well - the bottom of the image is the first in memory. It's just the way Windows has always worked.

小端解释似乎很明显,但我认为这不是事实。如果你看一下Windows API中 COLORREF的定义,你会注意到Red是低位字节而Blue是高位字节;如果你把它存储为一个整数值,它将是RGB0。

The little-endian explanation seems obvious, but I don't think it's the truth. If you look at the definition of COLORREF in the Windows API, you'll notice that Red is the low order byte and Blue is the higher order; if you stored this as a single integer value, it would be RGB0.

这篇关于为什么我的图像看起来采用Bgra而不是Argb的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆