![1bpp像素遍历(找了半天,感谢github) 1bpp像素遍历(找了半天,感谢github)](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
/// <summary>
/// 获取比例
/// </summary>
/// <param name="rect"></param>
/// <param name="bimp"></param>
/// <returns></returns>
private static double DoGetBlackPercent(Rectangle rect, Bitmap bmpSource)
{
var width = rect.Width;
var height = rect.Height;
int area = width * rect.Height;
int grayCount = ;
BitmapData gbmData = bmpSource.LockBits(rect,
ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
IntPtr ScanG = gbmData.Scan0;
unsafe
{
byte* g = (byte*)(void*)ScanG;
for (int y = ; y < height; y++)
{
for (int x = ; x < width; x++)
{
int index = y*gbmData.Stride + (x >> );
byte mask = (byte)(0x80 >> (x & 0x07));
if ((g[index] & mask)!=)
{
grayCount++;
}
} }
}
var result = (grayCount / (area + 0d));
bmpSource.UnlockBits(gbmData);
return result;
}