水平和垂直遮罩图像动画效果

1.首先我们先设计好一个界面,如下图:
2. 水平和垂直遮罩图像动画效果
3.然后声明一个全局变量:
4. Bitmap MyBitmap;
5.
6.接下来写图像浏览点击事件:
7. private void button4_Click(object sender, EventArgs e)
8. {
9. openFileDialog1.Filter = “.jpg,.jpeg,.bmp,.gif,.ico,.png,.tif,.wmf|.jpg;.jpeg;.bmp;.gif;.ico;.png;.tif;.wmf”;
10.//设置打开图像的类型
11. this.openFileDialog1.ShowDialog(); //打开图像文件
12.
13. if (this.openFileDialog1.FileName.Trim() == “”)
14. return;
15. try
16. {
17. //得到原始大小的图像
18. Bitmap SrcBitmap = new Bitmap(this.openFileDialog1.FileName);
19. //得到缩放后的图像
20. MyBitmap = new Bitmap(SrcBitmap, this.pictureBox1.Width, this.pictureBox1.Height);
21. this.pictureBox1.Image = MyBitmap;
22. }
23. catch (Exception Err)
24. {
25. MessageBox.Show(this, “打开图像文件错误!”,"信息提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
26. }
27. }
28.
29.水平遮罩点击事件代码:
30. private void button1_Click(object sender, EventArgs e)
31. {
32. int iWidth = this.pictureBox1.Width; //图像宽度
33. int iHeight = this.pictureBox1.Height; //图像高度
34. //取得Graphics对象
35. Graphics g = this.pictureBox1.CreateGraphics();
36. g.Clear(Color.Gray); //初始为全灰色
37. Bitmap bitmap = new Bitmap(iWidth, iHeight);
38. int x = 0;
39. while (x <= iWidth / 2)
40. {
41. for (int i = 0; i <= iHeight - 1; i++)
42. {
43. bitmap.SetPixel(x, i, MyBitmap.GetPixel(x, i));
44. }
45. for (int i = 0; i <= iHeight - 1; i++)
46. {
47. bitmap.SetPixel(iWidth - x - 1, i,
48. MyBitmap.GetPixel(iWidth - x - 1, i));
49. }
50. x++;
51. this.pictureBox1.Refresh();
52. this.pictureBox1.Image = bitmap;
53. System.Threading.Thread.Sleep(3);
54. }
55. }
56.
57.垂直遮罩点击事件代码:
58. private void button2_Click(object sender, EventArgs e)
59. {
60.
61. int iWidth = this.pictureBox1.Width; //图像宽度
62. int iHeight = this.pictureBox1.Height; //图像高度
63. //取得Graphics对象
64. Graphics g = this.pictureBox1.CreateGraphics();
65. g.Clear(Color.Gray); //初始为全灰色
66. Bitmap bitmap = new Bitmap(iWidth, iHeight);
67. int x = 0;
68. while (x <= iHeight / 2)
69. {
70. for (int i = 0; i <= iWidth - 1; i++)
71. {
72. bitmap.SetPixel(i, x,
73. MyBitmap.GetPixel(i, x));
74. }
75. for (int i = 0; i <= iWidth - 1; i++)
76. {
77. bitmap.SetPixel(i, iHeight - x - 1,
78. MyBitmap.GetPixel(i, iHeight - x - 1));
79. }
80. x++;
81. this.pictureBox1.Refresh();
82. this.pictureBox1.Image = bitmap;
83. System.Threading.Thread.Sleep(3);
84. }
85.
86. }
87.
88.退出点击事件代码:
89. private void button3_Click(object sender, EventArgs e)
90. {
91. Application.Exit();
92. }
93.
94.所有效果图如下:
水平和垂直遮罩图像动画效果水平和垂直遮罩图像动画效果水平和垂直遮罩图像动画效果