我非常接近实现自己的最终目标,但我似乎无法弄清楚。
在Form1加载中,应用程序:
我的问题是关于变焦。尽管可以使用,但只能从左上角放大和缩小,然后再回到左上角。
我想为用户提供放大其鼠标光标当前所处位置的体验。如果用户没有移动鼠标,只有滚轮,我想从同一点进行缩小。
这是我的代码,它执行上面列出的所有操作,
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// ONLY WORKS IN ZOOM MODE UNLESS...?
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
Image image;
// LOAD IMAGE CODE
OpenFileDialog open = new OpenFileDialog();
// image filters
open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp; *.tiff)|*.jpg; *.jpeg; *.gif; *.bmp; *.tiff";
if (open.ShowDialog() == DialogResult.OK)
{
image = Image.FromFile(open.FileName);
pictureBox1.Height = image.Height;
pictureBox1.Width = image.Width;
pictureBox1.Image = image;
}
}
// ZOOM IN/OUT CODE
protected override void OnMouseWheel(MouseEventArgs e)
{
if (e.Delta != 0)
{
if (e.Delta <= 0)
{
//set minimum size to zoom
if (pictureBox1.Width < 100)
return;
}
else
{
//set maximum size to zoom
if (pictureBox1.Width > 700)
return;
}
pictureBox1.Width += Convert.ToInt32(pictureBox1.Width * e.Delta / 1000);
pictureBox1.Height += Convert.ToInt32(pictureBox1.Height * e.Delta / 1000);
pictureBox1.Invalidate();
}
}
// DRAG PICTURE BOX CODE
private bool _moving;
private Point _startLocation;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
_moving = true;
_startLocation = e.Location;
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
_moving = false;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (_moving)
{
pictureBox1.Left += e.Location.X - _startLocation.X;
pictureBox1.Top += e.Location.Y - _startLocation.Y;
}
}