我正在寻找一个解决方案从c#管理的GDI +库检测c#位图的边缘空白。c#GDI边缘空白检测算法
的图像将是要么透明或白色,大部分400X的图片是与8000x8000px周围的边缘大约2000像素的空格。
什么是寻找边缘的最有效方式,x,y,高度和宽度坐标?我尝试了像素逐像素,但发现它非常缓慢。
更新到溶液 --Added左/右/顶/底边界
与图像细节中心图像有关的问题,现在作物任何透明(0%)或白色(#FFFFFF)像素。
var top = bitmap.Height;
var left = bitmap.Width;
var right = 0;
var bottom = 0;
...
var pData = pData0 + (y * data.Stride) + (x * 4);
var xyAlpha = pData[3];
var xyBlue = pData[0];
var xyGreen = pData[1];
var xyRed = pData[2];
if ((xyAlpha > 0) || (xyRed != 255 && xyGreen != 255 && xyBlue != 255)) {
if (y < top)
top = y;
if (y > bottom)
bottom = y;
if (x < left)
left = x;
if (x > right)
right = x;
}
...
var cropWidth = right - left;
var cropHeight = bottom - top;
var cropX = top;
var cropY = left;
var cacheBitmap = new Bitmap(cropWidth, cropHeight, PixelFormat.Format32bppArgb);
using (var cacheGraphics = Graphics.FromImage(cacheBitmap)) {
cacheGraphics.DrawImage(context.Image, new Rectangle(0, 0, cropWidth, cropHeight), cropX, cropY, cropWidth, cropHeight, GraphicsUnit.Pixel);
}