question

$$ANON_USER$$ avatar image
0 Votes"
$$ANON_USER$$ asked DanielZhang-MSFT edited

C# How can I find the mouse scroll coordinates?

Hello. I want the image to slide to the right when I shear the image to the right with the mouse, or to the downward when I shear downward. I have "shearing" codes. But I need the x and y coordinates of the mouse. How can I get the coordinates (x and y) of mouse movements? (Not mouse click coordinates. It's like holding the mouse and dragging it.) Ex: ![83870-example.jpg][1] [1]: /answers/storage/attachments/83870-example.jpg Thank You! My Shearing Codes: Color WriteColor; Bitmap ExImage; ExImage= new Bitmap(pictureBox1.Image); int ImageWidth = ExImage.Width; int ImageHeight = ExImage.Height; double Bendingcoef = 0.5; double x2 = 0,y2 = 0; for (int x1 = 0; x1<(ImageWidth ); x1++) { for (int y1 = 0; y1<(ImageHeight ); y1++) { WriteColor = ExImage.GetPixel(x1, y1); //+X Direction Axis //x2 = x1 + Bendingcoef y1; //y2 = y1; // -X Direction of Axis //x2 = x1 - Bendingcoef y1; //y2 = y1; //+Y Direction Axis //x2 = x1; //y2 = Bendingcoef x1 + y1; //-Y Direction Axis x2 = x1; y2 = -Bendingcoef x1 + y1; if (x2 > 0 && x2 < ImageWidth && y2 >0 && y2<ImageHeight) ExImage.SetPixel((int)x2,(int)y2, WriteColor); } } pictureBox1.Image = ExImage;

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered DanielZhang-MSFT edited

Try handling three events:

 bool IsShearing = false;
 Point OriginalCoordinates;
    
 private void pictureBox1_MouseDown( object sender, MouseEventArgs e )
 {
     IsShearing = true;
     OriginalCoordinates = e.Location;
 }
    
 private void pictureBox1_MouseMove( object sender, MouseEventArgs e )
 {
     if( IsShearing )
     {
         Point current_coordinates = e.Location;
         Size movement = new Size( current_coordinates.X - OriginalCoordinates.X, current_coordinates.Y - OriginalCoordinates.Y );
    
         // make the new image based on 'movement' value
         // ...
    
     }
    
 }
    
 private void pictureBox1_MouseUp( object sender, MouseEventArgs e )
 {
     if( IsShearing )
     {
         IsShearing = false;
         // do some final operations, if any
         // ...
     }
 }

You can also detect the clicked button and the keys like <Ctrl> or <Shift>, if needed.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks.I started to understand its logic..These are my shearing codes ( Top) .How can I make compatible with these? I tried to make it compatible with the codes but i cant.

0 Votes 0 ·

Hi,
we can't see your "[83870-example.jpg]", please attach it again.
And re-edit you code by using the following tab.
84445-45.png
First understand the use of events such as MouseMove, and what error did you encounter when you code compatible.
Best Regards,
Daniel Zhang


0 Votes 0 ·
45.png (4.0 KiB)