将HTML表格内容更改为图像格式

时间:2012-10-31 05:43:46

标签: c# asp.net

我想了解一下我的任务。我想将我的HTML表格内容代码更改为图像格式。我确实知道了。任何人都可以给我一个想法..

1 个答案:

答案 0 :(得分:3)

来源:Darin Dimitrov's Answer

  

首先需要一个能够处理HTML和HTML的渲染引擎   可选择javascript和css(如果你想支持它们)。   使用WebBrowser控件可以完成,但可能会有   更好的方式。

其他选项也很少,请参阅以下链接:
Html table (text) to image using C#
How to convert block of html to an image (e.g. jpg) in asp.net
Convert a HTML Control (Div or Table) to an image using C#
render HTML (convert to bitmap)

代码段:

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