通过鼠标操作改变pictureBox1的 大小和位置来调整里面图片的缩放和拖动
Bitmap myBmp;
Point mouseDownPoint = new Point(); //记录拖拽过程鼠标位置
bool isMove = false; //判断鼠标在picturebox上移动时,是否处于拖拽过程(鼠标左键是否按下)
int zoomStep = 10;//缩放比例
myBmp = new Bitmap(openFileDlg.FileName);
pictureBox1.BackgroundImage = myBmp;
pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
pictureBox1.Location = new System.Drawing.Point((panel1.Width - pictureBox1.Width) / 2, (panel1.Height - pictureBox1.Height) / 2);
//pictureBox1.Image = myBmp;
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; //设置picturebox为缩放模式
pictureBox1.Width = myBmp.Width;
pictureBox1.Height = myBmp.Height;
#region =鼠标操作picturebox=
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Cursor = Cursors.Hand;
if (pictureBox1.BackgroundImage != null)
{
if (e.Button == MouseButtons.Left)
{
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
isMove = true;
pictureBox1.Focus();
//panel2.Location = new Point((panel1.Width / 2) - pictureBox1.Location.X - 128, (panel1.Height / 2) - pictureBox1.Location.Y - 152);
}
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (pictureBox1.BackgroundImage != null)
{
if (e.Button == MouseButtons.Left)
{
isMove = false;
Cursor = Cursors.Default;
}
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (pictureBox1.BackgroundImage != null)
{
pictureBox1.Focus();
if (isMove)
{
int x, y;
int moveX, moveY;
moveX = Cursor.Position.X - mouseDownPoint.X;
moveY = Cursor.Position.Y - mouseDownPoint.Y;
x = pictureBox1.Location.X + moveX;
y = pictureBox1.Location.Y + moveY;
//防止图片移出panel2裁剪框
if (y > (panel1.Height - 304) / 2 + 20 || y < ((panel1.Height - 304) / 2 + 304 - pictureBox1.Height) - 20 || x > (panel1.Width - 256) / 2 + 40 || x < ((panel1.Width - 256) / 2 + 256 - pictureBox1.Width) - 20)
{
return;
}
pictureBox1.Location = new Point(x, y);
mouseDownPoint.X = Cursor.Position.X;
mouseDownPoint.Y = Cursor.Position.Y;
//panel2.Location = new Point((panel1.Width / 2) - pictureBox1.Location.X - 107, (panel1.Height / 2) - pictureBox1.Location.Y - 150);
PrintCutArea();
}
}
}
//鼠标滚轮放大缩小
private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
if (pictureBox1.BackgroundImage != null)
{
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.Location.Y > (panel1.Height - 304) / 2 || pictureBox1.Location.Y < ((panel1.Height - 304) / 2 + 304 - pictureBox1.Height) || pictureBox1.Location.X > (panel1.Width - 256) / 2 || pictureBox1.Location.X < ((panel1.Width - 256) / 2 + 256 - pictureBox1.Width))
{
return;
}
if (pictureBox1.Width < 256 || pictureBox1.Height < 304)
{
return;
}
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);
//panel2.Location = new Point((panel1.Width / 2) - pictureBox1.Location.X - 128, (panel1.Height / 2) - pictureBox1.Location.Y - 150);
PrintCutArea();
}
}
#endregion