我需要在内存中获得下载WPF浏览器控件的页面的屏幕截图。
基本要求是WPF浏览器是隐藏的,甚至没有在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:Tempscreenshot.png");
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:Tempscreenshot.png");
作为一种想法,您可以使用安装在PC上的虚拟打印机并以图像形式打印。
PrintDialog p = new PrintDialog();
p.PrintVisual(webBrowser1,"webBrowser1");