Winform 图片鼠标滚动查看(放大,缩小,旋转,拖动查看)

方法千千万,我只是其中一笔[通过控制PictureBox来控制图片,图片完全施展在控件中]...几久不做,还真有点陌生!

窗体构造中添加鼠标滚动:

1 /// <summary>
2 /// 窗体构造方法
3 /// </summary>
4 public CandidateForm()
5 {
6 InitializeComponent();
7 this.MouseWheel += new MouseEventHandler(CandidateForm_MouseWheel);
8 }

滚动监听:并且保持图片正中

 1 /// <summary>
 2 /// 鼠标滚动
 3 /// </summary>
 4 /// <param name="sender"></param>
 5 /// <param name="e"></param>
 6 private void CandidateForm_MouseWheel(object sender, MouseEventArgs e)
 7 {
 8 this.pic.Dock = DockStyle.None;
 9 this.pic.BorderStyle = BorderStyle.FixedSingle;
10 Size size = this.pic.Size;
11 size.Width += e.Delta;
12 if (size.Width > pic.Image.Width)
13 {
14 pic.Width = pic.Image.Width;
15 pic.Height = pic.Image.Height;
16 }
17 else if (size.Width * pic.Image.Height / pic.Image.Width < pic.Parent.Height - 200)
18 {
19 return;
20 }
21 else
22 {
23 pic.Width = size.Width;
24 pic.Height = size.Width * pic.Image.Height / pic.Image.Width;
25 }
26 pic.Left = (pic.Parent.Width - pic.Width) / 2;
27 pic.Top = (pic.Parent.Height - pic.Height) / 2;
28 }

旋转:

 1 private void 左旋ToolStripMenuItem_Click(object sender, EventArgs e)
 2 {
 3 if (basicBt != null)
 4 {
 5 basicBt = Tools.Rotate(basicBt, 90);
 6 height = this.pic.Width;
 7 width = this.pic.Height;
 8 setBasicPb(basicBt);
 9 }
10 }
11 
12 private void 右旋ToolStripMenuItem_Click(object sender, EventArgs e)
13 {
14 if (basicBt != null)
15 {
16 basicBt = Tools.Rotate(basicBt, 270);
17 height = this.pic.Width;
18 width = this.pic.Height;
19 setBasicPb(basicBt);
20 }
21 }

拖动直接在鼠标点击事件中,对PictureBox位置根据鼠标拖动进行处理就好.关键代码:

1 //拖动
2 if (canDrag)
3 {
4 pic.Location =
5 new Point(pic.Left + e.X - p0.X, pic.Top + e.Y - p0.Y);
6 }

如果不是特意做图片查看,一般还会有对图片进行处理的功能,及需要用到鼠标拖动啊,针对原图片的定位等等~~ 则要用到图片的相对位置定位.

因为使用的是通过改变PictureBox的方法进行处理,所以只需算出比例即可.

1 rate = (double)pic.Width / (double)pic.Image.Width;

知识不在于大小,在于总结。如有错误不足,还请不吝指正。

举报
评论 0