I have a WPF panel (in a WPF application) that contains a collection of Winform and WPF elements. The Winform elements are hosted through a WindowsFormsHost
.
I need to create a BitmapSource of the entire panel (exactly as it is), so I can manipulate it/save it/print it out.
It is not a problem to create a BitmapSource of the WPF elements but the Winform elements are not rendered (there is just a white space where they should be).
One example of that:
void GetBitmapSource(FrameworkElement element)
{
var matrix = PresentationSource.FromVisual(element).CompositionTarget.TransformToDevice;
double dpiX = 96.0 * matrix.M11;
double dpiY = 96.0 * matrix.M22;
var bitmapSource = new RenderTargetBitmap((int)element.ActualWidth + 1, (int)element.ActualHeight + 1, dpiX, dpiY, PixelFormats.Pbgra32);
bitmapSource.Render(element);
return bitmapSource;
}
This will print WPF elements just fine but ignore Winform content. How do I include that?
The image below shows a Tabpanel on the left and BitmapSource on the right. Notice how the content of the Winform Rectangle is empty.