通过GDI+对图片数据进行处理,下面列出各个效果的算法
对于读取图片的每个像素颜色的速度非常慢,这里使用LockBitmap类来对图片像素进行操作,LockBitmap类的定义看这里
http://www.cnblogs.com/bomo/archive/2013/02/26/2934055.html
1、旋转(90度,180度,270度)
1 //旋转90,180,270 2 public Bitmap RotateImage(Bitmap bmp, int angle) 3 { 4 if (angle != 90 && angle != 180 && angle != 270) 5 { 6 return null; 7 } 8 int width = bmp.Width; 9 int height = bmp.Height; 10 11 if (angle == 90) 12 { 13 Bitmap newbmp = new Bitmap(height, width); 14 using (Graphics g = Graphics.FromImage(newbmp)) 15 { 16 Point[] destinationPoints = { 17 new Point(height, 0), // destination for upper-left point of original 18 new Point(height, width),// destination for upper-right point of original 19 new Point(0, 0)}; // destination for lower-left point of original 20 g.DrawImage(bmp, destinationPoints); 21 } 22 return newbmp; 23 } 24 25 if (angle == 180) 26 { 27 Bitmap newbmp = new Bitmap(width, height); 28 using (Graphics g = Graphics.FromImage(newbmp)) 29 { 30 Point[] destinationPoints = { 31 new Point(width, height), // destination for upper-left point of original 32 new Point(0, height),// destination for upper-right point of original 33 new Point(width, 0)}; // destination for lower-left point of original 34 g.DrawImage(bmp, destinationPoints); 35 } 36 return newbmp; 37 } 38 39 if(angle == 270) 40 { 41 Bitmap newbmp = new Bitmap(height, width); 42 using (Graphics g = Graphics.FromImage(newbmp)) 43 { 44 Point[] destinationPoints = { 45 new Point(0, width), // destination for upper-left point of original 46 new Point(0, 0),// destination for upper-right point of original 47 new Point(height, width)}; // destination for lower-left point of original 48 g.DrawImage(bmp, destinationPoints); 49 } 50 return newbmp; 51 } 52 return null; 53 }
2、重设大小
重设大小
3、底片效果(反色)(255-r, 255-g, 255-b)
1 //底片 2 public Bitmap NegativeImage(Bitmap bmp) 3 { 4 int height = bmp.Height; 5 int width = bmp.Width; 6 Bitmap newbmp = new Bitmap(width, height); 7 8 LockBitmap lbmp = new LockBitmap(bmp); 9 LockBitmap newlbmp = new LockBitmap(newbmp); 10 lbmp.LockBits(); 11 newlbmp.LockBits(); 12 13 Color pixel; 14 for (int x = 1; x < width; x++) 15 { 16 for (int y = 1; y < height; y++) 17 { 18 int r, g, b; 19 pixel = lbmp.GetPixel(x, y); 20 r = 255 - pixel.R; 21 g = 255 - pixel.G; 22 b = 255 - pixel.B; 23 newlbmp.SetPixel(x, y, Color.FromArgb(r, g, b)); 24 } 25 } 26 lbmp.UnlockBits(); 27 newlbmp.UnlockBits(); 28 return newbmp; 29 }
4、黑白效果
1 //黑白 2 public Bitmap GrayImage(Bitmap bmp, int type) 3 { 4 int height = bmp.Height; 5 int width = bmp.Width; 6 Bitmap newbmp = new Bitmap(width, height); 7 8 LockBitmap lbmp = new LockBitmap(bmp); 9 LockBitmap newlbmp = new LockBitmap(newbmp); 10 lbmp.LockBits(); 11 newlbmp.LockBits(); 12 13 Color pixel; 14 for (int x = 0; x < width; x++) 15 { 16 for (int y = 0; y < height; y++) 17 { 18 pixel = lbmp.GetPixel(x, y); 19 int r, g, b, Result = 0; 20 r = pixel.R; 21 g = pixel.G; 22 b = pixel.B; 23 switch (type) 24 { 25 case 0://平均值法 26 Result = ((r + g + b) / 3); 27 break; 28 case 1://最大值法 29 Result = r > g ? r : g; 30 Result = Result > b ? Result : b; 31 break; 32 case 2://加权平均值法 33 Result = ((int)(0.3 * r) + (int)(0.59 * g) + (int)(0.11 * b)); 34 break; 35 } 36 newlbmp.SetPixel(x, y, Color.FromArgb(Result, Result, Result)); 37 } 38 } 39 lbmp.UnlockBits(); 40 newlbmp.UnlockBits(); 41 return newbmp; 42 }
5、浮雕:找出附近的像素点r1,abs(r-r2+128)
1 //浮雕 2 public Bitmap EmbossmentImage(Bitmap bmp) 3 { 4 int height = bmp.Height; 5 int width = bmp.Width; 6 Bitmap newbmp = new Bitmap(width, height); 7 8 LockBitmap lbmp = new LockBitmap(bmp); 9 LockBitmap newlbmp = new LockBitmap(newbmp); 10 lbmp.LockBits(); 11 newlbmp.LockBits(); 12 13 Color pixel1, pixel2; 14 for (int x = 0; x < width - 1; x++) 15 { 16 for (int y = 0; y < height - 1; y++) 17 { 18 int r = 0, g = 0, b = 0; 19 pixel1 = lbmp.GetPixel(x, y); 20 pixel2 = lbmp.GetPixel(x + 1, y + 1); 21 r = Math.Abs(pixel1.R - pixel2.R + 128); 22 g = Math.Abs(pixel1.G - pixel2.G + 128); 23 b = Math.Abs(pixel1.B - pixel2.B + 128); 24 if (r > 255) 25 r = 255; 26 if (r < 0) 27 r = 0; 28 if (g > 255) 29 g = 255; 30 if (g < 0) 31 g = 0; 32 if (b > 255) 33 b = 255; 34 if (b < 0) 35 b = 0; 36 newlbmp.SetPixel(x, y, Color.FromArgb(r, g, b)); 37 } 38 } 39 lbmp.UnlockBits(); 40 newlbmp.UnlockBits(); 41 return newbmp; 42 }
6、柔化
1 //柔化 2 public Bitmap SoftenImage(Bitmap bmp) 3 { 4 int height = bmp.Height; 5 int width = bmp.Width; 6 Bitmap newbmp = new Bitmap(width, height); 7 8 LockBitmap lbmp = new LockBitmap(bmp); 9 LockBitmap newlbmp = new LockBitmap(newbmp); 10 lbmp.LockBits(); 11 newlbmp.LockBits(); 12 13 Color pixel; 14 //高斯模板 15 int[] Gauss ={ 1, 2, 1, 2, 4, 2, 1, 2, 1 }; 16 for (int x = 1; x < width - 1; x++) 17 { 18 for (int y = 1; y < height - 1; y++) 19 { 20 int r = 0, g = 0, b = 0; 21 int Index = 0; 22 for (int col = -1; col <= 1; col++) 23 { 24 for (int row = -1; row <= 1; row++) 25 { 26 pixel = lbmp.GetPixel(x + row, y + col); 27 r += pixel.R * Gauss[Index]; 28 g += pixel.G * Gauss[Index]; 29 b += pixel.B * Gauss[Index]; 30 Index++; 31 } 32 } 33 r /= 16; 34 g /= 16; 35 b /= 16; 36 //处理颜色值溢出 37 r = r > 255 ? 255 : r; 38 r = r < 0 ? 0 : r; 39 g = g > 255 ? 255 : g; 40 g = g < 0 ? 0 : g; 41 b = b > 255 ? 255 : b; 42 b = b < 0 ? 0 : b; 43 newlbmp.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b)); 44 } 45 } 46 lbmp.UnlockBits(); 47 newlbmp.UnlockBits(); 48 return newbmp; 49 }
7、锐化
1 //锐化 2 public Bitmap SharpenImage(Bitmap bmp) 3 { 4 int height = bmp.Height; 5 int width = bmp.Width; 6 Bitmap newbmp = new Bitmap(width, height); 7 8 LockBitmap lbmp = new LockBitmap(bmp); 9 LockBitmap newlbmp = new LockBitmap(newbmp); 10 lbmp.LockBits(); 11 newlbmp.LockBits(); 12 13 Color pixel; 14 //拉普拉斯模板 15 int[] Laplacian = { -1, -1, -1, -1, 9, -1, -1, -1, -1 }; 16 for (int x = 1; x < width - 1; x++) 17 { 18 for (int y = 1; y < height - 1; y++) 19 { 20 int r = 0, g = 0, b = 0; 21 int Index = 0; 22 for (int col = -1; col <= 1; col++) 23 { 24 for (int row = -1; row <= 1; row++) 25 { 26 pixel = lbmp.GetPixel(x + row, y + col); r += pixel.R * Laplacian[Index]; 27 g += pixel.G * Laplacian[Index]; 28 b += pixel.B * Laplacian[Index]; 29 Index++; 30 } 31 } 32 //处理颜色值溢出 33 r = r > 255 ? 255 : r; 34 r = r < 0 ? 0 : r; 35 g = g > 255 ? 255 : g; 36 g = g < 0 ? 0 : g; 37 b = b > 255 ? 255 : b; 38 b = b < 0 ? 0 : b; 39 newlbmp.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b)); 40 } 41 } 42 lbmp.UnlockBits(); 43 newlbmp.UnlockBits(); 44 return newbmp; 45 }
8、雾化
1 //雾化 2 public Bitmap AtomizationImage(Bitmap bmp) 3 { 4 int height = bmp.Height; 5 int width = bmp.Width; 6 Bitmap newbmp = new Bitmap(width, height); 7 8 LockBitmap lbmp = new LockBitmap(bmp); 9 LockBitmap newlbmp = new LockBitmap(newbmp); 10 lbmp.LockBits(); 11 newlbmp.LockBits(); 12 13 System.Random MyRandom = new Random(); 14 Color pixel; 15 for (int x = 1; x < width - 1; x++) 16 { 17 for (int y = 1; y < height - 1; y++) 18 { 19 int k = MyRandom.Next(123456); 20 //像素块大小 21 int dx = x + k % 19; 22 int dy = y + k % 19; 23 if (dx >= width) 24 dx = width - 1; 25 if (dy >= height) 26 dy = height - 1; 27 pixel = lbmp.GetPixel(dx, dy); 28 newlbmp.SetPixel(x, y, pixel); 29 } 30 } 31 lbmp.UnlockBits(); 32 newlbmp.UnlockBits(); 33 return newbmp; 34 }
转载来源:https://www.cnblogs.com/bomo/archive/2013/03/01/2939453.html