问题
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