public Bitmap Convolute(Bitmap srcImage)
{
if (scale == 0) scale = 1;
int width = srcImage.Width;
int height = srcImage.Height;
Bitmap dstImage = (Bitmap)srcImage.Clone();
BitmapData srcData = srcImage.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData dstData = dstImage.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
int rectTop = 1;
int rectBottom = height - 1;
int rectLeft = 1;
int rectRight = width - 1;
unsafe
{
byte* src = (byte*)srcData.Scan0;
byte* dst = (byte*)dstData.Scan0;
int stride = srcData.Stride;
int offset = stride - width * BPP;
int pixel = 0;
src += stride;
dst += stride;
for (int y = rectTop; y < rectBottom; y++)
{
src += BPP;
dst += BPP;
for (int x = rectLeft; x < rectRight; x++)
{
if (src[3] > 0)
{
for (int i = 0; i < 3; i++)
{
pixel =src[i - stride - BPP] * topLeft +
src[i - stride] * topMid +
src[i - stride + BPP] * topRight +
src[i - BPP] * midLeft +
src[i] * center +
src[i + BPP] * midRight +
src[i + stride - BPP] * bottomLeft +
src[i + stride] * bottomMid +
src[i + stride + BPP] * bottomRight;
pixel = pixel / scale + kernelOffset;
if (pixel < 0) pixel = 0;
if (pixel > 255) pixel = 255;
dst[i] = (byte)pixel;
}
}
src += BPP;
dst += BPP;
}
src += (offset + BPP);
dst += (offset + BPP);
} }
srcImage.UnlockBits(srcData);
dstImage.UnlockBits(dstData);
srcImage.Dispose();
return dstImage; }
{
if (scale == 0) scale = 1;
int width = srcImage.Width;
int height = srcImage.Height;
Bitmap dstImage = (Bitmap)srcImage.Clone();
BitmapData srcData = srcImage.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData dstData = dstImage.LockBits(new Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
int rectTop = 1;
int rectBottom = height - 1;
int rectLeft = 1;
int rectRight = width - 1;
unsafe
{
byte* src = (byte*)srcData.Scan0;
byte* dst = (byte*)dstData.Scan0;
int stride = srcData.Stride;
int offset = stride - width * BPP;
int pixel = 0;
src += stride;
dst += stride;
for (int y = rectTop; y < rectBottom; y++)
{
src += BPP;
dst += BPP;
for (int x = rectLeft; x < rectRight; x++)
{
if (src[3] > 0)
{
for (int i = 0; i < 3; i++)
{
pixel =src[i - stride - BPP] * topLeft +
src[i - stride] * topMid +
src[i - stride + BPP] * topRight +
src[i - BPP] * midLeft +
src[i] * center +
src[i + BPP] * midRight +
src[i + stride - BPP] * bottomLeft +
src[i + stride] * bottomMid +
src[i + stride + BPP] * bottomRight;
pixel = pixel / scale + kernelOffset;
if (pixel < 0) pixel = 0;
if (pixel > 255) pixel = 255;
dst[i] = (byte)pixel;
}
}
src += BPP;
dst += BPP;
}
src += (offset + BPP);
dst += (offset + BPP);
} }
srcImage.UnlockBits(srcData);
dstImage.UnlockBits(dstData);
srcImage.Dispose();
return dstImage; }
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货