简体   繁体   中英

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 .

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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