【问题标题】:Are there any free ways to turn an html page into an image with .net是否有任何免费的方法可以将 html 页面转换为 .net 的图像
【发布时间】:2010-09-13 21:50:18
【问题描述】:

我想获取 html,包括文本和图像,并将其转换为包含所有内容的图像。有免费的方法吗?

这是使用 .net 3.5。

另见:

Server Generated web screenshots?
What is the best way to create a web page thumbnail?

【问题讨论】:

    标签: .net html rendering screenshot webpage


    【解决方案1】:

    您可以查看this projectthis page

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      这是我几周前在博客上发布的一些代码:

      C#: Generate WebPage Thumbnail Screenshot Image

      我还将在下面发布它的代码:

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

      以下是一些示例用法:

      // Generate thumbnail of a webpage at 1024x768 resolution
      Bitmap thumbnail = GenerateScreenshot("http://pietschsoft.com", 1024, 768);
      
      // Generate thumbnail of a webpage at the webpage's full size (height and width)
      thumbnail = GenerateScreenshot("http://pietschsoft.com");
      
      // Display Thumbnail in PictureBox control
      pictureBox1.Image = thumbnail;
      
      /*
      // Save Thumbnail to a File
      thumbnail.Save("thumbnail.png", System.Drawing.Imaging.ImageFormat.Png);
      */
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-13
        • 1970-01-01
        • 1970-01-01
        • 2016-06-07
        • 1970-01-01
        • 2012-03-04
        • 2012-02-20
        • 1970-01-01
        相关资源
        最近更新 更多