Fork me on GitHub

C#图像处理

[原创]C#图像处理

亮度设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public Bitmap SetBrightness(Bitmap image, int brightness)
{
Bitmap temp = image;
Bitmap bmap = (Bitmap)temp.Clone();
if (brightness < -255) brightness = -255;
if (brightness > 255) brightness = 255;
Color c;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
c = bmap.GetPixel(i, j);
int cR = c.R + brightness;
int cG = c.G + brightness;
int cB = c.B + brightness;

if (cR < 0) cR = 1;
if (cR > 255) cR = 255;

if (cG < 0) cG = 1;
if (cG > 255) cG = 255;

if (cB < 0) cB = 1;
if (cB > 255) cB = 255;

bmap.SetPixel(i, j,
Color.FromArgb((byte)cR, (byte)cG, (byte)cB));
}
}
return (Bitmap)bmap.Clone();
}

RGB设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//私有方法
private byte[] CreateGammaArray(double color)
{
byte[] gammaArray = new byte[256];
for (int i = 0; i < 256; ++i)
{
gammaArray[i] = (byte)Math.Min(255,
(int)((255.0 * Math.Pow(i / 255.0, 1.0 / color)) + 0.5));
}
return gammaArray;
}
//设置RGB
public Bitmap SetGamma(Bitmap image, double red, double green, double blue)
{
Bitmap temp = image;
Bitmap bmap = (Bitmap)temp.Clone();
Color c;
byte[] redGamma = CreateGammaArray(red);
byte[] greenGamma = CreateGammaArray(green);
byte[] blueGamma = CreateGammaArray(blue);
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
c = bmap.GetPixel(i, j);
bmap.SetPixel(i, j, Color.FromArgb(redGamma[c.R],
greenGamma[c.G], blueGamma[c.B]));
}
}
return (Bitmap)bmap.Clone();
}

对比度设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public Bitmap SetContrast(Bitmap image, double contrast)
{
Bitmap temp = image;
Bitmap bmap = (Bitmap)temp.Clone();
if (contrast < -100) contrast = -100;
if (contrast > 100) contrast = 100;
contrast = (100.0 + contrast) / 100.0;
contrast *= contrast;
Color c;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
c = bmap.GetPixel(i, j);
double pR = c.R / 255.0;
pR -= 0.5;
pR *= contrast;
pR += 0.5;
pR *= 255;
if (pR < 0) pR = 0;
if (pR > 255) pR = 255;

double pG = c.G / 255.0;
pG -= 0.5;
pG *= contrast;
pG += 0.5;
pG *= 255;
if (pG < 0) pG = 0;
if (pG > 255) pG = 255;

double pB = c.B / 255.0;
pB -= 0.5;
pB *= contrast;
pB += 0.5;
pB *= 255;
if (pB < 0) pB = 0;
if (pB > 255) pB = 255;

bmap.SetPixel(i, j,
Color.FromArgb((byte)pR, (byte)pG, (byte)pB));
}
}
return (Bitmap)bmap.Clone();
}

转换成黑白

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public Bitmap SetGrayscale(Bitmap image)
{
Bitmap temp = image;
Bitmap bmap = (Bitmap)temp.Clone();
Color c;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
c = bmap.GetPixel(i, j);
byte gray = (byte)(.299 * c.R + .587 * c.G + .114 * c.B);

bmap.SetPixel(i, j, Color.FromArgb(gray, gray, gray));
}
}
return (Bitmap)bmap.Clone();
}

图像倒置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  public Bitmap SetInvert(Bitmap image)
{
Bitmap temp = image;
Bitmap bmap = (Bitmap)temp.Clone();
Color c;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
c = bmap.GetPixel(i, j);
bmap.SetPixel(i, j,
Color.FromArgb(255 - c.R, 255 - c.G, 255 - c.B));
}
}
return (Bitmap)bmap.Clone();
}

重新设定图像大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
public Bitmap Resize(Bitmap image, int newWidth, int newHeight)
{
if (newWidth != 0 && newHeight != 0)
{
Bitmap temp = image;
Bitmap bmap = new Bitmap(newWidth, newHeight, temp.PixelFormat);

double nWidthFactor = (double)temp.Width / (double)newWidth;
double nHeightFactor = (double)temp.Height / (double)newHeight;

double fx, fy, nx, ny;
int cx, cy, fr_x, fr_y;
Color color1 = new Color();
Color color2 = new Color();
Color color3 = new Color();
Color color4 = new Color();
byte nRed, nGreen, nBlue;

byte bp1, bp2;

for (int x = 0; x < bmap.Width; ++x)
{
for (int y = 0; y < bmap.Height; ++y)
{

fr_x = (int)Math.Floor(x * nWidthFactor);
fr_y = (int)Math.Floor(y * nHeightFactor);
cx = fr_x + 1;
if (cx >= temp.Width) cx = fr_x;
cy = fr_y + 1;
if (cy >= temp.Height) cy = fr_y;
fx = x * nWidthFactor - fr_x;
fy = y * nHeightFactor - fr_y;
nx = 1.0 - fx;
ny = 1.0 - fy;

color1 = temp.GetPixel(fr_x, fr_y);
color2 = temp.GetPixel(cx, fr_y);
color3 = temp.GetPixel(fr_x, cy);
color4 = temp.GetPixel(cx, cy);

// Blue
bp1 = (byte)(nx * color1.B + fx * color2.B);

bp2 = (byte)(nx * color3.B + fx * color4.B);

nBlue = (byte)(ny * (double)(bp1) + fy * (double)(bp2));

// Green
bp1 = (byte)(nx * color1.G + fx * color2.G);

bp2 = (byte)(nx * color3.G + fx * color4.G);

nGreen = (byte)(ny * (double)(bp1) + fy * (double)(bp2));

// Red
bp1 = (byte)(nx * color1.R + fx * color2.R);

bp2 = (byte)(nx * color3.R + fx * color4.R);

nRed = (byte)(ny * (double)(bp1) + fy * (double)(bp2));

bmap.SetPixel(x, y, System.Drawing.Color.FromArgb
(255, nRed, nGreen, nBlue));
}
}
return (Bitmap)bmap.Clone();
}
return null;
}

旋转图像

1
2
3
4
5
6
7
public Bitmap RotateFlip(Bitmap image, RotateFlipType rotateFlipType)
{
Bitmap temp = image;
Bitmap bmap = (Bitmap)temp.Clone();
bmap.RotateFlip(rotateFlipType);
return (Bitmap)bmap.Clone();
}

裁剪图像

1
2
3
4
5
6
7
8
9
10
11
public Bitmap Crop(Bitmap image, int xPosition, int yPosition, int width, int height)
{
Bitmap temp = image;
Bitmap bmap = (Bitmap)temp.Clone();
if (xPosition + width > image.Width)
width = image.Width - xPosition;
if (yPosition + height > image.Height)
height = image.Height - yPosition;
Rectangle rect = new Rectangle(xPosition, yPosition, width, height);
return bmap.Clone(rect, bmap.PixelFormat);
}

画出外裁剪区域

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public Bitmap DrawOutCropArea(Bitmap image, int xPosition, int yPosition, int width, int height)
{
Bitmap bmap = (Bitmap)image.Clone();
Graphics gr = Graphics.FromImage(bmap);
Brush cBrush = new Pen(Color.FromArgb(150, Color.White)).Brush;
Rectangle rect1 = new Rectangle(0, 0, image.Width, yPosition);
Rectangle rect2 = new Rectangle(0, yPosition, xPosition, height);
Rectangle rect3 = new Rectangle
(0, (yPosition + height), image.Width, image.Height);
Rectangle rect4 = new Rectangle((xPosition + width),
yPosition, (image.Width - xPosition - width), height);
gr.FillRectangle(cBrush, rect1);
gr.FillRectangle(cBrush, rect2);
gr.FillRectangle(cBrush, rect3);
gr.FillRectangle(cBrush, rect4);
return (Bitmap)bmap.Clone();
}

添加文字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public Bitmap InsertText(Bitmap image, string text, int xPosition, int yPosition, string fontName, float fontSize, string fontStyle, string colorName1, string colorName2)
{
Bitmap temp = image;
Bitmap bmap = (Bitmap)temp.Clone();
Graphics gr = Graphics.FromImage(bmap);
if (string.IsNullOrEmpty(fontName))
fontName = "Times New Roman";
if (fontSize.Equals(null))
fontSize = 10.0F;
Font font = new Font(fontName, fontSize);
if (!string.IsNullOrEmpty(fontStyle))
{
FontStyle fStyle = FontStyle.Regular;
switch (fontStyle.ToLower())
{
case "bold":
fStyle = FontStyle.Bold;
break;
case "italic":
fStyle = FontStyle.Italic;
break;
case "underline":
fStyle = FontStyle.Underline;
break;
case "strikeout":
fStyle = FontStyle.Strikeout;
break;
}
font = new Font(fontName, fontSize, fStyle);
}
if (string.IsNullOrEmpty(colorName1))
colorName1 = "Black";
if (string.IsNullOrEmpty(colorName2))
colorName2 = colorName1;
Color color1 = Color.FromName(colorName1);
Color color2 = Color.FromName(colorName2);
int gW = (int)(text.Length * fontSize);
gW = gW == 0 ? 10 : gW;
LinearGradientBrush LGBrush =
new LinearGradientBrush(new Rectangle(0, 0, gW, (int)fontSize), color1,
color2, LinearGradientMode.Vertical);
gr.DrawString(text, font, LGBrush, xPosition, yPosition);
return (Bitmap)bmap.Clone();
}

添加图像

1
2
3
4
5
6
7
8
9
10
11
12
public Bitmap InsertImage(Bitmap image, Bitmap image2, string imagePath, int xPosition, int yPosition)
{
Bitmap temp = image;
Bitmap bmap = (Bitmap)temp.Clone();
Graphics gr = Graphics.FromImage(bmap);
if (!string.IsNullOrEmpty(imagePath))
{
Rectangle rect = new Rectangle(xPosition, yPosition, image2.Width, image2.Height);
gr.DrawImage(Bitmap.FromFile(imagePath), rect);
}
return (Bitmap)bmap.Clone();
}

添加形状

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public Bitmap InsertShape(Bitmap image, string shapeType, int xPosition, int yPosition, int width, int height, string colorName)
{
Bitmap temp = image;
Bitmap bmap = (Bitmap)temp.Clone();
Graphics gr = Graphics.FromImage(bmap);
if (string.IsNullOrEmpty(colorName))
colorName = "Black";
Pen pen = new Pen(Color.FromName(colorName));
switch (shapeType.ToLower())
{
case "filledellipse":
gr.FillEllipse(pen.Brush, xPosition,
yPosition, width, height);
break;
case "filledrectangle":
gr.FillRectangle(pen.Brush, xPosition,
yPosition, width, height);
break;
case "ellipse":
gr.DrawEllipse(pen, xPosition, yPosition, width, height);
break;
case "rectangle":
default:
gr.DrawRectangle(pen, xPosition, yPosition, width, height);
break;

}
return (Bitmap)bmap.Clone();
}

彩色滤镜

1
2
3
4
5
6
7
8
9
10
11
12
13
public Bitmap SetBitmapColorFilter(Bitmap image, Color Filter)
{
Bitmap bmp = image;
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
Color color = bmp.GetPixel(x, y);
bmp.SetPixel(x, y, Filter);
}
}
return bmp;
}

底片效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public Bitmap SetFilmEffect(Bitmap image)
{
int Height = image.Height;
int Width = image.Width;
Bitmap newbitmap = new Bitmap(Width, Height);
Bitmap oldbitmap = image;
Color pixel;
for (int x = 1; x < Width; x++)
{
for (int y = 1; y < Height; y++)
{
int r, g, b;
pixel = oldbitmap.GetPixel(x, y);
r = 255 - pixel.R;
g = 255 - pixel.G;
b = 255 - pixel.B;
newbitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
}
}
return newbitmap;
}

浮雕效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public Bitmap SetEmbossEffect(Bitmap image)
{
int Height = image.Height;
int Width = image.Width;
Bitmap newBitmap = new Bitmap(Width, Height);
Bitmap oldBitmap = image;
Color pixel1, pixel2;
for (int x = 0; x < Width - 1; x++)
{
for (int y = 0; y < Height - 1; y++)
{
int r = 0, g = 0, b = 0;
pixel1 = oldBitmap.GetPixel(x, y);
pixel2 = oldBitmap.GetPixel(x + 1, y + 1);
r = Math.Abs(pixel1.R - pixel2.R + 128);
g = Math.Abs(pixel1.G - pixel2.G + 128);
b = Math.Abs(pixel1.B - pixel2.B + 128);
if (r > 255)
r = 255;
if (r < 0)
r = 0;
if (g > 255)
g = 255;
if (g < 0)
g = 0;
if (b > 255)
b = 255;
if (b < 0)
b = 0;
newBitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
}
}
return newBitmap;
}

转圆形图像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public Bitmap CutEllipse(Bitmap image, Rectangle rec, Size size)
{
Bitmap bitmap = new Bitmap(size.Width, size.Height);
using (Graphics g = Graphics.FromImage(bitmap))
{
using (TextureBrush br = new TextureBrush(image, WrapMode.Clamp, rec))
{
br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.FillEllipse(br, new Rectangle(Point.Empty, size));
}
}
return bitmap;
}

添加椒盐噪声

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//Pa=Pb0.2
public Bitmap SetSaltAndPepperNoise(Bitmap image, double Pa, double Pb)
{
Bitmap pic = new Bitmap(image, image.Width, image.Height);
double P = Pb / (1 - Pa);//程序要,为了得到一个概率Pb事件
int width = pic.Width;
int height = pic.Height;
Random rand = new Random();
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int gray;
int noise = 1;
double probility = rand.NextDouble();
if (probility < Pa)
{
noise = 255;//有Pa概率 噪声设为最大值
}
else
{
double temp = rand.NextDouble();
if (temp < P)//有1 - Pa的几率到达这里,再乘以 P ,刚好等于Pb
noise = 0;
}
if (noise != 1)
{
gray = noise;
}
else gray = pic.GetPixel(j, i).R;
Color color = Color.FromArgb(gray, gray, gray);
pic.SetPixel(j, i, color);
}
}
return pic;
}

马赛克

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public Bitmap SetMosaic(Bitmap image, int RIDIO = 50)
{
Bitmap newbitmap = image;
for (int h = 0; h < newbitmap.Height; h += RIDIO)
{
for (int w = 0; w < newbitmap.Width; w += RIDIO)
{
int avgRed = 0, avgGreen = 0, avgBlue = 0;
int count = 0;
//取周围的像素
for (int x = w; (x < w + RIDIO && x < newbitmap.Width); x++)
{
for (int y = h; (y < h + RIDIO && y < newbitmap.Height); y++)
{
Color pixel = newbitmap.GetPixel(x, y);
avgRed += pixel.R;
avgGreen += pixel.G;
avgBlue += pixel.B;
count++;
}
}

//取平均值
avgRed = avgRed / count;
avgBlue = avgBlue / count;
avgGreen = avgGreen / count;

//设置颜色
for (int x = w; (x < w + RIDIO && x < newbitmap.Width); x++)
{
for (int y = h; (y < h + RIDIO && y < newbitmap.Height); y++)
{
Color newColor = Color.FromArgb(avgRed, avgGreen, avgBlue);
newbitmap.SetPixel(x, y, newColor);
}
}
}
}
return (Bitmap)newbitmap.Clone();
}

添加暗角

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public Bitmap SetVignetting(Bitmap image)
{
Bitmap newbitmap = image;
int width = newbitmap.Width;
int height = newbitmap.Height;
float cx = width / 2;
float cy = height / 2;
float maxDist = cx * cx + cy * cy;
float currDist = 0, factor;
Color pixel;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
currDist = ((float)i - cx) * ((float)i - cx) + ((float)j - cy) * ((float)j - cy);
factor = currDist / maxDist;

pixel = newbitmap.GetPixel(i, j);
int red = (int)(pixel.R * (1 - factor));
int green = (int)(pixel.G * (1 - factor));
int blue = (int)(pixel.B * (1 - factor));
newbitmap.SetPixel(i, j, Color.FromArgb(red, green, blue));
}
}
return (Bitmap)newbitmap.Clone();
}