简体   繁体   English

在 MouseDown 事件上,更改图像的大小

[英]On MouseDown event, change size of image

I have an image that I need to change the size of at runtime, what I'm trying to do is that when a user press's the left mouse button and the cursor is on the border of the image then when he moves the mouse cursor the image size changes according to where the cursor is going.我有一张需要在运行时更改大小的图像,我想要做的是当用户按下鼠标左键并且光标位于图像的边框上时,当他移动鼠标光标时图像大小根据光标所在的位置而变化。

This is what I have so far:这是我到目前为止:

   Point p = e.GetPosition(this);
   Image img = (Image)e.Source;
   var pos =Mouse.GetPosition(this);


   if ((p.X == 89 && (p.Y > 204 && p.Y < 252)) || (p.Y == 245 && (p.X > 89 && p.X < 138)) || (p.Y == 213 && (p.X > 89 && p.X < 138)) || p.X == 138 && p.Y > 204 && p.Y < 252)
   {
       img.Width = p.X;
       img.Height = p.Y;
   }

The problem is that the image doesn't change according to the cursor.问题是图像不会根据光标而改变。 Can anyone help?任何人都可以帮忙吗?

The problem is in your code's logic: The image will change only on very specific coordinates.问题在于您的代码逻辑:图像只会在非常特定的坐标上发生变化。 It took me quite a while to exactly click the image when x == 138 for instance, and even then it took me more time to find the pixels that match 204 < y < 252 .例如,当x == 138时,我花了很长时间才能准确单击图像,即使这样,我也花了更多时间来找到与204 < y < 252匹配的像素。

When I did find the correct locations, the image resized itself as requested.当我没有找到正确的位置,将图像尺寸调整自身的要求。

I recommend rechecking the logic in your if statement and at the least - allow a bigger range of coordinates (for instance x <= 40 && x >= 70 instead of just x == 83 )我建议重新检查if语句中的逻辑,至少 - 允许更大范围的坐标(例如x <= 40 && x >= 70而不是x == 83

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM