2010-04-20 38 views
3

我有一個顯示一些銀色燈光控件的網頁。我需要以編程方式截取此網頁的屏幕截圖。以網頁截圖(使用Silverlight控件)編程式

當前使用System.Windows.Forms.WebBrowser控件進行屏幕截圖。

Forms.WebBrowser在正常頁面截圖時正常工作。但對於使用Silverlight控件的頁面無效。

我的屏幕截圖代碼如下: 位圖bitmap = null;使用(WebBrowser webBrowser = new WebBrowser()) { webBrowser.ScrollBarsEnabled = false; webBrowser.ScriptErrorsSuppressed = true;

  // Set the size of the WebBrowser control 
      webBrowser.Width = width; 
      webBrowser.Height = height; 

      // Load the webpage into a WebBrowser control 
      webBrowser.Navigate(url); 
      while (webBrowser.ReadyState != WebBrowserReadyState.Complete) 
      { 
       Application.DoEvents(); 
      } 

      if (width == -1) 
      { 
       // Take Screenshot of the web pages full width 
       webBrowser.Width = webBrowser.Document.Body.ScrollRectangle.Width; 
      } 

      if (height == -1) 
      { 
       // Take Screenshot of the web pages full height 
       webBrowser.Height = webBrowser.Document.Body.ScrollRectangle.Height; 

      } 

      // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control 
      bitmap = new Bitmap(webBrowser.Width, webBrowser.Height); 
      webBrowser.DrawToBitmap(bitmap, new Rectangle(0, 0, webBrowser.Width, webBrowser.Height)); 

}

+0

您是否找到答案?因爲我面臨同樣的問題。 – NLV 2011-05-10 17:21:41

回答

0

我用這個代碼,以網頁的schreenshot:Web瀏覽器控件的

string myPicsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + @"\SchreenshotFolder\"; 
    if (System.IO.Directory.Exists(myPicsPath)) 
    { 
     Rectangle bounds = yourBrowser.Bounds; 
     using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height)) 
     { 
     using (Graphics g = Graphics.FromImage(bitmap)) 
     { 
      g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size); 
     } 
     bitmap.Save(myPicsPath + System.IO.Path.GetRandomFileName() + ".png", System.Drawing.Imaging.ImageFormat.Png); 
     } 
    } 
    else 
    { 
     System.IO.Directory.CreateDirectory(myPicsPath); 
     MessageBox.Show("Screenshot directory created in " + Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + @"\" + " named SchreenshotFolder\nScreenshot is not saved. Hit button again to save it."); 
    } 

結構界限包含你所需要採取截圖的一切。 或者,如果您想將其作爲縮略圖使用,則可以減小位圖的大小。