Z-Order hack for WinForms interop controls in WPF

If you ever tried to put something from WPF world over Windows Forms controls (interop), you, probably, found it impossible. After it, while searching internet, you found AirSpace article, that clearly describes the reasons and rules. Digging more will bring you to the blog of Jeremiah Morrill, who fighting with AirSpace issue about a year. But this can not help you (and especially to your angry client). From one hand, it’s too hard to tell your client: "Sorry, we do now support it". From the other hand, you do not want to mess with complicated 3rd party controls with fair performance. What to do? Scrap it with GDI+. Here the example of scrapping WebBrowser control with it’s internal function DrawToBitmap(). We can incorporate it into BitmapSource solution and have nice graphics instead of static control, when you need it. Just collapse WindowsFormsHost and Show Image control with graphical representation of  your Win32 control

  BitmapSource GetScreen()
     { 

 Bitmap bm = new Bitmap(wb.ClientRectangle.Width, wb.ClientRectangle.Height);
     wb.DrawToBitmap(bm, wb.Bounds); 

 BitmapSource src = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bm.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
     System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
     src.Freeze(); 

 bm.Dispose(); 

 bm = null;
     return src; 

 }

Wait a second… Why it takes a while to capture the browser screen? Let’s see what Control.DrawToBitmap() method doing.

  public void DrawToBitmap(Bitmap bitmap, Rectangle targetBounds)
     { 

 int width = Math.Min(this.Width, targetBounds.Width);
     int height = Math.Min(this.Height, targetBounds.Height);
     Bitmap image = new Bitmap(width, height, bitmap.PixelFormat);
     using (Graphics graphics = Graphics.FromImage(image))
     { 

 IntPtr hdc = graphics.GetHdc();
     using (Graphics graphics2 = Graphics.FromImage(bitmap))
     { 

 IntPtr handle = graphics2.GetHdc();
     BitBlt(new HandleRef(graphics2, handle), targetBounds.X, targetBounds.Y, width, height, new HandleRef(graphics, hdc), 0, 0, 0xcc0020);
     graphics2.ReleaseHdcInternal(handle); 

 } 

 graphics.ReleaseHdcInternal(hdc); 

 } 

 } 

 

 

Well, why to repaint graphics with BitBlit? Really, I can not see any reason of it. Let’s just copy handler

  BitmapSource GetScreenInt()
     { 

 Bitmap bm = new Bitmap(wb.ClientRectangle.Width, wb.ClientRectangle.Height);
     Graphics g = Graphics.FromImage(bm);
     PrintWindow(wb.Handle, g.GetHdc(), 0); 

 g.ReleaseHdc(); 

 g.Flush(); 

 BitmapSource src = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bm.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty,
     System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
     src.Freeze(); 

 bm.Dispose(); 

 bm = null;
     return src; 

 } 

 

 

Now, the operation takes less, then 0.3 seconds. Pretty good, isn’t it? The only thing, you’ll need is unmanaged signature of PrintWindow. Here is comes

  [DllImport("user32.dll", SetLastError=true)]
     static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags); 

 

 

Have a nice day

4 Responses to “Z-Order hack for WinForms interop controls in WPF”

  1. Rick Says:

    Great stuff, thanks a ton for this Tamir.

  2. metin2 , hack , tradehack Says:

    I was suggested this blog by means of my cousin. I’m not certain whether this post is written by means of him as no one else understand such precise about my problem. You’re amazing! Thank you!

  3. Chris Says:

    Hi,

    Thanks for a solution to this annoying problem. I am trying to use it to update an image source from a winforms webcam component. I am using a timer (at 60 times per second) which calls the method GetScreenInt() but I quicly run out of memory. Is there a better way of doing what I want?

  4. Isadora Virgel Says:

    I have fun with, lead to I discovered exactly what I was taking a look for. You have ended my 4 day long hunt! God Bless you man. Have a great day. Bye

Leave a Reply

Recommended

 


Sponsor


Partners

WPF Disciples
Dreamhost
Code Project
Switched to Better Place

Together