C# 制作超酷图像效果

如果您觉得C#制作的艺术字比较好玩, 但是还觉得没看够,不过瘾,那么我今天就让您一饱眼福, 看看C#如何制作的效果超酷的图像.
(注: 我之前曾写过类似的文章, 但没有原理说明, 代码注释不够详细, 也没有附相应的 Demo...因此如果您觉得好像哪看过类似的文章可以看看我之前写的...)

为了演示后面的效果, 这里有必要先让大家看看今天的原始图片: ISINBAEVA ~~~~~~~~
附件: 2008092313094598.jpg

一. 底片效果
原理: GetPixel方法获得每一点像素的值, 然后再使用SetPixel方法将取反后的颜色值设置到对应的点.
效果图:
附件: 2008092313082497.jpg
代码实现:
底片效果
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //以底片效果显示图像
  4. try
  5. {
  6. int Height = this.pictureBox1.Image.Height;
  7. int Width = this.pictureBox1.Image.Width;
  8. Bitmap newbitmap = new Bitmap(Width, Height);
  9. Bitmap oldbitmap = (Bitmap)this.pictureBox1.Image;
  10. Color pixel;
  11. for (int x = 1; x < Width; x++)
  12. {
  13. for (int y = 1; y < Height; y++)
  14. {
  15. int r, g, b;
  16. pixel = oldbitmap.GetPixel(x, y);
  17. r = 255 - pixel.R;
  18. g = 255 - pixel.G;
  19. b = 255 - pixel.B;
  20. newbitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
  21. }
  22. }
  23. this.pictureBox1.Image = newbitmap;
  24. }
  25. catch (Exception ex)
  26. {
  27. MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  28. }
  29. }
复制代码
二. 浮雕效果
原理: 对图像像素点的像素值分别与相邻像素点的像素值相减后加上128, 然后将其作为新的像素点的值.
效果图:
附件: 2008092313164752.jpg
代码实现:
浮雕效果
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //以浮雕效果显示图像
  4. try
  5. {
  6. int Height = this.pictureBox1.Image.Height;
  7. int Width = this.pictureBox1.Image.Width;
  8. Bitmap newBitmap = new Bitmap(Width, Height);
  9. Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image;
  10. Color pixel1, pixel2;
  11. for (int x = 0; x < Width - 1; x++)
  12. {
  13. for (int y = 0; y < Height - 1; y++)
  14. {
  15. int r = 0, g = 0, b = 0;
  16. pixel1 = oldBitmap.GetPixel(x, y);
  17. pixel2 = oldBitmap.GetPixel(x + 1, y + 1);
  18. r = Math.Abs(pixel1.R - pixel2.R + 128);
  19. g = Math.Abs(pixel1.G - pixel2.G + 128);
  20. b = Math.Abs(pixel1.B - pixel2.B + 128);
  21. if (r > 255)
  22. r = 255;
  23. if (r < 0)
  24. r = 0;
  25. if (g > 255)
  26. g = 255;
  27. if (g < 0)
  28. g = 0;
  29. if (b > 255)
  30. b = 255;
  31. if (b < 0)
  32. b = 0;
  33. newBitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
  34. }
  35. }
  36. this.pictureBox1.Image = newBitmap;
  37. }
  38. catch (Exception ex)
  39. {
  40. MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  41. }
  42. }
复制代码
三. 黑白效果
原理: 彩色图像处理成黑白效果通常有3种算法;
(1).最大值法: 使每个像素点的 R, G, B 值等于原像素点的 RGB (颜色值) 中最大的一个;
(2).平均值法: 使用每个像素点的 R,G,B值等于原像素点的RGB值的平均值;
(3).加权平均值法: 对每个像素点的 R, G, B值进行加权
    ---自认为第三种方法做出来的黑白效果图像最 "真实".
效果图:
附件: 2008092313241741.jpg
代码实现:
黑白效果
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //以黑白效果显示图像
  4. try
  5. {
  6. int Height = this.pictureBox1.Image.Height;
  7. int Width = this.pictureBox1.Image.Width;
  8. Bitmap newBitmap = new Bitmap(Width, Height);
  9. Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image;
  10. Color pixel;
  11. for (int x = 0; x < Width; x++)
  12. for (int y = 0; y < Height; y++)
  13. {
  14. pixel = oldBitmap.GetPixel(x, y);
  15. int r, g, b, Result = 0;
  16. r = pixel.R;
  17. g = pixel.G;
  18. b = pixel.B;
  19. //实例程序以加权平均值法产生黑白图像
  20. int iType =2;
  21. switch (iType)
  22. {
  23. case 0://平均值法
  24. Result = ((r + g + b) / 3);
  25. break;
  26. case 1://最大值法
  27. Result = r > g ? r : g;
  28. Result = Result > b ? Result : b;
  29. break;
  30. case 2://加权平均值法
  31. Result = ((int)(0.7 * r) + (int)(0.2 * g) + (int)(0.1 * b));
  32. break;
  33. }
  34. newBitmap.SetPixel(x, y, Color.FromArgb(Result, Result, Result));
  35. }
  36. this.pictureBox1.Image = newBitmap;
  37. }
  38. catch (Exception ex)
  39. {
  40. MessageBox.Show(ex.Message, "信息提示");
  41. }
  42. }
复制代码
四. 柔化效果
原理: 当前像素点与周围像素点的颜色差距较大时取其平均值.
效果图:
附件: 2008092313290866.jpg
代码实现:
柔化效果
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //以柔化效果显示图像
  4. try
  5. {
  6. int Height = this.pictureBox1.Image.Height;
  7. int Width = this.pictureBox1.Image.Width;
  8. Bitmap bitmap = new Bitmap(Width, Height);
  9. Bitmap MyBitmap = (Bitmap)this.pictureBox1.Image;
  10. Color pixel;
  11. //高斯模板
  12. int[] Gauss ={ 1, 2, 1, 2, 4, 2, 1, 2, 1 };
  13. for (int x = 1; x < Width - 1; x++)
  14. for (int y = 1; y < Height - 1; y++)
  15. {
  16. int r = 0, g = 0, b = 0;
  17. int Index = 0;
  18. for (int col = -1; col <= 1; col++)
  19. for (int row = -1; row <= 1; row++)
  20. {
  21. pixel = MyBitmap.GetPixel(x + row, y + col);
  22. r += pixel.R * Gauss[Index];
  23. g += pixel.G * Gauss[Index];
  24. b += pixel.B * Gauss[Index];
  25. Index++;
  26. }
  27. r /= 16;
  28. g /= 16;
  29. b /= 16;
  30. //处理颜色值溢出
  31. r = r > 255 ? 255 : r;
  32. r = r < 0 ? 0 : r;
  33. g = g > 255 ? 255 : g;
  34. g = g < 0 ? 0 : g;
  35. b = b > 255 ? 255 : b;
  36. b = b < 0 ? 0 : b;
  37. bitmap.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));
  38. }
  39. this.pictureBox1.Image = bitmap;
  40. }
  41. catch (Exception ex)
  42. {
  43. MessageBox.Show(ex.Message, "信息提示");
  44. }
  45. }
复制代码
五.锐化效果
原理:突出显示颜色值大(即形成形体边缘)的像素点.
效果图:
附件: 2008092313325828.jpg
实现代码:
锐化效果
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //以锐化效果显示图像
  4. try
  5. {
  6. int Height = this.pictureBox1.Image.Height;
  7. int Width = this.pictureBox1.Image.Width;
  8. Bitmap newBitmap = new Bitmap(Width, Height);
  9. Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image;
  10. Color pixel;
  11. //拉普拉斯模板
  12. int[] Laplacian ={ -1, -1, -1, -1, 9, -1, -1, -1, -1 };
  13. for (int x = 1; x < Width - 1; x++)
  14. for (int y = 1; y < Height - 1; y++)
  15. {
  16. int r = 0, g = 0, b = 0;
  17. int Index = 0;
  18. for (int col = -1; col <= 1; col++)
  19. for (int row = -1; row <= 1; row++)
  20. {
  21. pixel = oldBitmap.GetPixel(x + row, y + col); r += pixel.R * Laplacian[Index];
  22. g += pixel.G * Laplacian[Index];
  23. b += pixel.B * Laplacian[Index];
  24. Index++;
  25. }
  26. //处理颜色值溢出
  27. r = r > 255 ? 255 : r;
  28. r = r < 0 ? 0 : r;
  29. g = g > 255 ? 255 : g;
  30. g = g < 0 ? 0 : g;
  31. b = b > 255 ? 255 : b;
  32. b = b < 0 ? 0 : b;
  33. newBitmap.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));
  34. }
  35. this.pictureBox1.Image = newBitmap;
  36. }
  37. catch (Exception ex)
  38. {
  39. MessageBox.Show(ex.Message, "信息提示");
  40. }
  41. }
复制代码
六. 雾化效果
原理: 在图像中引入一定的随机值, 打乱图像中的像素值
效果图:
附件: 2008092313371870.jpg
实现代码:
雾化效果
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //以雾化效果显示图像
  4. try
  5. {
  6. int Height = this.pictureBox1.Image.Height;
  7. int Width = this.pictureBox1.Image.Width;
  8. Bitmap newBitmap = new Bitmap(Width, Height);
  9. Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image;
  10. Color pixel;
  11. for (int x = 1; x < Width - 1; x++)
  12. for (int y = 1; y < Height - 1; y++)
  13. {
  14. System.Random MyRandom = new Random();
  15. int k = MyRandom.Next(123456);
  16. //像素块大小
  17. int dx = x + k % 19;
  18. int dy = y + k % 19;
  19. if (dx >= Width)
  20. dx = Width - 1;
  21. if (dy >= Height)
  22. dy = Height - 1;
  23. pixel = oldBitmap.GetPixel(dx, dy);
  24. newBitmap.SetPixel(x, y, pixel);
  25. }
  26. this.pictureBox1.Image = newBitmap;
  27. }
  28. catch (Exception ex)
  29. {
  30. MessageBox.Show(ex.Message, "信息提示");
  31. }
  32. }
复制代码
七. 光照效果
原理: 对图像中的某一范围内的像素的亮度分别进行处理.
效果图:
附件: 2008092313414314.jpg
实现代码:
光照效果
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //以光照效果显示图像
  4. Graphics MyGraphics = this.pictureBox1.CreateGraphics();
  5. MyGraphics.Clear(Color.White);
  6. Bitmap MyBmp = new Bitmap(this.pictureBox1.Image, this.pictureBox1.Width, this.pictureBox1.Height);
  7. int MyWidth = MyBmp.Width;
  8. int MyHeight = MyBmp.Height;
  9. Bitmap MyImage = MyBmp.Clone(new RectangleF(0, 0, MyWidth, MyHeight), System.Drawing.Imaging.PixelFormat.DontCare);
  10. int A = Width / 2;
  11. int B = Height / 2;
  12. //MyCenter图片中心点,发亮此值会让强光中心发生偏移
  13. Point MyCenter = new Point(MyWidth / 2, MyHeight / 2);
  14. //R强光照射面的半径,即”光晕”
  15. int R = Math.Min(MyWidth / 2, MyHeight / 2);
  16. for (int i = MyWidth - 1; i >= 1; i--)
  17. {
  18. for (int j = MyHeight - 1; j >= 1; j--)
  19. {
  20. float MyLength = (float)Math.Sqrt(Math.Pow((i - MyCenter.X), 2) + Math.Pow((j - MyCenter.Y), 2));
  21. //如果像素位于”光晕”之内
  22. if (MyLength < R)
  23. {
  24. Color MyColor = MyImage.GetPixel(i, j);
  25. int r, g, b;
  26. //220亮度增加常量,该值越大,光亮度越强
  27. float MyPixel = 220.0f * (1.0f - MyLength / R);
  28. r = MyColor.R + (int)MyPixel;
  29. r = Math.Max(0, Math.Min(r, 255));
  30. g = MyColor.G + (int)MyPixel;
  31. g = Math.Max(0, Math.Min(g, 255));
  32. b = MyColor.B + (int)MyPixel;
  33. b = Math.Max(0, Math.Min(b, 255));
  34. //将增亮后的像素值回写到位图
  35. Color MyNewColor = Color.FromArgb(255, r, g, b);
  36. MyImage.SetPixel(i, j, MyNewColor);
  37. }
  38. }
  39. //重新绘制图片
  40. MyGraphics.DrawImage(MyImage, new Rectangle(0, 0, MyWidth, MyHeight));
  41. }

  42. }
  43. }
复制代码
八.百叶窗效果
原理:(1).垂直百叶窗效果:
根据窗口或图像的高度或宽度和定制的百叶窗显示条宽度计算百叶窗显示的条数量 ;
根据窗口或图像的高度或宽度定制百叶窗显示条数量计算百窗显示的条宽度.
(2).水平百叶窗效果: 原理同上,只是绘制像素点开始的坐标不同.
效果图:
附件: 2008092314525230.jpg     附件: 2008092314541655.jpg
实现代码:
垂直百叶窗
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //垂直百叶窗显示图像
  4. try
  5. {
  6. MyBitmap = (Bitmap)this.pictureBox1.Image.Clone();
  7. int dw = MyBitmap.Width / 30;
  8. int dh = MyBitmap.Height;
  9. Graphics g = this.pictureBox1.CreateGraphics();
  10. g.Clear(Color.Gray);
  11. Point[] MyPoint = new Point[30];
  12. for (int x = 0; x < 30; x++)
  13. {
  14. MyPoint[x].Y = 0;
  15. MyPoint[x].X = x * dw;
  16. }
  17. Bitmap bitmap = new Bitmap(MyBitmap.Width, MyBitmap.Height);
  18. for (int i = 0; i < dw; i++)
  19. {
  20. for (int j = 0; j < 30; j++)
  21. {
  22. for (int k = 0; k < dh; k++)
  23. {
  24. bitmap.SetPixel(MyPoint[j].X + i, MyPoint[j].Y + k,
  25. MyBitmap.GetPixel(MyPoint[j].X + i, MyPoint[j].Y + k));
  26. }
  27. }
  28. this.pictureBox1.Refresh();
  29. this.pictureBox1.Image = bitmap;
  30. System.Threading.Thread.Sleep(100);
  31. }
  32. }
  33. catch (Exception ex)
  34. {
  35. MessageBox.Show(ex.Message, "信息提示");
  36. }

  37. }
复制代码
水平百叶窗
  1. private void button3_Click(object sender, EventArgs e)
  2. {
  3. //水平百叶窗显示图像
  4. try
  5. {
  6. MyBitmap = (Bitmap)this.pictureBox1.Image.Clone();
  7. int dh = MyBitmap.Height / 20;
  8. int dw = MyBitmap.Width;
  9. Graphics g = this.pictureBox1.CreateGraphics();
  10. g.Clear(Color.Gray);
  11. Point[] MyPoint = new Point[20];
  12. for (int y = 0; y < 20; y++)
  13. {
  14. MyPoint[y].X = 0;
  15. MyPoint[y].Y = y * dh;
  16. }
  17. Bitmap bitmap = new Bitmap(MyBitmap.Width, MyBitmap.Height);
  18. for (int i = 0; i < dh; i++)
  19. {
  20. for (int j = 0; j < 20; j++)
  21. {
  22. for (int k = 0; k < dw; k++)
  23. {
  24. bitmap.SetPixel(MyPoint[j].X + k, MyPoint[j].Y + i, MyBitmap.GetPixel(MyPoint[j].X + k, MyPoint[j].Y + i));
  25. }
  26. }
  27. this.pictureBox1.Refresh();
  28. this.pictureBox1.Image = bitmap;
  29. System.Threading.Thread.Sleep(100);
  30. }
  31. }
  32. catch (Exception ex)
  33. {
  34. MessageBox.Show(ex.Message, "信息提示");
  35. }
  36. }
复制代码
九.马赛克效果
原理: 确定图像的随机位置点和确定马赛克块的大小,然后马赛克块图像覆盖随机点即可.
效果图:
附件: 2008092315095126.jpg
实现代码:
马赛克效果
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //以马赛克效果显示图像
  4. try
  5. {
  6. int dw = MyBitmap.Width / 50;
  7. int dh = MyBitmap.Height / 50;
  8. Graphics g = this.pictureBox1.CreateGraphics();
  9. g.Clear(Color.Gray);
  10. Point[] MyPoint = new Point[2500];
  11. for (int x = 0; x < 50; x++)
  12. for (int y = 0; y < 50; y++)
  13. {
  14. MyPoint[x * 50 + y].X = x * dw;
  15. MyPoint[x * 50 + y].Y = y * dh;
  16. }
  17. Bitmap bitmap = new Bitmap(MyBitmap.Width, MyBitmap.Height);
  18. for (int i = 0; i < 10000; i++)
  19. {
  20. System.Random MyRandom = new Random();
  21. int iPos = MyRandom.Next(2500);
  22. for (int m = 0; m < dw; m++)
  23. for (int n = 0; n < dh; n++)
  24. {
  25. bitmap.SetPixel(MyPoint[iPos].X + m, MyPoint[iPos].Y + n, MyBitmap.GetPixel(MyPoint[iPos].X + m, MyPoint[iPos].Y + n));
  26. }
  27. this.pictureBox1.Refresh();
  28. this.pictureBox1.Image = bitmap;
  29. }
  30. for (int i = 0; i < 2500; i++)
  31. for (int m = 0; m < dw; m++)
  32. for (int n = 0; n < dh; n++)
  33. {
  34. bitmap.SetPixel(MyPoint[i].X + m, MyPoint[i].Y + n, MyBitmap.GetPixel(MyPoint[i].X + m, MyPoint[i].Y + n));
  35. }
  36. this.pictureBox1.Refresh();
  37. this.pictureBox1.Image = bitmap;
  38. }
  39. catch (Exception ex)
  40. {
  41. MessageBox.Show(ex.Message, "信息提示");
  42. }
  43. }
复制代码
十. 油画效果
原理: 对图像中某一范围内的像素引入随机值.
效果图:
附件: 2008092315141094.jpg
实现代码:
油画效果
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //以油画效果显示图像
  4. Graphics g = this.panel1.CreateGraphics();
  5. //Bitmap bitmap = this.MyBitmap;
  6. //取得图片尺寸
  7. int width = MyBitmap.Width;
  8. int height = MyBitmap.Height;
  9. RectangleF rect = new RectangleF(0, 0, width, height);
  10. Bitmap img = MyBitmap.Clone(rect, System.Drawing.Imaging.PixelFormat.DontCare);
  11. //产生随机数序列
  12. Random rnd = new Random();
  13. //取不同的值决定油画效果的不同程度
  14. int iModel = 2;
  15. int i = width - iModel;
  16. while (i > 1)
  17. {
  18. int j = height - iModel;
  19. while (j > 1)
  20. {
  21. int iPos = rnd.Next(100000) % iModel;
  22. //将该点的RGB值设置成附近iModel点之内的任一点
  23. Color color = img.GetPixel(i + iPos, j + iPos);
  24. img.SetPixel(i, j, color);
  25. j = j - 1;
  26. }
  27. i = i - 1;
  28. }
  29. //重新绘制图像
  30. g.Clear(Color.White);
  31. g.DrawImage(img, new Rectangle(0, 0, width, height));
  32. }
复制代码
十一: 扭曲效果
原理: 将图像缩放为一个非矩形的平等四边形即可
效果图:
附件: 2008092315191111.jpg
实现代码:
扭曲效果
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //以扭曲效果显示图像
  4. if (h == panel1.Height/2)
  5. {
  6. w = 0;
  7. h = 0;
  8. }
  9. Size offset =new Size (w++,h++);//设置偏移量
  10. Graphics g = panel1.CreateGraphics();
  11. Rectangle rect = this.panel1.ClientRectangle;
  12. Point[] points = new Point[3];
  13. points[0] = new Point(rect.Left+offset.Width ,rect.Top +offset .Height);
  14. points[1] = new Point(rect.Right, rect.Top + offset.Height);
  15. points[2] = new Point(rect.Left, rect.Bottom - offset.Height);
  16. g.Clear(Color.White);
  17. g.DrawImage(MyBitmap, points);
  18. }
复制代码
十二.积木效果
原理: 对图像中的各个像素点着重(即加大分像素的颜色值)着色.
效果图:
附件: 2008092315230292.jpg
实现代码:
积木效果
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. //以积木效果显示图像
  4. try
  5. {
  6. Graphics myGraphics = this.panel1.CreateGraphics ();
  7. //Bitmap myBitmap = new Bitmap(this.BackgroundImage);
  8. int myWidth, myHeight, i, j, iAvg, iPixel;
  9. Color myColor, myNewColor;
  10. RectangleF myRect;
  11. myWidth = MyBitmap.Width;
  12. myHeight = MyBitmap.Height;
  13. myRect = new RectangleF(0, 0, myWidth, myHeight);
  14. Bitmap bitmap = MyBitmap.Clone(myRect, System.Drawing.Imaging.PixelFormat.DontCare);
  15. i = 0;
  16. while (i < myWidth - 1)
  17. {
  18. j = 0;
  19. while (j < myHeight - 1)
  20. {
  21. myColor = bitmap.GetPixel(i, j);
  22. iAvg = (myColor.R + myColor.G + myColor.B) / 3;
  23. iPixel = 0;
  24. if (iAvg >= 128)
  25. iPixel = 255;
  26. else
  27. iPixel = 0;
  28. myNewColor = Color.FromArgb(255, iPixel, iPixel, iPixel);
  29. bitmap.SetPixel(i, j, myNewColor);
  30. j = j + 1;
  31. }
  32. i = i + 1;
  33. }
  34. myGraphics.Clear(Color.WhiteSmoke);
  35. myGraphics.DrawImage(bitmap, new Rectangle(0, 0, myWidth, myHeight));
  36. }
  37. catch (Exception ex)
  38. {
  39. MessageBox.Show(ex.Message, "信息提示");
  40. }
  41. }
复制代码

说明.这些大多为静态图. 后面会有图像的动态显示. 如分块合成图像, 四周扩散显示图像, 上下对接显示图像等.

      这些也许能说明一下 PPT或者手机中的图片效果处理程序是如果做出来的.原理应该是相通的.

      制作图像一般常用的类有: Bitmap; Graphics; Rectangle;Color; 用到的方法是 Graphics类的DrawImage;
      此方法共有30个版本, 我习惯用 DrawImage("图像", "图框") 版本.

      因为这个版本的思想是最简单的----把一张**地图像装在一个**地框里! (**代表某种效果的图像和某种效果的框)
      如. g.DrawImage(new Bitmap("myPicture"), new Rectangle(0, 0, myWidth, myHeight));

      呵呵, 到此
    希望对大家有所帮助.

来自:http://www.pin5i.com/showtopic-20228.html

原文地址:https://www.cnblogs.com/gisoracle/p/2019022.html