我正在尝试学习用于图像处理的 LockBitmap 类,并且遇到了下面发布的这段代码。基本上它返回 xy 坐标的颜色。
当然,这种方法只有在我执行source.LockBits()
and Marshal.Copy()
/之后才有效unsafe context
。
public Color GetPixel(int x, int y, Bitmap source)
{
int Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);
int Width = source.Width;
int Height = source.Height;
Color clr = Color.Empty;
// Get color components count
int cCount = Depth / 8;
int PixelCounts = Width * Height;
byte[] Pixels = new byte[cCount * PixelCounts];
// Get start index of the specified pixel
int i = ((y * Width) + x) * cCount;
byte b = Pixels[i];
byte g = Pixels[i + 1];
byte r = Pixels[i + 2];
byte a = Pixels[i + 3]; // a
clr = Color.FromArgb(a, r, g, b);
return clr;
}
- 什么是
cCount
,为什么总是这样Depth / 8
? int i = ((y * Width) + x) * cCount
,这是从 (x,y) 坐标转换为 的固定公式Pixels[i]
吗?为什么?