After button was pressed, get pixel properties from a picturebox only after mouse was clicked c#

心不动则不痛 提交于 2019-12-11 19:43:27

问题


I have the following code :

    private void Calculate_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Please click the object in the image ", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) == DialogResult.OK)
        {
            click_the_object();
        }
    }

In the click_the_object() function I want to click a pixel in the picturebox and get it's color.

The function for getting the pixel properties is :

    private Color culoarepixel(Point point)
    {
        Bitmap bitmap = (Bitmap)pbOriginal.Image;

        return bitmap.GetPixel(point.X, point.Y);
    }

The problem is that I don't know how to make the click_the_object() function record only one click, the first one. I tried using eventhandlers, but it enters a loop.


回答1:


public Form1()
{
    InitializeComponent();
    this.myPictureBox.BackColor = Color.Red;
}

private void startButton_Click(object sender, EventArgs e)
{
    if (MessageBox.Show(
        "Please click the object in the image ",
        "", 
        MessageBoxButtons.OKCancel, 
        MessageBoxIcon.Exclamation, 
        MessageBoxDefaultButton.Button1) == DialogResult.OK)
    {
        this.myPictureBox.MouseClick += this.myPictureBox_MouseClick;
    }
}

void myPictureBox_MouseClick(object sender, MouseEventArgs e)
{
    this.myPictureBox.MouseClick -= myPictureBox_MouseClick;
    var point = new Point(e.X, e.Y);
    MessageBox.Show(string.Format("You've selected a pixel with coordinates: {0}:{1}", point.X, point.Y));
}


来源:https://stackoverflow.com/questions/20311607/after-button-was-pressed-get-pixel-properties-from-a-picturebox-only-after-mous

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!