使用WebBrowser将html的特定部分转换为图像

时间:2016-07-06 07:12:20

标签: c#

<html>
  <head>
    <!-- something -->
  </head>
  <body>
    <!-- some tags -->
    <ifram>
      <html>
        <head>
          <!-- something -->
        </head>
        <body>
          <!-- some tags -->
            <table>
            </table>
          <!-- some tags -->
        </body>
      </html>
    </ifram>  
    <!-- some tags -->
  </body>
</html>

我找到了将html的整个内容转换为图像的解决方案,但我想要它的一个特定部分。在我的html页面中有一个表元素,我想将表转换为图像,我不想要保留元素。在我找到的代码下面,有一种方法可以为我的目的修改代码吗?

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

0 个答案:

没有答案