在C#按钮上绘制矩形

时间:2014-05-04 08:48:09

标签: c# screenshot image-recognition botsuite.net

我的表格背景是透明的。我在表格上有一个按钮。单击按钮时,将拍摄透明区域的屏幕截图,并分析屏幕截图以查找特定参考图像,如果找到图像,则应在参考图像周围绘制一个矩形。现在按下按钮时没有任何反应。我正在使用BotSuite Dll,这里提供:http://www.botsuite.net/。我点击按钮的代码如下:

    private void button1_Click(object sender, EventArgs e)
    {
        Invalidate();
        //take screenshot of transparent form area
        Bitmap CapturedScreen = ScreenShot.Create((this.Left + 8), (this.Top + 30), 780, 415);
        ImageData refpic = new ImageData("pallo.bmp");
        ImageData source = new ImageData(CapturedScreen);
        Graphics graphics = this.CreateGraphics();
        Pen p = new Pen(Color.Black, 1);
        graphics.DrawRectangle(p, Template.Image(source, refpic, 100));
        Refresh();
    }

1 个答案:

答案 0 :(得分:3)

尝试将绘图逻辑移动到Paint evet。

假设您正在使用名为pnl的面板。请尝试以下方法:

在你的构造函数注册到paint evet:

 this.pnl.Paint += pnl_Paint;

单击后,设置一个标志以指示矩形需要绘画:

bool _paintRect;

private void button1_Click(object sender, EventArgs e)
{
    this._paintRect = true;
    Invalidate();        
    Refresh();
}

在paint事件处理程序中,执行实际绘制:

private void pnl_Paint(object sender, PaintEventArgs e)
{
   //take screenshot of transparent form area
    Bitmap CapturedScreen = ScreenShot.Create((this.Left + 8), (this.Top + 30), 780, 415);
    ImageData refpic = new ImageData("pallo.bmp");
    ImageData source = new ImageData(CapturedScreen);

    Graphics graphics = e.Graphics;

    Pen p = new Pen(Color.Black, 1);
    graphics.DrawRectangle(p, Template.Image(source, refpic, 100));
}