开发者

C# detect where user clicked on a image/Bitmap

开发者 https://www.devze.com 2023-03-13 17:18 出处:网络
Is it possible to detected where user clicked on a loaded image or a bitmap on a C# form ? . just need it to be independent to 开发者_如何学运维image not screen locations !Well, you don\'t tell us how

Is it possible to detected where user clicked on a loaded image or a bitmap on a C# form ? . just need it to be independent to 开发者_如何学运维image not screen locations !


Well, you don't tell us how you are displaying the image, so I am forced to guess. I am assuming a picturebox displaying the image at it's native resolution. So, in that case:

class MyForm : Form
{
    public MyForm()
    {
        picturebox1.MouseDown += picturebox1_MouseDown;       
    }

    private void picturebox1_MouseDown( object sender, MouseEventArgs e )
    {
        if( (e.Button & MouseButtons.Left) == MouseButtons.Left )
        {
            var imagePos = e.Location; // that's it
        }
    }
}

If your image is scaled you will need to do the math. Get the Width and Height of the control and figure out the ratio between them and the dimensions of your image. Multiply the click position by that ratio.

0

精彩评论

暂无评论...
验证码 换一张
取 消