using System;using System.Drawing;using System.Drawing.Imaging;using System.Runtime.InteropServices;namespace ConsoleApplication1{ class ImageUtil { public static void ClearBlank(string sourceFileName,out int x,out int y,out int w,out int h) { using (var image = new Bitmap(sourceFileName)) { //获取图像的BitmapData对像 var data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); //循环处理 var minW = 1000000; var maxW = 0; var minH = 1000000; var maxH = 0; unsafe { byte* ptr = (byte*)(data.Scan0.ToPointer()); for (var i = 0; i < data.Height; i++) { for (var j = 0; j < data.Width; j++) { var a = Convert.ToInt32(ptr[0].ToString()); var b = Convert.ToInt32(ptr[1].ToString()); var c = Convert.ToInt32(ptr[2].ToString()); if (a < 150 && b < 150 && c < 150) { if ((j + 1) > maxW) { maxW = j + 1; } if ((i + 1) > maxH) { maxH = i + 1; } if ((j + 1) < minW) { minW = j + 1; } if ((i + 1) < minH) { minH = i + 1; } } ptr += 3; } ptr += data.Stride - data.Width * 3; } } x = minW; y = minH; w = maxW - minW; h = maxH - minH; } } }}