After button was pressed, get pixel properties fro

2019-09-19 03:14发布

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条回答
姐就是有狂的资本
2楼-- · 2019-09-19 03:21
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));
}
查看更多
登录 后发表回答