渲染到jpg / png WPF控件和包含WinForms控件的WindowsFormHost

时间:2016-10-24 14:17:08

标签: c# .net wpf winforms

我有一个包含 WindowsFormsHost 控件的WPF表单。

有意添加此 WindowsFormsHost 控件以同时添加ActiveX / WinForms控件。

最后,我必须拍摄包含 WindowsFormsHost 控件的表单,这是问题所在。
我已经找到了如何将wpf(this way)保存为jpg并将winforms保存为jpg(here)。

来自WPF:
formGrid是WPF格式的基础网格

RenderTargetBitmap rtb = new RenderTargetBitmap((int)formGrid.ActualWidth, (int)formGrid.ActualHeight, 96, 96, PixelFormats.Pbgra32);
rtb.Render(formGrid);

JpegBitmapEncoder jpg = new JpegBitmapEncoder();
jpg.Frames.Add(BitmapFrame.Create(rtb));

using (Stream stm = File.Create(@"c:\testwpf.jpg"))
{
     jpg.Save(stm);
}

来自WinForms:
formhost是WindowsFormHost控件

using (var bmp = new System.Drawing.Bitmap((int)formhost.Width, (int)formhost.Height))
{
    formhost.Panel.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height));
    bmp.Save(@"c:\testwinform.jpg") 
}

我想要的是混合/合并,如果可能的话,将两个渲染都放到一个单独的图像中。

PD:我尝试从Bitmap创建一个BitmapSource并将其添加到JpegBitmapEncoder但我没有成功的结果。

0 个答案:

没有答案