Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

screenshot - Draw rectangle on button click in C#

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();
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try moving the drawing logic to the Paint evet.

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...