保存WPF网页浏览器的屏幕截图Frame
问题描述:
我有一个Frame
元素在我的WPF应用程序中显示一个html页面,并希望将Frame的屏幕截图保存为图像。保存WPF网页浏览器的屏幕截图Frame
随着谷歌的帮助下,我有这样的代码:
Size size = new Size(PreviewFrame.ActualWidth, PreviewFrame.ActualHeight);
PreviewFrame.Measure(size);
PreviewFrame.Arrange(new Rect(size));
var renderBitmap = new RenderTargetBitmap(
(int)size.Width,
(int)size.Height,
96d,
96d,
PixelFormats.Pbgra32);
renderBitmap.Render(PreviewFrame);
但所有我曾经得到的是一个空白图像。
有关如何解决这段代码和/或以另一种方式捕捉网页作为我的应用中的图像的想法?
答
原来的GDI Graphics
类有一个CopyFromScreen
方法行之有效,并捕获Frame
的内容:
var topLeftCorner = PreviewFrame.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)PreviewFrame.ActualWidth, (int)PreviewFrame.ActualHeight);
var screenShot = new Bitmap((int)PreviewFrame.ActualWidth, (int)PreviewFrame.ActualHeight);
using (var graphics = Graphics.FromImage(screenShot)) {
graphics.CopyFromScreen(topLeftGdiPoint, new System.Drawing.Point(),
size, CopyPixelOperation.SourceCopy);
}
screenShot.Save(@"C:\screenshot.png", ImageFormat.Png);
CopyFromScreen不DPI知道,所以如果你的用户是在DPI运行的100多设置其他%(96dpi)结果图像不正确。更现代的RenderTargetBitmap应该解决这个问题,但似乎不适用于WPF WebBrowser。 – phoff 2012-02-01 21:13:27
今天有什么解决方案可以让WPF WebBrowser正确渲染截图图像,而不依赖于使用哪种DPI设置? – Nuts 2014-09-21 21:43:59