Search code examples
c#imagemouseeventpictureboxpixel

mouse position (Cursor.Position)


I have image 10*10 mult it in factor and loading the image to picturebox I have problem to select the mouse position when I clicking in everywhere over the image the point showing in the corner Upper-left what is the error??


Solution

  • To draw into a zoomed Image of a PictureBox this works fine for me:

    float zoom = 7.5f;
    
    public Form1()
    {
        InitializeComponent();
        pb1.SizeMode = PictureBoxSizeMode.Zoom;
        pb1.ClientSize = new Size((int) (pb1.Image.Size.Width  * zoom), 
                                  (int) (pb1.Image.Size.Height * zoom) );
    }
    
    
    
    private void pb1_MouseClick(object sender, MouseEventArgs e)
    {
       int x = (int)Math.Round(e.X / zoom) ;
       int y = (int)Math.Round(e.Y / zoom) ;
       Bitmap bmp = (Bitmap) pb1.Image;
       bmp.SetPixel(x, y, Color.Red);
       pb1.Refresh();
    }
    

    Note: This works equally well if the PictureBox is sitting in an AutoScroll Panel.

    enter image description hereenter image description here