簡體   English   中英

按下按鈕后,只有在單擊鼠標后才能從圖片框中獲取像素屬性c#

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

我有以下代碼:

    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();
        }
    }

在click_the_object()函數中,我想點擊圖片框中的一個像素並獲得它的顏色。

獲取像素屬性的函數是:

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

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

問題是我不知道如何使click_the_object()函數記錄只有一次點擊,第一次點擊。 我嘗試使用eventhandler,但它進入循環。

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));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM