从WPF的WebBrowser控件保存图像 - 你是如何做到的?

Luc*_*uck 5 wpf wpf-controls

大家.可能有一个简单的解决方案,但我似乎找不到一个.我正在使用Visual Studio 2010附带的WPF中的WebBrowser控件,并尝试以编程方式将可能出现在网页上的图像保存到磁盘.

提前谢谢了!运气

luv*_*ere 6

添加System.Drawing为参考并在应捕获图像的方法中执行以下操作:

Rect bounds = VisualTreeHelper.GetDescendantBounds(browser1);

System.Windows.Point p0 = browser1.PointToScreen(bounds.TopLeft);
System.Drawing.Point p1 = new System.Drawing.Point((int)p0.X, (int)p0.Y);

Bitmap image = new Bitmap((int)bounds.Width, (int)bounds.Height);
Graphics imgGraphics = Graphics.FromImage(image);

imgGraphics.CopyFromScreen(p1.X, p1.Y,
                           0, 0,
                           new System.Drawing.Size((int)bounds.Width,
                                                        (int)bounds.Height));

image.Save("C:\\a.bmp", ImageFormat.Bmp);
Run Code Online (Sandbox Code Playgroud)