C# PictureBox 放大、缩小、移动图片
class PictureBoxEx
{
private static bool isMove = false;
private static Point mouseDownPoint;
private static int zoomStep = 30;
public static int ZoomStep
{
get
{
return zoomStep;
}
set
{
zoomStep = value;
}
}
/// <summary>
/// 加载IMG
/// </summary>
/// <param name="sender"></param>
/// <param name="path"></param>
public static void loadImg(object sender, string path)
{
PictureBox pbox = sender as PictureBox;
pbox.MouseDown += new System.Windows.Forms.MouseEventHandler(MouseDown);
pbox.MouseMove += new System.Windows.Forms.MouseEventHandler(MouseMove);
pbox.MouseUp += new System.Windows.Forms.MouseEventHandler(MouseUp);
Image img = Image.FromFile(path);
pbox.Image = img;
}
public static void 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;
}
}
public static void 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);
}
public static void MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMove = false;
}
}
public static void 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 drawGrids(PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen myPen = Pens.Blue;
for (int i = 0; i < ClientRectangle.Width; i++)
{
g.DrawLine(myPen, new Point(i, 0), new Point(i, ClientRectangle.Bottom));
i += 50;
}
for (int j = 0; j < ClientRectangle.Height; j++)
{
g.DrawLine(myPen, new Point(0, j), new Point(ClientRectangle.Right, j));
j += 50;
}
}
private void drawLine(PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen myPen = Pens.BlueViolet;
int i = pictureBox1.Width / 2;
int j = pictureBox1.Height / 2;
g.DrawLine(myPen, new Point(i, 0), new Point(i, ClientRectangle.Bottom));
g.DrawLine(myPen, new Point(0, j), new Point(ClientRectangle.Right, j));
}
private void pictureBox1_Paint (object sender, PaintEventArgs e) {
if (checkBox1.Checked) {
//drawGrids(e);
drawLine (e);
}
}
private void checkBox1_CheckStateChanged (object sender, EventArgs e) {
pictureBox1.Refresh ();
}