任务
打开一幅图片,实现图片的平移和缩放。
代码
1. using System;
2. using System.Collections.Generic;
3. using System.ComponentModel;
4. using System.Data;
5. using System.Drawing;
6. using System.Linq;
7. using System.Text;
8. using System.Windows.Forms;
9.
10. namespace WindowsFormsApplication1
11. {
12. public partial class Form1 : Form
13. {
14. public Form1()
15. {
16. InitializeComponent();
17. }
18. private string pathname = string.Empty;
19. private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
20. {
21. OpenFileDialog file = new OpenFileDialog();
22. file.InitialDirectory = ".";
23. file.Filter = "所有文件(*.*)|*.*";
24. file.ShowDialog();
25. if (file.FileName != string.Empty)
26. {
27. try
28. {
29. pathname = file.FileName; //获得文件的绝对路径
30. this.pictureBox1.Load(pathname);
31. }
32. catch (Exception ex)
33. {
34. MessageBox.Show(ex.Message);
35. }
36. }
37. }
38.
39. private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
40. {
41. SaveFileDialog save = new SaveFileDialog();
42. save.ShowDialog();
43. if (save.FileName != string.Empty)
44. {
45. pictureBox1.Image.Save(save.FileName);
46. }
47. }
48.
49.
50.
51. private void 上_Click(object sender, EventArgs e)
52. {
53. pictureBox1.Top-=30;
54. }
55.
56. private void button1_Click(object sender, EventArgs e)
57. {
58. pictureBox1.Left -= 30;
59. }
60.
61. private void button2_Click(object sender, EventArgs e)
62. {
63. pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
64. pictureBox1.Width = pictureBox1.Width * 2;
65. pictureBox1.Height = pictureBox1.Height * 2;
66. }
67.
68. private void button3_Click(object sender, EventArgs e)
69. {
70. pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
71. pictureBox1.Width = pictureBox1.Width / 2;
72. pictureBox1.Height = pictureBox1.Height / 2;
73. }
74.
75. private void button4_Click(object sender, EventArgs e)
76. {
77. pictureBox1.Left += 30;
78. }
79.
80. private void button5_Click(object sender, EventArgs e)
81. {
82. pictureBox1.Top += 30;
83. }
84. }
85. }