C#图形新手问题

cos*_*son 2 .net c# graphics gdi+ visual-studio-2008

我有一些小的透明gif图像(100x100以下),并编写以下代码迭代所有像素,给我RGB值:

private void IteratePixels(string filepath)
{
    string dataFormat = String.Empty;
    Bitmap objBitmap = new Bitmap(filepath);
    int counter = 0;
    for (int y = 0; y < objBitmap.Height; y++)
    {
        for (int x = 0; x < objBitmap.Width; x++)
        {
            Color col = objBitmap.GetPixel(x, y);
            dataFormat = String.Format("{0} => Red: {1:x} Green: {2:x} Blue: {3:x}",
                counter++, col.R, col.G, col.B);
            System.Diagnostics.Debug.WriteLine(dataFormat);

            // Perform an operation on the Color value here.
            // objBitmap.SetPixel(x, y, col);
        }
    }


}
Run Code Online (Sandbox Code Playgroud)

代码工作(虽然因为GetPixel和字符串格式化很慢),但我最惊讶的是输出报告透明像素是黑色的!我想知道为什么?

0 =>红色:0绿色:0蓝色:0
1 =>红色:0绿色:0蓝色:0 ...

现在让我们说如果我确实有一个透明的gif图像,黑色背景覆盖图像区域的25%,我怎么知道像素是透明的还是黑色的?

McP*_*inM 5

图像确实有4个属性:红色,绿色,蓝色和Alpha.

Alpha是一个区域的透明度.GIF图像仅支持透明/不透明,不像其他格式,如PNG,具有完全的alpha支持,所以你可以做像40%透明像素的事情.

您可以在上面的代码中使用col.A访问它.

作为参考,MSDN Color结构是http://msdn.microsoft.com/en-us/library/system.drawing.color.aspx