Link to home
Create AccountLog in
Avatar of DrowningTrout
DrowningTrout

asked on

C# ScreenShot of Webbrowser

How do I take a screenshot of only my webbrowser1 that's within my C# program, I can successfully take a screenshot of the whole screen but not just the browser.

And how can I save the image as a Monochrome bitmap? (like in MS Paint)

Also, If I end up wanting to take a screenshot of the webbrowser while its minimized could I move it off the screen, un-minimize then take screenshot, move back?
bmpScreenshot = new Bitmap(webBrowser1.Bounds.Width, webBrowser1.Bounds.Height, PixelFormat.Format32bppRgb);
            gfxScreenshot = Graphics.FromImage(bmpScreenshot);
            gfxScreenshot.CopyFromScreen(webBrowser1.Bounds.X, webBrowser1.Bounds.Y, webBrowser1.Bounds.X, webBrowser1.Bounds.Y, webBrowser1.Bounds.Size, CopyPixelOperation.SourceCopy);
            bmpScreenshot.Save("Test.bmp", ImageFormat.Bmp);

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

It is generally accepted with E-E to only ask one question.  What is your most important question?
Avatar of DrowningTrout
DrowningTrout

ASKER

"How do I take a screenshot of only my webbrowser1 that's within my C# program, I can successfully take a screenshot of the whole screen but not just the browser."

Sorry about that... The above is my most important question.
I believe that you need to use the WebBrowser.DrawToBitmap method.

Proof-of-concept:

C#: Generate WebPage Thumbmail Screenshot Image
http://pietschsoft.com/post/2008/07/C-Generate-WebPage-Thumbmail-Screenshot-Image.aspx
public Bitmap GenerateScreenshot(string url)
{
    // This method gets a screenshot of the webpage
    // rendered at its full size (height and width)
    return GenerateScreenshot(url, -1, -1);
}
 
public Bitmap GenerateScreenshot(string url, int width, int height)
{
    // Load the webpage into a WebBrowser control
    WebBrowser wb = new WebBrowser();
    wb.ScrollBarsEnabled = false;
    wb.ScriptErrorsSuppressed = true;
    wb.Navigate(url);
    while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }
 
 
    // Set the size of the WebBrowser control
    wb.Width = width;
    wb.Height = height;
 
    if (width == -1)
    {
        // Take Screenshot of the web pages full width
        wb.Width = wb.Document.Body.ScrollRectangle.Width;
    }
 
    if (height == -1)
    {
        // Take Screenshot of the web pages full height
        wb.Height = wb.Document.Body.ScrollRectangle.Height;
    }
 
    // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
    Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
    wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
    wb.Dispose();
 
    return bitmap;
} 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of amar31282
amar31282
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Any updates @DrowningTrout.

Regards,
Amarjit
THANK YOU!
This project was put on hold and I have been busy, sorry for the late response, this helped me tremendously!