【C#】图片处理——浮雕、黑白、油画效果源码

本例应用场合:对图片进行基本的翻转操作,图片浮雕、黑白、油画效果的实现。

本例编译环境VS2010及以上版本编译通过。

运行效果:

 

主要功能:

  • 图片浮雕、黑白、油画效果处理;

  • 图片翻转处理——旋转180°,顺时针、逆时针旋转90°,垂直、水平翻转;

  • 图片保存、另存为。

 

主要代码:

 1 /// <summary>
 2 /// 浮雕效果
 3 /// </summary>
 4 /// <param name="sender"></param>
 5 /// <param name="e"></param>
 6 private void btnFudiao_Click(object sender, EventArgs e)
 7 {
 8         label1.Text = "正在完成图片操作,可能会出现程序未响应的情况。请稍等一段时间,如果等待时间过长,请重新启动程序!";
 9         shangyibu = new Bitmap(pictureBox1.Image);
10         Thread.Sleep(500);
11         try
12         {
13                 Bitmap newBitmap = new Bitmap(intWidth, intHeight);
14                 Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image;
15                 Color pixel1, pixel2;
16                 for (int x = 0; x < intWidth - 1; x++)
17                 {
18                         for (int y = 0; y < intHeight - 1; y++)
19                         {
20                                 int r = 0, g = 0, b = 0;
21                                 pixel1 = oldBitmap.GetPixel(x, y);
22                                 pixel2 = oldBitmap.GetPixel(x + 1, y + 1);
23                                 r = Math.Abs(pixel1.R - pixel2.R + 128);
24                                 g = Math.Abs(pixel1.G - pixel2.G + 128);
25                                 b = Math.Abs(pixel1.B - pixel2.B + 128);
26                                 if (r > 255)
27                                 {
28                                         r = 255;
29                                 }
30                                 if (r < 0)
31                                 {
32                                         r = 0;
33                                 }
34                                 if (g > 255)
35                                 {
36                                         g = 255;
37                                 }
38                                 if (g < 0)
39                                 {
40                                         g = 0;
41                                 }
42                                 if (b > 255)
43                                 {
44                                         b = 255;
45                                 }
46                                 if (b < 0)
47                                 {
48                                         b = 0;
49                                 }
50                                 newBitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
51                          }
52                 }
53                 this.pictureBox1.Image = newBitmap;
54                 label1.Text = "已完成图片操作:“浮雕”!";
55                 save = 1;
56                 this.Text = "*图片处理——" + open;
57         }
58         catch (Exception ex)
59         {
60                 MessageBox.Show(ex.Message);
61         }
62 }                                                                                                                
View Code
 1         /// <summary>
 2         /// 黑白效果
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void btnHeibai_Click(object sender, EventArgs e)
 7         {
 8             label1.Text = "正在完成图片操作,可能会出现程序未响应的情况。请稍等一段时间,如果等待时间过长,请重新启动程序!";
 9             shangyibu = new Bitmap(pictureBox1.Image);
10             Thread.Sleep(500);
11             try
12             {
13                 Bitmap newBitmap = new Bitmap(intWidth, intHeight);
14                 Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image;
15                 Color pixel;
16                 for (int x = 0; x < intWidth; x++)
17                 {
18                     for (int y = 0; y < intHeight; y++)
19                     {
20                         pixel = oldBitmap.GetPixel(x, y);
21                         int r, g, b, Result = 0;
22                         r = pixel.R;
23                         g = pixel.G;
24                         b = pixel.B;
25                         int iType = 2;
26                         switch (iType)
27                         {
28                             case 0:
29                                 Result = ((r + g + b) / 3);
30                                 break;
31                             case 1:
32                                 Result = r > g ? r : g;
33                                 Result = Result > b ? Result : b;
34                                 break;
35                             case 2:
36                                 Result = ((int)(0.7 * r) + (int)(0.2 * g) + (int)(0.1 * b));
37                                 break;
38                         }
39                         newBitmap.SetPixel(x, y, Color.FromArgb(Result, Result, Result));
40                     }
41                 }
42                 this.pictureBox1.Image = newBitmap;
43                 label1.Text = "已完成图片操作:“黑白”!";
44                 save = 1;
45                 this.Text = "*图片处理——" + open;
46             }
47             catch (Exception ex)
48             {
49                 MessageBox.Show(ex.Message);
50             }
51         }
View Code
 1         /// <summary>
 2         /// 保存图片
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void saveToolStripMenuItem_Click(object sender, EventArgs e)
 7         {
 8             System.IO.FileStream picture = null;
 9             try
10             {
11                 Bitmap bmp = new Bitmap(pictureBox1.Image);
12                 s = new Bitmap(pictureBox1.Image);
13                 bmp.Save(open);
14                 MessageBox.Show("保存成功");
15                 label1.Text = "保存成功!";
16                 this.Text = "图片处理——" + open;
17                 baocun = new Bitmap(pictureBox1.Image);
18                 save = 0;
19                 save = 0;
20                 this.Text = "图片处理——" + open;
21             }
22             catch (Exception ex)
23             {
24                 MessageBox.Show(ex.Message);
25                 label1.Text = "保存失败!";
26             }
27             finally
28             {
29                 if (picture != null)
30                 {
31                     picture.Close();
32                     picture.Dispose();
33                 }
34             }
35         }
36     
View Code
 1         /// <summary>
 2         /// 还原图片
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void btnHuanyuan_Click(object sender, EventArgs e)
 7         {
 8             DialogResult aaa = MessageBox.Show("你确定要还原为未修改过的原照片吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
 9             if (aaa == DialogResult.Yes)
10             {
11                 pictureBox1.Image = huanyuan;
12 
13                 if (pictureBox1.Image != s)
14                 {
15                     save = 1;
16                     this.Text = "*图片处理——" + open;
17                 }
18                 else
19                 {
20                     save = 0;
21                     this.Text = "图片处理——" + open;
22                 }
23             }
24         }
View Code

 

图片的打开、保存、翻转功能详见源代码。

源代码免费下载地址请关注公众号【几行简码】后回复【图片处理】获取。

原创文章,转载请阅读公众号内转载须知。

点关注,不迷路。

posted @ 2020-04-29 11:08  seuzy  阅读(401)  评论(0编辑  收藏  举报