C#中,滚动鼠标滚轮时,图片以鼠标点为中心,进行放大缩小,给出代码

1937668561 2016-03-10 10:26:15
我写的代码可以放大缩小,但是缩小之后,跑到左上角去了
代码如下:
private void test_MouseWheel(object sender, MouseEventArgs e)
{
System.Drawing.Size t = pbSample.Size;

t.Width += (int)(e.Delta*1.1f);
t.Height += (int)(e.Delta * 1.1f);

//设置最小显示大小
if (t.Width < 40)
t.Width = 40;
if (t.Height < 30)
t.Height = 30;

//设置最大显示大小
if (t.Width >1000)
t.Width = 1000;
if (t.Height > 850)
t.Height = 850;
pbSample.Width = t.Width;
pbSample.Height = t.Height;
}
...全文
2516 7 打赏 收藏 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
快乐的肉球 2017-08-14
  • 打赏
  • 举报
回复
double step = 1.2;//缩放倍率 if (e.Delta > 0)//以鼠标为中心放大 { //放大解锁拖拽 pictureEdit_Main.Dock = DockStyle.None; pictureEdit_Main.Height = (int)(pictureEdit_Main.Height * step); pictureEdit_Main.Width = (int)(pictureEdit_Main.Width * step); int px_add = (int)(e.X * (step - 1.0)); int py_add = (int)(e.Y * (step - 1.0)); pictureEdit_Main.Location = new Point(pictureEdit_Main.Location.X - px_add, pictureEdit_Main.Location.Y - py_add); } else { //缩小 pictureEdit_Main.Height = (int)(pictureEdit_Main.Height / step); pictureEdit_Main.Width = (int)(pictureEdit_Main.Width / step); int px_add = (int)(e.X * (1.0 - 1.0 / step)); int py_add = (int)(e.Y * (1.0 - 1.0 / step)); pictureEdit_Main.Location = new Point(pictureEdit_Main.Location.X + px_add, pictureEdit_Main.Location.Y + py_add); }
理想三旬 2016-05-13
  • 打赏
  • 举报
回复
引用 4 楼 sp1234 的回复:
你只是改变了 Width 和 Height,没有改变左上角的坐标。每一次缩放之后,应该立刻改变坐标位置。 假设你的鼠标指针坐标是(x,y),而 test 在缩放前的左上角坐标是(u,v),那么缩放之后的 test 的左上角的新坐标就是 (x-(u-x)*s, y-(v-y)*s),这里的 s 就是缩放比率。
应该是 (x+(u-x)*s, y+(v-y)*s) 里面是加号吧。。
Ny-6000 2016-03-10
  • 打赏
  • 举报
回复
具体变动多少,自己算好了,这个理应不难的
Ny-6000 2016-03-10
  • 打赏
  • 举报
回复
因为每次变性大小时,没有重新定位图片位置 这个也需要动态改动的 应该是top和left这2个值
  • 打赏
  • 举报
回复
你只是改变了 Width 和 Height,没有改变左上角的坐标。每一次缩放之后,应该立刻改变坐标位置。 假设你的鼠标指针坐标是(x,y),而 test 在缩放前的左上角坐标是(u,v),那么缩放之后的 test 的左上角的新坐标就是 (x-(u-x)*s, y-(v-y)*s),这里的 s 就是缩放比率。
  • 打赏
  • 举报
回复
你可以看出来,假设 s =0.0,那么test 的新的左上角坐标恰好是(x,y);假设s=1.0,那么 test 的新的左上角的坐标恰好是 (u,v)。
crystal_lz 2016-03-10
  • 打赏
  • 举报
回复
记得以前做过一个 demo http://bbs.csdn.net/topics/390261361

111,128

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧