尝试制作缩放效果

发布于 2024-08-25 01:51:08 字数 694 浏览 5 评论 0原文

我正在尝试用鼠标滚轮在图片框上制作缩放效果。一切正常,除了当我使用鼠标中键放大或缩小时,可以,但它不会放大或缩小鼠标光标所在的点。当我放大该点时,我希望它始终滑动。请帮我添加一个代码片段以使其工作。

这是我的代码:

int i = 5;
int index = 10;
private double[] zoomfactor = { .25, .33, .50, .66, .80, 1, 1.25, 1.5, 2.0, 2.5, 3.0 };

private void Zoom(int i)
{

  double new_Zoom = zoomfactor[i];

  imgBox.Width = Convert.ToInt32(imgBox.Image.Width * new_Zoom);
  imgBox.Height = Convert.ToInt32(imgBox.Image.Height * new_Zoom);

}

private void On_wheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
  i = i + e.Delta / 120;
  if (i < 0)
  {
    i = 0;
  }
  else
  {
    if (i <= index)
      i = i;
    else
      i = index;
   }
   Zoom(i);
}

I am trying to make a zoom effect on a picturebox with mouse wheel. Everything is OK except that when I use mouse middle button to zoom in or zoom out, it is ok, but it does not zoom in or zoom out the point which the mouse cursor is on. When I zoom in the point, I want it always slide. Please help me add a code snippet to make it work.

Here is my code:

int i = 5;
int index = 10;
private double[] zoomfactor = { .25, .33, .50, .66, .80, 1, 1.25, 1.5, 2.0, 2.5, 3.0 };

private void Zoom(int i)
{

  double new_Zoom = zoomfactor[i];

  imgBox.Width = Convert.ToInt32(imgBox.Image.Width * new_Zoom);
  imgBox.Height = Convert.ToInt32(imgBox.Image.Height * new_Zoom);

}

private void On_wheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
  i = i + e.Delta / 120;
  if (i < 0)
  {
    i = 0;
  }
  else
  {
    if (i <= index)
      i = i;
    else
      i = index;
   }
   Zoom(i);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷乱花海 2024-09-01 01:51:08

您需要根据鼠标相对于表单的位置来调整图片框的位置。

这是一个粗略但有效的示例,说明了如何执行此操作:

var i = 5;
var zoomfactor = new[] {.25, .33, .50, .66, .80, 1, 1.25, 1.5, 2.0, 2.5, 3.0};
var origin = new Point(100, 100);
var image = Image.FromFile(@"c:\example.png");
var imgBox = new PictureBox {
        Location = origin,
        Size = image.Size,
        Image = image,
        SizeMode = PictureBoxSizeMode.StretchImage
    };
var form = new Form {
        Size = new Size(800, 600),
        Controls = {imgBox}
    };
form.MouseWheel += (sender, e) => {
        i += e.Delta/120;
        if (i < 0) {
            i = 0;
        }
        if (i >= zoomfactor.Length) {
            i = zoomfactor.Length - 1;
        }
        var newZoom = zoomfactor[i];
        imgBox.Width = (int) (imgBox.Image.Width*newZoom);
        imgBox.Height = (int) (imgBox.Image.Height*newZoom);
        imgBox.Left = (int) (e.X - newZoom*(e.X - origin.X));
        imgBox.Top = (int) (e.Y - newZoom*(e.Y - origin.Y));
    };
form.ShowDialog();

You need to adjust the picture box location based on the mouse position relative to the form.

Here is a rough but working example of how you might do this:

var i = 5;
var zoomfactor = new[] {.25, .33, .50, .66, .80, 1, 1.25, 1.5, 2.0, 2.5, 3.0};
var origin = new Point(100, 100);
var image = Image.FromFile(@"c:\example.png");
var imgBox = new PictureBox {
        Location = origin,
        Size = image.Size,
        Image = image,
        SizeMode = PictureBoxSizeMode.StretchImage
    };
var form = new Form {
        Size = new Size(800, 600),
        Controls = {imgBox}
    };
form.MouseWheel += (sender, e) => {
        i += e.Delta/120;
        if (i < 0) {
            i = 0;
        }
        if (i >= zoomfactor.Length) {
            i = zoomfactor.Length - 1;
        }
        var newZoom = zoomfactor[i];
        imgBox.Width = (int) (imgBox.Image.Width*newZoom);
        imgBox.Height = (int) (imgBox.Image.Height*newZoom);
        imgBox.Left = (int) (e.X - newZoom*(e.X - origin.X));
        imgBox.Top = (int) (e.Y - newZoom*(e.Y - origin.Y));
    };
form.ShowDialog();
迟月 2024-09-01 01:51:08

您没有考虑鼠标坐标。

MouseEventArgs 类告诉您鼠标所在的位置(XYLocation 属性),因此您需要进行相应调整。

You are not taking the mouse coordinates into account.

The MouseEventArgs class tells you where the mouse is (X, Y and Location properties), so you need to adjust accordingly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文