using System; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.InteropServices; namespace HNWD.Pregrant.Common { public class BitmapBlack { public static bool bSuccessed = true; public static Bitmap BitmapToBlack(Bitmap img, Double hsb) { int w = img.Width; int h = img.Height; Bitmap bmp = new Bitmap(w, h, PixelFormat.Format1bppIndexed); BitmapData data = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);//将 Bitmap 锁定到系统内存中 for (int y = 0; y < h; y++) { byte[] scan = new byte[(w + 7) / 8]; for (int x = 0; x < w; x++) { Color c = img.GetPixel(x, y); if (c.GetBrightness() >= hsb) scan[x / 8] |= (byte)(0x80 >> (x % 8));//亮度值和原来比较,二值化处理 } Marshal.Copy(scan, 0, (IntPtr)((int)data.Scan0 + data.Stride * y), scan.Length); } bmp.UnlockBits(data);//将 Bitmap 锁定到系统内存中 return bmp; } //图片二值化 public static Bitmap binarization(Bitmap img) { bSuccessed = true; int width = img.Width; int height = img.Height; int area = width * height; int[,] gray = new int[width, height]; int average = 0;// 灰度平均值 int graysum = 0; int graymean = 0; int grayfrontmean = 0; int graybackmean = 0; int pixelGray; int front = 0; int back = 0; int[] pix = new int[width * height]; for (int i = 1; i < width; i++) { // 不算边界行和列,为避免越界 for (int j = 1; j < height; j++) { pix[j * width + i] = img.GetPixel(i, j).ToArgb(); //得到单点上的颜色值 int x = j * width + i; int r = (pix[x] >> 16) & 0xff; int g = (pix[x] >> 8) & 0xff; int b = pix[x] & 0xff; pixelGray = (int)(0.3 * r + 0.59 * g + 0.11 * b);// 计算每个坐标点的灰度 gray[i, j] = (pixelGray << 16) + (pixelGray << 8) + (pixelGray); graysum += pixelGray; } } graymean = (int)(graysum / area);// 整个图的灰度平均值 average = graymean; for (int i = 0; i < width; i++) // 计算整个图的二值化阈值 { for (int j = 0; j < height; j++) { if (((gray[i, j]) & (0x0000ff)) < graymean) { graybackmean += ((gray[i, j]) & (0x0000ff)); back++; } else { grayfrontmean += ((gray[i, j]) & (0x0000ff)); front++; } } } if (front == 0 || back == 0) { bSuccessed = false; return null; } int frontvalue = (int)(grayfrontmean / front);// 前景中心 int backvalue = (int)(graybackmean / back);// 背景中心 float[] G = new float[frontvalue - backvalue + 1];// 方差数组 int s = 0; for (int i1 = backvalue; i1 < frontvalue + 1; i1++)// 以前景中心和背景中心为区间采用大津法算法(OTSU算法) { back = 0; front = 0; grayfrontmean = 0; graybackmean = 0; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (((gray[i, j]) & (0x0000ff)) < (i1 + 1)) { graybackmean += ((gray[i, j]) & (0x0000ff)); back++; } else { grayfrontmean += ((gray[i, j]) & (0x0000ff)); front++; } } } if (front == 0 || back == 0) { bSuccessed = false; return null; } grayfrontmean = (int)(grayfrontmean / front); graybackmean = (int)(graybackmean / back); G[s] = (((float)back / area) * (graybackmean - average) * (graybackmean - average) + ((float)front / area) * (grayfrontmean - average) * (grayfrontmean - average)); s++; } float max = G[0]; int index = 0; for (int i = 1; i < frontvalue - backvalue + 1; i++) { if (max < G[i]) { max = G[i]; index = i; } } for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int inin = j * width + i; if (((gray[i, j]) & (0x0000ff)) < (index + backvalue)) { pix[inin] = Color.FromArgb(0, 0, 0).ToArgb(); } else { pix[inin] = Color.FromArgb(255, 255, 255).ToArgb(); } } } Bitmap temp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555); for (int i = 1; i < width; i++) { // 不算边界行和列,为避免越界 for (int j = 1; j < height; j++) { img.SetPixel(i, j, Color.FromArgb(pix[j * width + i])); } } return temp; } /// /// 防锯齿 /// /// /// /// /// /// [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")] private static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni ); private static int BitmapHandlerAction = 75;//启用 或 关闭 平滑效果 /// /// 关闭 平滑效果 /// /// public static int SetBitmapSmoothDisable() { return SystemParametersInfo(BitmapHandlerAction, 0, null, 0); //关闭 平滑效果 } /// /// 启用 平滑效果 /// /// public static int SetBitmapSmoothEnable() { return SystemParametersInfo(BitmapHandlerAction, 1, null, 0); //关闭 平滑效果 } } }