在WindowsFormsHost上使用RenderTargetBitmap [英] Using RenderTargetBitmap on WindowsFormsHost
问题描述
我在 WindowsFormsHost
内有一组控件,我想捕获当前视图并将其保存为图像,但是我只得到了一些面板
在图像中可见。
是否可以将WindowsFormsHost用作可视并捕获包装的控件? / p>
请参阅我的示例:
< WindowsFormsHost x:Name = windowHost>
< wf:Panel Dock =填充 x:Name = basePanel />
< / WindowsFormsHost>
如果我要向 basePanel添加按钮或其他内容
使用以下代码导出到PNG时将不可见:
RenderTargetBitmap rtb = new RenderTargetBitmap( basePanel.Width,
basePanel.Height,96,96,PixelFormats.Pbgra32);
rtb.Render(windowHost);
PngBitmapEncoder pnge =新的PngBitmapEncoder();
pnge.Frames.Add(BitmapFrame.Create(rtb));
流stream = File.Create( test.jpg);
pnge.Save(stream);
stream.Close();
关于为什么这可能不起作用以及可能的解决方法的建议?我想这并不是真的应该以这种方式工作,但确实可以希望!
Windows窗体控件也知道如何渲染自身,则不必跳过屏幕捕捉框。使其看起来像这样:
使用(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);
}
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!
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");
}
这篇关于在WindowsFormsHost上使用RenderTargetBitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!