gpt4 book ai didi

c# - 尝试制作缩放效果

转载 作者:太空宇宙 更新时间:2023-11-03 11:49:37 26 4
gpt4 key购买 nike

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

这是我的代码:

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);
}

最佳答案

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

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

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();

关于c# - 尝试制作缩放效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2432076/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com