我有一些小的透明GIF圖像(在100×100),並寫了下面的代碼在所有的像素迭代給我的RGB值:C#圖形新手問題
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);
}
}
}
代碼工作(儘管是緩慢的,因爲的GetPixel和字符串格式),但我最驚訝的是,輸出報告透明像素是黑色的!我想知道爲什麼?
0 =>紅色:綠色0:0藍:0
1 =>紅色:綠色0:0藍:0 ...
現在讓我們假設,如果我也有一個透明的gif圖像一個覆蓋圖像面積25%的黑色背景,我怎麼知道像素是透明還是黑色?