I am trying to learn LockBitmap class for image processing and I came across this code posted below. Basically it returns the color of a x-y coordinate.
Of course this method only works after I perform 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;
}
- What is
cCount
, why is it alwaysDepth / 8
? int i = ((y * Width) + x) * cCount
, is this a fixed formula to convert from (x,y) coordinate toPixels[i]
? Why?