Save WebBrowser control sceenshot while not in ful

2019-07-22 03:31发布

问题:

I get the screenshot of a webpage contained inside a WebBrowser control just as described here and here, but I have a problem: if the WebBrowser control is not at full screen, the screenshot gets cropped to the size of the control.

Is it possible to save a screenshot of the whole page if the WebBrowser control is not at full screen?

This is the code I'm currently using to save the screenshot:

private void SaveBrowserScreenshot(WebBrowser browser, string path, string name)
{
    const int width = 1024;
    const int height = 768;
    const string extension = ".png";

    using (var bitmap = new Bitmap(width, height))
    {
        var rect = new Rectangle(0, 0, width, height);
        browser.DrawToBitmap(bitmap, rect);

        using (var image = bitmap)
        {
            using (var graphic = Graphics.FromImage(image))
            {
                graphic.CompositingQuality = CompositingQuality.HighQuality;
                graphic.SmoothingMode = SmoothingMode.HighQuality;
                graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;

                image.Save(String.Concat(path, "\\", name, extension), ImageFormat.Png);
            }
        }
    }
}

Performance is not an issue since this will never be used in production.

回答1:

Could you make a button that when it takes the snapshot it takes the current URL and puts that into a new WeBrowser object which is then expanded to a much bigger size so when it takes the screenshot it's full size?



回答2:

Since my WebBrowser control was hidden, I was able to fix this by simply resizing it. If the control isn't hidden, it will get re-sized while the screenshot in being saved.

private void SaveBrowserScreenshot(WebBrowser browser, string path, string name)
{
    const string extension = ".png";

    int width = Math.Max(1024, Screen.PrimaryScreen.Bounds.Width);
    int height = Math.Max(768, Screen.PrimaryScreen.Bounds.Height);
    int originalWidth = browser.Width;
    int originalHeight = browser.Heignt;

    browser.Width = width;
    browser.Heigt = height;

    using (var bitmap = new Bitmap(width, height))
    {
        var rect = new Rectangle(0, 0, width, height);
        browser.DrawToBitmap(bitmap, rect);

        using (var image = bitmap)
        {
            using (var graphic = Graphics.FromImage(image))
            {
                graphic.CompositingQuality = CompositingQuality.HighQuality;
                graphic.SmoothingMode = SmoothingMode.HighQuality;
                graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;

                image.Save(String.Concat(path, "\\", name, extension), ImageFormat.Png);
            }
        }
    }

    browser.Width = originalWidth;
    browser.Height = originalHeight;
}

As an alternative, you could create another WebBrowser control, hide it and re-size it as willDaBeast suggested, but this will have some performance implications.



回答3:

Unfortunately this is not possible, as a "ScreenShot" records the visible items on the screen at that time

Maybe you could look into saving the HTML and rendering that as an image instead?

EDIT:

This link could help with my suggestion:

How do I save a web page to image