这段是我的代码 请高手给重点字段的解释(作用,效果) private void btn_convert_Click(object sender, EventArgs e) { Bitmap img1 = (Bitmap)pictureBox1.Image; pictureBox2.Image = GrayByPixels(img1); Double d = 0.8; Bitmap img2 = (Bitmap)pictureBox2.Image; pictureBox3.Image = BitmapToBlack(img2, d); } public Bitmap GrayByPixels(Bitmap bmpcode) { bmpcode = new Bitmap(bmpcode); for (int i = 0; i < bmpcode.Height; i++) { for (int j = 0; j < bmpcode.Width; j++) { int tmpValue = GetGrayNumColor(bmpcode.GetPixel(j, i)); bmpcode.SetPixel(j, i, Color.FromArgb(tmpValue, tmpValue, tmpValue)); } } return bmpcode; } private int GetGrayNumColor(System.Drawing.Color codecolor) { return (codecolor.R * 19595 + codecolor.G * 38469 + codecolor.B * 7472) >> 16; } 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; }