`

wpf - Draw Windows Forms controls

    博客分类:
  • WPF
wpf 
阅读更多

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;
    }
 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics