wpf - Draw Windows Forms controls

Following the discussion on this topic that I previous published:  Convert from System.Drawing.Icon to System.Media.ImageSource and vice versa , which we discussed the possibility to convert a System.Windows.Forms.Icon into WPF classes such as System.Media.ImageSource;

 

While there also exist some requirement to do the bridge for the classes such as Forms, or any other specific classes descendant from the System.Windows.Forms.Control class.  One of such scenario exists when you say want to print the Visual of the Windows form control.

 

 

Fortunately the similar techinque exist for the Forms controls, because like the ICon class, all Form class has a DrawToBitMap method  to draw the Control to a bit map, with which you can grab/grip/snatch and make a call to the System.Windows.Interop.Imaging.GetBitmapSourceFromHBitmap(Handle,..); 

 

Please read for more information about the DrawToBitmap method, 

 

Below show one example that use this very similar technique but on the general Forms controls.

 

 

 

 

    public static ImageSource CreateImageSource(System.Windows.Forms.Control control, double width, double height)
    {
      System.Drawing.Bitmap b = new System.Drawing.Bitmap((int) width, (int) height);
      IntPtr bmpHandle = IntPtr.Zero;

      try
      {
        control.DrawToBitmap(b, new System.Drawing.Rectangle(0, 0, (int)width, (int)height);
        var imageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
          b.GetHbitmap(),
          IntPtr.Zero,
          Int32Rect.Empty, // Int32Rect.Empty to indicate all sourceRect to to draw?
          System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
        return imageSource;
      }
      finally
      {
        if (bmpHandle != IntPtr.Zero)
        {
          NativeMethods.DeleteObject(bmpHandle);
        }
      }
    }
 

 

With the returned ImageSource, you can do a lot of things, including creating a ImageBrush, with wich you can paint to the DrawingContext.DrawRectangle or something. e.g. are 

 

 

 

public static DrawingVisual CreateDrawingImage(System.Windows.Media.ImageSource imageSource, double width, double height)
    {
      var drawingVisual = new DrawingVisual();
      // you can create the ImageBrush 
      //var imageBrush = new ImageBrush(imageSource);
      // open the Render of the DrawingVisual
      using (var dc = drawingVisual.RenderOpen())
      {
        var rectangle = new Rect
        {
          X = 0,
          Y = 0,
          Width = width,
          Height = height,
        };

        // draw the white background
        dc.DrawRectangle(Brushes.White, null, rectangle);
        // if you create the ImageBrush, you can pain with the brush 
        //dc.DrawRectangle(imageBrush, null, rectangle);
        dc.DrawImage(imageSource, rectangle);
      }

      return drawingVisual;
    }
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值