/// <summary>
/// 柔化实现方法
/// </summary>
void Image_Soften()
{
if (pbshowbox.Image != null)
{
int Height = this.pbshowbox.Image.Height;
int Width = this.pbshowbox.Image.Width;
Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format32bppRgb);
Bitmap MyBitmap = (Bitmap)this.pbshowbox.Image;
BitmapData oldData = MyBitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb);
BitmapData newData = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
unsafe
{
byte* pin = (byte*)(oldData.Scan0.ToPointer());
byte* pout = (byte*)(newData.Scan0.ToPointer());
//高斯模板
int[] Gauss = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };
for (int i = 1; i < Width - 1; i++)
{
for (int j = 1; j < Height - 1; j++)
{
int r = 0, g = 0, b = 0;
int Index = 0;
for (int col = -1; col <= 1; col++)
{
for (int row = -1; row <= 1; row++)
{
int off = ((j + row) * (Width) + (i + col)) * 4;
r += pin[off + 0] * Gauss[Index];
g += pin[off + 1] * Gauss[Index];
b += pin[off + 2] * Gauss[Index];
Index++;
}
}
r /= 16;
g /= 16;
b /= 16;
//处理颜色值溢出
if (r < 0) r = 0;
if (r > 255) r = 255;
if (g < 0) g = 0;
if (g > 255) g = 255;
if (b < 0) b = 0;
if (b > 255) b = 255;
int off2 = (j * Width + i) * 4;
pout[off2 + 0] = (byte)r;
pout[off2 + 1] = (byte)g;
pout[off2 + 2] = (byte)b;
}
}
bitmap.UnlockBits(newData);
MyBitmap.UnlockBits(oldData);
this.pbshowbox.Image = bitmap;
}