1

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

4

1 回答 1

3

资料来源:达林·季米特洛夫的回答

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

还有一些其他选项,请参阅以下链接:
Html table (text) to image using C#
How to convert a block of html to an image (eg jpg) in asp.net
Convert a HTML Control (Div or Table) to an image使用 C#
渲染 HTML(转换为位图)

代码片段:

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;
}
于 2012-10-31T06:09:36.587 回答