1bpp像素遍历(找了半天,感谢github)

 /// <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 = 0;
            BitmapData gbmData = bmpSource.LockBits(rect, 
                ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
            IntPtr ScanG = gbmData.Scan0;
            unsafe
            {
                byte* g = (byte*)(void*)ScanG;
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        int index = y*gbmData.Stride + (x >> 3);
                        byte mask = (byte)(0x80 >> (x & 0x07));
                        if ((g[index] & mask)!=0)
                        {
                            grayCount++;
                        }
                    }
                   
                }
            }
            var result = (grayCount / (area + 0d));
            bmpSource.UnlockBits(gbmData);
            return result;
        }
原文地址:https://www.cnblogs.com/daxiongblog/p/5578640.html