C# How can I find the mouse scroll coordinates?

Anonymous
2021-04-02T03:51:47.633+00:00

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]: /api/attachments/83870-example.jpg?platform=QnA 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;

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,235 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 112.1K Reputation points
    2021-04-02T08:28:08.003+00:00

    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.