Using RenderTargetBitmap on WindowsFormsHost
.net, c#, winforms, wpf
Solution
A Windows Forms control also knows how to render itself, you don't have to jump through the screen capture hoop. Make it look like this:
using (var bmp = new System.Drawing.Bitmap(basePanel.Width, basePanel.Height)) {
basePanel.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height));
bmp.Save(@"c:\temp\test.png");
}
Problem
I have a set of controls inside a `WindowsFormsHost` and I would like to capture the current view and just save it as an image, I however only get some `Panel` visible in the Image. Is it possible to use the WindowsFormsHost as a "Visual" and capture the wrapped controls? See my example: ``` <WindowsFormsHost x:Name="windowHost"> <wf:Panel Dock="Fill" x:Name="basePanel"/> </WindowsFormsHost> ``` If i were to add a Button or whatever to the `basePanel` this wouldn't be visible when exporting to a PNG with the following code: ``` RenderTargetBitmap rtb = new RenderTargetBitmap(basePanel.Width, basePanel.Height, 96, 96, PixelFormats.Pbgra32); rtb.Render(windowHost); PngBitmapEncoder pnge = new PngBitmapEncoder(); pnge.Frames.Add(BitmapFrame.Create(rtb)); Stream stream = File.Create("test.jpg"); pnge.Save(stream); stream.Close(); ``` Suggestions on why this might not work and maybe a possible work-around? I guess it's not really suppose to work this way, but one could really hope!