在C#中单击按钮绘制矩形 [英] Draw rectangle on button click in C#
问题描述
我的背景透明.我在表格上有一个按钮.当我单击按钮时,将截取透明区域的屏幕快照,并对屏幕快照进行分析以获取特定的参考图像,如果找到该图像,则应在参考图像周围绘制一个矩形.现在,当我按下按钮时什么也没有发生.我正在使用BotSuite Dll,在此处提供: http://www.botsuite.net/.我的按钮单击代码如下:
I have form in which background is transparent. I have a button on the form. When I click the button, screenshot of the transparent area is taken and the screenshot is analyzed for a certain reference-image, and if the image is found, a rectangle should be drawn around the reference-image. For now nothing happens when I press the button. I'm using BotSuite Dll, provided here: http://www.botsuite.net/. I My code for the button click is as follows:
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();
}
推荐答案
Try moving the drawing logic to the Paint
evet.
让我们假设您正在一个名为pnl
的面板上绘画.请尝试以下操作:
Let's assume you are drawing on a panel called pnl
. Try the following:
在构造函数上注册到绘画绘画专家:
On your constructor register to the paint evet:
this.pnl.Paint += pnl_Paint;
在单击时,设置一个标志以指示矩形需要绘画:
Upon a click, set a flag to indicate that painting is required for the rectangle:
bool _paintRect;
private void button1_Click(object sender, EventArgs e)
{
this._paintRect = true;
Invalidate();
Refresh();
}
在paint事件处理程序中,进行实际的绘制:
In the paint event handler, do the actual 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));
}
这篇关于在C#中单击按钮绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!