PictureBox中的图片实现鼠标滑轮控制缩放和平移

Posted mazai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PictureBox中的图片实现鼠标滑轮控制缩放和平移相关的知识,希望对你有一定的参考价值。

昨天刚学会了如何实现PictureBox控件中的图像自动缩放和平移,下面我把过程给大家说一下:

参考了:https://www.cnblogs.com/jinqier/p/3497201.html

1、首先要拖一个控件,Panel控件作为容器

2、然后父容器里面拖一个PictureBox控件,设置属性为“”停靠在父容器“”

pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.Dock = DockStyle.None;

 3‘设置背景色了。Panel 和PictureBox的背景色要设置成一样的,我这里设置的都是为Blue;

4、为PictureBox添加事件了,下面是代码

 1  public System.Drawing.Point mouseDownPoint;//存储鼠标焦点的全局变量
 2         public bool isSelected = false;
 3         private void Form1_Load(object sender, EventArgs e)
 4         {
 5             pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
 6             pictureBox1.Dock = DockStyle.None;
 7           
 8         }
 9       //这个事件是鼠标滑轮滚动的触发事件,可以在Designer.cs中注册。
      this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel);
      this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
      this.pictureBox1.MouseEnter += new System.EventHandler(this.pictureBox1_MouseEnter);
      this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
      this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
     
     10 private void pictureBox1_MouseWheel(object sender, MouseEventArgs e) 11 { 12 double scale = 1; 13 if (pictureBox1.Height > 0) 14 { 15 scale = (double)pictureBox1.Width / (double)pictureBox1.Height; 16 } 17 pictureBox1.Width += (int)(e.Delta * scale); 18 pictureBox1.Height += e.Delta; 19 } 20 private void pictureBox1_MouseEnter(object sender, EventArgs e) 21 { 22 pictureBox1.Focus(); 23 pictureBox1.Cursor = Cursors.SizeAll; 24 } 25 26 //在MouseDown处获知鼠标是否按下,并记录下此时的鼠标坐标值; 27 private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 28 { 29 if (e.Button == MouseButtons.Left) 30 { 31 mouseDownPoint.X = Cursor.Position.X; //注:全局变量mouseDownPoint前面已定义为Point类型 32 mouseDownPoint.Y = Cursor.Position.Y; 33 isSelected = true; 34 } 35 } 36 37 //在MouseUp处获知鼠标是否松开,终止拖动操作; 38 private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 39 { 40 isSelected = false; 41 42 } 43 44 private bool IsMouseInPanel() 45 { 46 if (this.panel_Picture.Left < PointToClient(Cursor.Position).X 47 && PointToClient(Cursor.Position).X < this.panel_Picture.Left 48 + this.panel_Picture.Width && this.panel_Picture.Top 49 < PointToClient(Cursor.Position).Y && PointToClient(Cursor.Position).Y 50 < this.panel_Picture.Top + this.panel_Picture.Height) 51 { 52 return true; 53 } 54 else 55 { 56 return false; 57 } 60 } 61 62 //图片平移,在MouseMove处添加拖动函数操作 63 private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 64 { 65 if (isSelected && IsMouseInPanel())//确定已经激发MouseDown事件,和鼠标在picturebox的范围内 66 { 67 this.pictureBox1.Left = this.pictureBox1.Left + (Cursor.Position.X - mouseDownPoint.X); 68 this.pictureBox1.Top = this.pictureBox1.Top + (Cursor.Position.Y - mouseDownPoint.Y); 69 mouseDownPoint.X = Cursor.Position.X; 70 mouseDownPoint.Y = Cursor.Position.Y; 71 } 72 73 }


此方法实现的原理就是改变PictureBox的大小而改变图像的大小,平移也是。

这个比较简单的一种方法。

 

以上是关于PictureBox中的图片实现鼠标滑轮控制缩放和平移的主要内容,如果未能解决你的问题,请参考以下文章

Vue 实现图片监听鼠标滑轮滚动实现图片缩小放大功能

我要用C#实现对图像的缩放、鼠标拖曳和灰度拉伸等功能,且图像可能非常大,请问我该使用picturebox控件吗

带有缩放和平移功能的 Windows 窗体的 PictureBox

C# winform 中的picturebox 中装载的图片怎样随滚轮放大缩小,图框大小不变

Winform鼠标滑轮控制自定义滚动条

c#如何设置panel的鼠标滑轮事件?MouseWheel只能应用于Form吗?