按下按钮后,只有在鼠标点击后才能从图片框中获取像素属性c# [英] After button was pressed, get pixel properties from a picturebox only after mouse was clicked c#

查看:410
本文介绍了按下按钮后,只有在鼠标点击后才能从图片框中获取像素属性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()函数中,我想单击图片框中的像素,并获得它的颜色。

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

问题是我不知道如何使click_the_object()功能记录只有一个点击,第一个。我尝试使用事件处理程序,但它进入一个循环。

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.

推荐答案

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

这篇关于按下按钮后,只有在鼠标点击后才能从图片框中获取像素属性c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆