如何获取WPF Webbrowser Control的屏幕截图?

时间:2012-02-09 18:24:20

标签: c# .net wpf

我需要获取页面的屏幕截图,以便在内存中下载WPF Webrowser Control。

基本要求是WPF Webrowser被隐藏,甚至没有在XAML中实现。

有可能吗?如果是,那么如何?

--------草案解决方案-----------

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");

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分:1)

您可以使用安装在PC上的虚拟打印机并以图像形式打印。

PrintDialog p = new PrintDialog();
p.PrintVisual(webBrowser1,"webBrowser1");