2012-02-09 72 views
3

我需要得到一個截圖在內存中下載WPF Webrowser Control的頁面。如何獲取WPF Webrowser控件的屏幕截圖?

基本要求是WPF Webrowser被隱藏,甚至沒有在XAML中實現。

可以嗎?如果是,那麼如何?

-------- DRAFT解決方案-----------

var topLeftCorner = MainBrowser.PointToScreen(new System.Windows.Point(0, 0)); 
var topLeftGdiPoint = new System.Drawing.Point((int)topLeftCorner.X, (int)topLeftCorner.Y); 
var size = new System.Drawing.Size((int)MainBrowser.ActualWidth, (int)MainBrowser.ActualHeight); 

Bitmap screenShot = new Bitmap((int)MainBrowser.ActualWidth, (int)MainBrowser.ActualHeight); 

using (var graphics = Graphics.FromImage(screenShot)) 
{ 
    graphics.CopyFromScreen(topLeftGdiPoint, new System.Drawing.Point(), 
     size, CopyPixelOperation.SourceCopy); 
} 

screenShot.Save(@"D:\Temp\screenshot.png"); 
+0

只是一個隨機的想法,我不知道是去工作,如果顯示的web瀏覽器,但在屏幕的笑。 (我不知道該怎麼做,但我想知道怎麼做) – 2012-02-09 20:05:44

+1

隱藏/摺疊的元素不能作爲圖形複製到剪貼板內存中......你的屏幕視頻緩衝區沒有渲染它,所以你如何期待它被複制? – 2012-02-10 10:55:33

回答

1

作爲一種理念,你可以使用安裝在您的PC和打印​​作爲一個虛擬打印機圖片。

PrintDialog p = new PrintDialog(); 
p.PrintVisual(webBrowser1,"webBrowser1"); 
3
var topLeftCorner = MainBrowser.PointToScreen(new System.Drawing.Point(0, 0)); 
var topLeftGdiPoint = new System.Drawing.Point((int)topLeftCorner.X, (int)topLeftCorner.Y); 
var size = new System.Drawing.Size((int)MainBrowser.ActualWidth, (int)MainBrowser.ActualHeight); 
Bitmap screenShot = new Bitmap((int)MainBrowser.ActualWidth, (int)MainBrowser.ActualHeight); 
using (var graphics = Graphics.FromImage(screenShot)) 
{ 
    graphics.CopyFromScreen(topLeftGdiPoint, new System.Drawing.Point(), size, CopyPixelOperation.SourceCopy); 
} 
screenShot.Save(@"D:\Temp\screenshot.png");