在c#中点击按钮绘制矩形

本文关键字:绘制 按钮 | 更新日期: 2023-09-27 18:01:39

我有一个透明背景的表单。我在表单上有一个按钮。当我点击按钮时,会截取透明区域的截图,并对截图进行分析,找出某一参考图像,如果找到该图像,则在参考图像周围绘制一个矩形。现在我按下按钮什么也没发生。我使用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();
    }

在c#中点击按钮绘制矩形

尝试将绘图逻辑移动到Paint事件

让我们假设你在一个叫做pnl的面板上画画。试试以下命令:

在构造函数中注册到paint事件:

 this.pnl.Paint += pnl_Paint;

单击时,设置标志,表示需要对矩形进行绘制:

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

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

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