wpf Save a image using DrawingImage() and workaround the WebBrowser drawing issu

本文介绍如何使用WPF中的DrawingVisual类来创建视觉元素,并特别关注解决WebBrowser控件无法正常渲染的问题。通过使用DrawingImage替代VisualBrush,实现了WebBrowser内容的有效捕获。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WPF has a class that is called System.Windows.Media.DrawingVisual where you can pass a System.Windows.Controls.Visual to it and it is supposed draw/render the visual as it is on the screen to some place in the memory. 

 

generaly you can have the following code to create a drawing visual .

 

 

    public static DrawingVisual CreateDrawingVisual(FrameworkElement visual, double width, double height)
    {
      var drawingVisual = new DrawingVisual();
      // open the Render of the DrawingVisual
      using (var dc = drawingVisual.RenderOpen())
      {
        var vb = new VisualBrush(visual) { Stretch = Stretch.None };
        var rectangle = new Rect
        {
          X = 0,
          Y = 0,
          Width = width,
          Height = height,
        };

        // draw the white background
        dc.DrawRectangle(Brushes.White, null, rectangle);
        // draw the visual
        dc.DrawRectangle(vb, null, rectangle);
      }
      return drawingVisual;
    }

 

 

With the code above, you are create a drawing visual with the same size as the size of the element that you passed in. Also, it create some background for the drawing visual.

 

This all works well except that it does not work for the type WebBrowser. It does not fail, but you won't see anything if try to display the drawing visual.

 

 

There is a post on the web telling that "Save as Image using DrawingImage() in WPF". Basically the code that the author proposed is like this; 

 

 

/*
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
*/
 
// Creates thumbnail image form the webbrowser control in wpf
// Or Thumbnail of web pages image created can be easily loaded in any
// image control..
 
public static string GetThumbnailImage(WebBrowser CurrentBrowser)
        {
            Guid guid = Guid.NewGuid();
            string ThumbnailPath =  @"E:\" + guid.ToString() + ".png";
            Image imgScreen = new Image();
            imgScreen.Width = 120;
            imgScreen.Height = 100;
            imgScreen.Source = new DrawingImage(VisualTreeHelper.GetDrawing(CurrentBrowser));
 
            FileStream stream = new FileStream(ThumbnailPath, FileMode.Create);
 
            DrawingVisual vis = new DrawingVisual();
            DrawingContext cont = vis.RenderOpen();
            cont.DrawImage(imgScreen.Source, new Rect(new Size(120d, 100d)));
            cont.Close();
 
            RenderTargetBitmap rtb = new RenderTargetBitmap((int)imgScreen.Width,
                (int)imgScreen.Height, 96d, 96d, PixelFormats.Default);
            rtb.Render(vis);
 
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(rtb));
            encoder.Save(stream);
            stream.Close();
 
            return ThumbnailPath;
        }
 

While If you adop the code above and you will probably do the following 

 

public static DrawingVisual CreateDrawingImage(Visual visual, double width, double height)
    {
      var drawingVisual = new DrawingVisual();
      // open the Render of the DrawingVisual
      using (var dc = drawingVisual.RenderOpen())
      {
        var drawingImage = new DrawingImage(VisualTreeHelper.GetDrawing(visual));
        var rectangle = new Rect
        {
          X = 0,
          Y = 0,
          Width = width,
          Height = height,
        };

        // draw the white background
        dc.DrawRectangle(Brushes.White, null, rectangle);
        // draw the visual
        //NOTE:
        // instead of Creating one VisualBrush, it create and use one DrawingImage
        // and use that DrawingImage , because VisualBrush has issue with WebBrowser
        // 
        dc.DrawImage(drawingImage, rectangle);
      }

      return drawingVisual;
    }
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值