在asp.net中截图

时间:2013-04-13 22:15:34

标签: c# javascript asp.net html screenshot

我想在asp.net中截取Div Element的截图并将其保存到磁盘。我不想使用html2canvas库或html5 canvas元素。方法可以是服务器端或客户端。

1 个答案:

答案 0 :(得分: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;
}

http://pietschsoft.com/post/2008/07/c-generate-webpage-thumbmail-screenshot-image.aspx