C#控件picturebox实现图像鼠标拖拽和大小缩放
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { switch (comboBox1.SelectedIndex) { case 0: SetTypography(2, 3); break; case 1: SetTypography(3, 3); break; case 2: SetTypography(4, 3); break; default: break; } } private void SetTypography(int x,int y) { panel1.Controls.Clear(); for (int i = 0; i < y; i++) { for (int j = 0; j < x; j++) { PictureBox pbox = new PictureBox(); pbox.Image = Image.FromFile(@"C:\Users\ws\Desktop\one team one goal.jpg"); pbox.SizeMode = PictureBoxSizeMode.Zoom; pbox.Width = 250; pbox.Height = 150; pbox.DoubleClick += Pbox_DoubleClick; pbox.Location = new Point(i * 260+10, j *150+10); panel1.Controls.Add(pbox); } } } int zoomStep = 50; private void Pbox_MouseWheel(object sender, MouseEventArgs e) { PictureBox pbox = sender as PictureBox; int x = e.Location.X; int y = e.Location.Y; int ow = pbox.Width; int oh = pbox.Height; int VX, VY; //因缩放产生的位移矢量 if (e.Delta > 0) //放大 { //第①步 pbox.Width += zoomStep; pbox.Height += zoomStep; //第②步 PropertyInfo pInfo = pbox.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic); Rectangle rect = (Rectangle)pInfo.GetValue(pbox, null); //第③步 pbox.Width = rect.Width; pbox.Height = rect.Height; } if (e.Delta < 0) //缩小 { //防止一直缩成负值 if (pbox.Width < 300) return; pbox.Width -= zoomStep; pbox.Height -= zoomStep; PropertyInfo pInfo = pbox.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic); Rectangle rect = (Rectangle)pInfo.GetValue(pbox, null); pbox.Width = rect.Width; pbox.Height = rect.Height; } //第④步,求因缩放产生的位移,进行补偿,实现锚点缩放的效果 VX = (int)((double)x * (ow - pbox.Width) / ow); VY = (int)((double)y * (oh - pbox.Height) / oh); pbox.Location = new Point(pbox.Location.X + VX, pbox.Location.Y + VY); } Point oldPoint = default; private bool isMove=false; private Point mouseDownPoint; private void Pbox_DoubleClick(object sender, EventArgs e) { PictureBox pbox = sender as PictureBox; if (pbox.Width == 250) { oldPoint = pbox.Location; } if (pbox.Width == 250) { pbox.Width = 780; pbox.Height = 458; pbox.MouseWheel += Pbox_MouseWheel; pbox.MouseDown += pictureBox1_MouseDown; pbox.MouseUp += pictureBox1_MouseUp; pbox.MouseMove += pictureBox1_MouseMove; pbox.Location = new Point(10, 0); pbox.BringToFront(); } else { pbox.Width = 250; pbox.Height = 150; pbox.Location = oldPoint; pbox.MouseWheel -= Pbox_MouseWheel; pbox.MouseDown -= pictureBox1_MouseDown; pbox.MouseUp -= pictureBox1_MouseUp; pbox.MouseMove -= pictureBox1_MouseMove; } } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { PictureBox pbox = sender as PictureBox; if (e.Button == MouseButtons.Left) { mouseDownPoint.X = Cursor.Position.X; //记录鼠标左键按下时位置 mouseDownPoint.Y = Cursor.Position.Y; isMove = true; pbox.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) { PictureBox pbox = sender as PictureBox; pbox.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 = pbox.Location.X + moveX; y = pbox.Location.Y + moveY; pbox.Location = new Point(x, y); mouseDownPoint.X = Cursor.Position.X; mouseDownPoint.Y = Cursor.Position.Y; } }
参考来自:https://www.jb51.net/article/147868.htm
xue wa ji qing dao shan dong lan xiang!