gpt4 book ai didi

.net - 保存 WPF 网页浏览器 Frame 的截图

转载 作者:行者123 更新时间:2023-12-04 00:24:14 26 4
gpt4 key购买 nike

我有一个 Frame元素在我的 WPF 应用程序中显示一个 html 页面,并且想要将 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类(class)有 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);

关于.net - 保存 WPF 网页浏览器 Frame 的截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3884966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com