C#- 实现图片的放大缩小移动功能
不说费话了,自己理解代码,直接上代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CsReviewView { public partial class FormPicView : Form { public FormPicView() { InitializeComponent(); } Point imagePoint = new Point(); Bitmap bkImage;//定义Bitmap变量 /// <summary> /// 在窗体背景中显示被拖放的图片 /// </summary> public void SetDragImageToFrm(Form form, DragEventArgs e) { e.Effect = DragDropEffects.Copy;//设置拖放操作中目标放置类型为复制 //检索数据格式相关联的数据 String[] str_Drop = (String[])e.Data.GetData(DataFormats.FileDrop, true); string strPath; strPath = str_Drop[0];//获取拖放文件的目录 try { bkImage = new Bitmap(strPath);//存储拖放的图片 pictureBox1.Image = bkImage;//在pictureBox上显示图片 } catch { } } public 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) //放大 { int width = Convert.ToInt32(pictureBox1.Width * 1.5); int height = Convert.ToInt32(pictureBox1.Height * 1.5); if (width * height > 45800000) return; pictureBox1.Width = width; pictureBox1.Height = height; 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 < 100 || pictureBox1.Height < 100) return; pictureBox1.Width = pictureBox1.Width / 2; pictureBox1.Height = pictureBox1.Height / 2; 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); } private void FormPicView_DragEnter(object sender, DragEventArgs e) { SetDragImageToFrm(this, e); } private void FormPicView_Load(object sender, EventArgs e) { this.AllowDrop = true; pictureBox1.Location = new Point(0, 0); pictureBox1.Width = this.ClientRectangle.Width; pictureBox1.Height = this.ClientRectangle.Height-10; pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; pictureBox1.MouseWheel += pictureBox1_MouseWheel; pictureBox1.MouseDown += PictureBox1_MouseDown; pictureBox1.MouseUp += PictureBox1_MouseUp; pictureBox1.MouseMove += PictureBox1_MouseMove; } protected override void OnResize(EventArgs e) { base.OnResize(e); pictureBox1.Width = this.ClientRectangle.Width; pictureBox1.Height = this.ClientRectangle.Height - 10; } private Point mouseDownPoint = new Point(); private bool flagIsMove = false; private void PictureBox1_MouseMove(object sender, MouseEventArgs e) { pictureBox1.Focus(); //鼠标在picturebox上时才有焦点,此时可以缩放 if (flagIsMove) { 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 PictureBox1_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { flagIsMove = false; } } private void PictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { mouseDownPoint.X = Cursor.Position.X; //记录鼠标左键按下时位置 mouseDownPoint.Y = Cursor.Position.Y; flagIsMove = true; pictureBox1.Focus(); //鼠标滚轮事件(缩放时)需要picturebox有焦点 } } } }