2010-09-30 67 views
1

我抓住从下面的链接一些代码,以生成从Web浏览器控制的屏幕截图: http://pietschsoft.com/post/2008/07/C-Generate-WebPage-Thumbmail-Screenshot-Image.aspx生成截图从web浏览器控件

然后,我修饰成像的扩展方法,以便:

public static Bitmap GetScreenshot(this WebBrowser webBrowser) 
     { 
      // Load the webpage into a WebBrowser control 
      using (WebBrowser wb = new WebBrowser()) 
      { 
       wb.ScrollBarsEnabled = false; 
       wb.ScriptErrorsSuppressed = true; 
       wb.Navigate(webBrowser.Url); 
       while (wb.ReadyState != WebBrowserReadyState.Complete) 
       { 
        Application.DoEvents(); 
       } 

       // Set the size of the WebBrowser control 
       wb.Width = wb.Document.Body.ScrollRectangle.Width; 
       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)); 

       return bitmap; 
      } 
     } 

它适用于大多数网页,但我试图将其用于SharePoint网站,并且图像每次都以250x250出现。 :-s

这里可以给我一个解决方案的任何天才吗?

在此先感谢

回答

0

那么,这样做的原因似乎是,我的新web浏览器(WB)未初始化即使我打电话导航()。 Url属性显示为空。这很奇怪。这是否意味着我无法在代码中创建WebBrowser并且从不显示它?如果要展示这个东西,那将是一件耻辱。 :-(

+0

好没关系,这种理论......我被厚厚的...这是null,因为DocumentCompleted没有被称为然而,在任何情况下,我仍然有问题,大多数时间也都是空白的白色图像。 – Matt 2010-10-11 08:24:44

0

其一,DrawToBitmap类不WebBrowser控件下存在的。我建议使用CopyFromScreen这里是一个代码片段:

Bitmap b = new Bitmap(wb.ClientSize.Width, webBrowser1.ClientSize.Height); 
    Graphics g = Graphics.FromImage(b); 
    g.CopyFromScreen(this.PointToScreen(wb.Location), new Point(0, 0), wb.ClientSize); 
    //The bitmap is ready. Do whatever you please with it! 
    b.Save(@"c:\screenshot.jpg", ImageFormat.Jpeg); 
    MessageBox.Show("Screen Shot Saved!");