C#图像处理(收集处理方法)
// 柔化效果
public Bitmap SmoothEffect(Bitmap bmpSource)
{
Color c = new Color();
Color c1 = new Color();
Bitmap bmp1 = new Bitmap(bmpSource);
Bitmap bmp2 = new Bitmap(bmpSource);
int i, j, xres, yres, rx, gx, bx;
xres = bmpSource.Width;
yres = bmpSource.Height;
for (i = 1; i < xres - 1; i++)//边界像素无法平均
{
for (j = 1; j < yres - 1; j++)
{
//3*3像素平均
rx = gx = bx = 0;
for (int k1 = -1; k1 <= 1; k1++)
{
for (int k2 = -1; k2 <= 1; k2++)
{
c = bmp1.GetPixel(i + k1, j + k2);
rx += c.R;
gx += c.G;
bx += c.B;
}
}
rx /= 9;
gx /= 9;
bx /= 9;
if (rx < 0) rx = 0;
if (rx > 255) rx = 255;
if (gx < 0) gx = 0;
if (gx > 255) gx = 255;
if (bx < 0) bx = 0;
if (bx > 255) bx = 255;
c1 = Color.FromArgb(rx, gx, bx);
bmp2.SetPixel(i, j, c1);
}
}
return bmp2;
}
//单色处理
public Bitmap ColorOnly(int colorID, Bitmap bmpSource)
{
Color c = new Color();
Color c1 = new Color();
Bitmap bmp1 = new Bitmap(bmpSource);
Bitmap bmp2 = new Bitmap(bmpSource);
int i, j, xres, yres;
xres = bmpSource.Width;
yres = bmpSource.Height;
for (i = 0; i <= xres - 1; i++)
{
for (j = 0; j <= yres - 1; j++)
{
c = bmp1.GetPixel(i, j);
switch (colorID)
{
case 1://R单色
c1 = Color.FromArgb(c.R, c.R, c.R);
break;
case 2://G单色
c1 = Color.FromArgb(c.G, c.G, c.G);
break;
case 3: //B单色
c1 = Color.FromArgb(c.B, c.B, c.B);
break;
case -1://反色处理(底片效果显示图像)
c1 = Color.FromArgb(255 - c.R, 255 - c.G, 255 - c.B);
break;
default:
break;
}
bmp2.SetPixel(i, j, c1);
}
}
return bmp2;
}
//霓虹效果
public Bitmap NeonEffect(Bitmap bmpSource)
{
Color c = new Color();
Color c1 = new Color();
Color c2 = new Color();
Bitmap bmp1 = new Bitmap(bmpSource);
Bitmap bmp2 = new Bitmap(bmpSource);
int i, j, xres, yres, rx, gx, bx;
xres = bmpSource.Width;
yres = bmpSource.Height;
for (i = 0; i <= xres - 1; i++)
{
for (j = 0; j <= yres - 1; j++)
{
rx = gx = bx = 0;
c = bmp1.GetPixel(i, j);
if (i + 1 <= xres - 1 && j + 1 <= yres - 1)
{
c1 = bmp1.GetPixel(i + 1, j);
c2 = bmp1.GetPixel(i, j + 1);
//以下计算梯度
rx = (int)(2 * Math.Sqrt((c.R - c1.R) * (c.R - c1.R) + (c.R - c2.R) * (c.R - c2.R)));
gx = (int)(2 * Math.Sqrt((c.G - c1.G) * (c.G - c1.G) + (c.G - c2.G) * (c.G - c2.G)));
bx = (int)(2 * Math.Sqrt((c.B - c1.B) * (c.B - c1.B) + (c.B - c2.B) * (c.B - c2.B)));
if (rx < 0) rx = 0;
if (rx > 255) rx = 255;
if (gx < 0) gx = 0;
if (gx > 255) gx = 255;
if (bx < 0) bx = 0;
if (bx > 255) bx = 255;
c1 = Color.FromArgb(rx, gx, bx);
}
else
c1 = c;
bmp2.SetPixel(i, j, c1);
}
}
return bmp2;
}
//锐化效果
public Bitmap SharpEffect(Bitmap bmpSource)
{
Color c = new Color();
Color c1 = new Color();
Bitmap bmp1 = new Bitmap(bmpSource);
Bitmap bmp2 = new Bitmap(bmpSource);
int i, j, xres, yres, rx, gx, bx;
xres = bmpSource.Width;
yres = bmpSource.Height;
for (i = 0; i <= xres - 1; i++)
{
for (j = 0; j <= yres - 1; j++)
{
rx = gx = bx = 0;
c = bmp1.GetPixel(i, j);
if (i - 1 >= 0 && j - 1 >= 0)
{
c1 = bmp1.GetPixel(i - 1, j - 1);
//以下计算新值
rx = c.R + Math.Abs(c.R - c1.R) / 4;
gx = c.G + Math.Abs(c.G - c1.G) / 4;
bx = c.B + Math.Abs(c.B - c1.B) / 4;
if (rx < 0) rx = 0;
if (rx > 255) rx = 255;
if (gx < 0) gx = 0;
if (gx > 255) gx = 255;
if (bx < 0) bx = 0;
if (bx > 255) bx = 255;
c1 = Color.FromArgb(rx, gx, bx);
}
else
c1 = c;
bmp2.SetPixel(i, j, c1);
}
}
return bmp2;
}
//浮雕效果
public Bitmap SculptureEffect(Bitmap bmpSource)
{
Color c = new Color();
Color c1 = new Color();
Bitmap bmp1 = new Bitmap(bmpSource);
Bitmap bmp2 = new Bitmap(bmpSource);
int i, j, xres, yres, rx, gx, bx;
xres = bmpSource.Width;
yres = bmpSource.Height;
for (i = 0; i <= xres - 1; i++)
{
for (j = 0; j <= yres - 1; j++)
{
rx = gx = bx = 0;
c = bmp1.GetPixel(i, j);
if (i - 1 >= 0)
{
c1 = bmp1.GetPixel(i - 1, j);
//以下计算新值
rx = c1.R - c.R + 128;
gx = c1.G - c.G + 128;
bx = c1.B - c.B + 128;
if (rx < 0) rx = 0;
if (rx > 255) rx = 255;
if (gx < 0) gx = 0;
if (gx > 255) gx = 255;
if (bx < 0) bx = 0;
if (bx > 255) bx = 255;
c1 = Color.FromArgb(rx, gx, bx);
}
else
c1 = c;
bmp2.SetPixel(i, j, c1);
}
}
return bmp2;
}
//马赛克效果
public Bitmap InlayEffect(Bitmap bmpSource)
{
Color c = new Color();
Color c1 = new Color();
Bitmap bmp1 = new Bitmap(bmpSource);
Bitmap bmp2 = new Bitmap(bmpSource);
int i, j, xres, yres, rx, gx, bx;
xres = bmpSource.Width;
yres = bmpSource.Height;
//5*5处理
for (i = 0; i <= xres - 1; i += 5)
{
for (j = 0; j <= yres - 1; j += 5)
{
rx = gx = bx = 0;
int count = 0;
for (int k1 = 0; k1 <= 4; k1++)
{
for (int k2 = 0; k2 <= 4; k2++)
{
if (i + k1 <= xres - 1 && j + k2 <= yres - 1)
{
c = bmp1.GetPixel(i + k1, j + k2);
rx += c.R;
gx += c.G;
bx += c.B;
count++;
}
}
}
rx = (int)(rx / count);
gx = (int)(gx / count);
bx = (int)(bx / count);
if (rx < 0) rx = 0;
if (rx > 255) rx = 255;
if (gx < 0) gx = 0;
if (gx > 255) gx = 255;
if (bx < 0) bx = 0;
if (bx > 255) bx = 255;
for (int k1 = 0; k1 <= 4; k1++)
{
for (int k2 = 0; k2 <= 4; k2++)
{
if (i + k1 <= xres - 1 && j + k2 <= yres - 1)
{
c1 = Color.FromArgb(rx, gx, bx);
bmp2.SetPixel(i + k1, j + k2, c1);
}
}
}
}
}
return bmp2;
}
//灰度处理
public Bitmap GreyEffect(Bitmap bmpSource)
{
Color c = new Color();
Color c1 = new Color();
Bitmap bmp1 = new Bitmap(bmpSource);
Bitmap bmp2 = new Bitmap(bmpSource);
int i, j, xres, yres;
xres = bmpSource.Width;
yres = bmpSource.Height;
for (i = 0; i <= xres - 1; i++)
{
for (j = 0; j <= yres - 1; j++)
{
c = bmp1.GetPixel(i, j);
int gray = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
c1 = Color.FromArgb(gray, gray, gray);
bmp2.SetPixel(i, j, c1);
}
}
return bmp2;
}
//透明度变换
public Bitmap OpacityEffect(int opacityValue, Bitmap bmpSource)
{
int value = opacityValue;
if (opacityValue < 0)
{
value = 0;
}
else if (opacityValue > 255)
{
value = 255;
}
Color c = new Color();
Color c1 = new Color();
Bitmap bmp1 = new Bitmap(bmpSource);
Bitmap bmp2 = new Bitmap(bmpSource);
int i, j, xres, yres;
xres = bmpSource.Width;
yres = bmpSource.Height;
for (i = 0; i <= xres - 1; i++)
{
for (j = 0; j <= yres - 1; j++)
{
c = bmp1.GetPixel(i, j);
c1 = Color.FromArgb(value, c.R, c.G, c.B);
bmp2.SetPixel(i, j, c1);
}
}
return bmp2;
}
//旋转
public Bitmap KiRotate(Bitmap bmp, RotateFlipType Rotate)
{
bmp.RotateFlip(Rotate);
return bmp;
}