C#控件picturebox实现图像拖拽和缩放
作者:zyz2233 时间:2023-08-09 08:23:05
本文实例为大家分享了C# picturebox实现图像拖拽和缩放的具体代码,供大家参考,具体内容如下
1.核心步骤:
①新建Point类型全局变量mouseDownPoint,记录拖拽过程中鼠标位置;
②MouseDown事件记录Cursor位置;
③MouseMove事件计算移动矢量,并更新pictureBox1.Location。
代码:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDownPoint.X = Cursor.Position.X; //记录鼠标左键按下时位置
mouseDownPoint.Y = Cursor.Position.Y;
isMove = true;
pictureBox1.Focus(); //鼠标滚轮事件(缩放时)需要picturebox有焦点
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMove = false;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
pictureBox1.Focus(); //鼠标在picturebox上时才有焦点,此时可以缩放
if (isMove)
{
int x, y; //新的pictureBox1.Location(x,y)
int moveX, moveY; //X方向,Y方向移动大小。
moveX = Cursor.Position.X - mouseDownPoint.X;
moveY = Cursor.Position.Y - mouseDownPoint.Y;
x = pictureBox1.Location.X + moveX;
y = pictureBox1.Location.Y + moveY;
pictureBox1.Location = new Point(x, y);
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
}
}
private void panel2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDownPoint.X = Cursor.Position.X; //记录鼠标左键按下时位置
mouseDownPoint.Y = Cursor.Position.Y;
isMove = true;
}
}
private void panel2_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMove = false;
}
}
private void panel2_MouseMove(object sender, MouseEventArgs e)
{
panel2.Focus(); //鼠标不在picturebox上时焦点给别的控件,此时无法缩放
if (isMove)
{
int x, y; //新的pictureBox1.Location(x,y)
int moveX, moveY; //X方向,Y方向移动大小。
moveX = Cursor.Position.X - mouseDownPoint.X;
moveY = Cursor.Position.Y - mouseDownPoint.Y;
x = pictureBox1.Location.X + moveX;
y = pictureBox1.Location.Y + moveY;
pictureBox1.Location = new Point(x, y);
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
}
}
2.图像缩放
核心思想:利用picturebox的zoom模式,根据图像显示大小更改picturebox大小,记录鼠标位置补偿缩放位移,实现锚点缩放,即以鼠标位置为中心进行缩放。
zoomstep --- 自己定义滚轮滑动缩放大小
代码:
//实现锚点缩放(以鼠标所指位置为中心缩放);
//步骤:
//①先改picturebox长宽,长宽改变量一样;
//②获取缩放后picturebox中实际显示图像的长宽,这里长宽是不一样的;
//③将picturebox的长宽设置为显示图像的长宽;
//④补偿picturebox因缩放产生的位移,实现锚点缩放。
// 注释:为啥要②③步?由于zoom模式的机制,把picturebox背景设为黑就知道为啥了。
//这里需要获取zoom模式下picturebox所显示图像的大小信息,添加 using System.Reflection;
//pictureBox1_MouseWheel事件没找到。。。手动添加,别忘在Form1.Designer.cs的“Windows 窗体设计器生成的代码”里加入:
//this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel)。
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
int x = e.Location.X;
int y = e.Location.Y;
int ow = pictureBox1.Width;
int oh = pictureBox1.Height;
int VX, VY; //因缩放产生的位移矢量
if (e.Delta > 0) //放大
{
//第①步
pictureBox1.Width += zoomStep;
pictureBox1.Height += zoomStep;
//第②步
PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
BindingFlags.NonPublic);
Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
//第③步
pictureBox1.Width = rect.Width;
pictureBox1.Height = rect.Height;
}
if (e.Delta < 0) //缩小
{
//防止一直缩成负值
if (pictureBox1.Width < myBmp.Width / 10)
return;
pictureBox1.Width -= zoomStep;
pictureBox1.Height -= zoomStep;
PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
BindingFlags.NonPublic);
Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
pictureBox1.Width = rect.Width;
pictureBox1.Height = rect.Height;
}
//第④步,求因缩放产生的位移,进行补偿,实现锚点缩放的效果
VX = (int)((double)x * (ow - pictureBox1.Width) / ow);
VY = (int)((double)y * (oh - pictureBox1.Height) / oh);
pictureBox1.Location = new Point(pictureBox1.Location.X + VX, pictureBox1.Location.Y + VY);
}
来源:https://blog.csdn.net/qq_39683659/article/details/79627629
标签:C#,图像拖拽,图像缩放
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java8中的LocalDateTime你会使用了吗
2023-11-15 07:02:07
在springboot中对kafka进行读写的示例代码
2023-11-26 00:48:02
![](https://img.aspxhome.com/file/2023/7/60457_0s.png)
使用Springboot对配置文件中的敏感信息加密
2021-05-24 10:50:25
![](https://img.aspxhome.com/file/2023/0/64020_0s.jpg)
Java 8 中 Function 接口使用方法介绍
2022-12-08 23:54:39
java中的前++和后++的区别示例代码详解
2023-09-01 10:15:01
![](https://img.aspxhome.com/file/2023/8/70358_0s.png)
Android开发中ProgressDialog简单用法示例
2021-11-03 10:17:37
Flutter app页面路由以及路由拦截的实现
2023-06-23 14:21:18
![](https://img.aspxhome.com/file/2023/0/87300_0s.gif)
springMvc注解之@ResponseBody和@RequestBody详解
2022-10-09 17:57:19
![](https://img.aspxhome.com/file/2023/2/62762_0s.png)
C# MVC 微信支付教程系列之公众号支付代码
2022-11-29 09:14:29
![](https://img.aspxhome.com/file/2023/6/106396_0s.png)
Android 滑动返回Activity的实现代码
2023-12-09 22:56:58
android实现录屏功能
2022-04-18 05:11:22
SpringBoot后端上传文件类型检测方式
2021-07-02 05:00:04
![](https://img.aspxhome.com/file/2023/6/63296_0s.jpg)
C#简单快速的json组件fastJSON使用介绍
2022-08-13 01:47:50
![](https://img.aspxhome.com/file/2023/8/102288_0s.png)
JFrame中添加和设置JPanel的方法实例解析
2022-07-02 06:04:41
![](https://img.aspxhome.com/file/2023/6/87196_0s.jpg)
Java基础之String类使用与字符串比较
2023-11-09 23:46:51
详谈java中File类getPath()、getAbsolutePath()、getCanonical的区别
2022-08-18 19:21:44
SpringMVC的源码解析
2022-10-05 20:12:18
![](https://img.aspxhome.com/file/2023/4/77494_0s.png)
SpringMVC 传日期参数到后台的实例讲解
2022-01-18 23:31:48
Socket结合线程池使用实现客户端和服务端通信demo
2023-01-21 20:23:50
![](https://img.aspxhome.com/file/2023/5/86835_0s.jpg)
C#操作config文件的具体方法
2023-09-03 12:18:07