问题
I'm trying to generate screenshot from helix control but only in code behind - why this not work? Something is missing?
private void generate() {
ModelImporter importer = new ModelImporter();
Model3D model = importer.Load(@"c:\test\test.obj");
Model3DGroup group = new Model3DGroup();
group.Children.Add(model);
ModelVisual3D myModelVisual3D = new ModelVisual3D();
myModelVisual3D.Content = group;
HelixViewport3D viewer = new HelixViewport3D();
viewer.Children.Add(myModelVisual3D);
Dispatcher.BeginInvoke(new Action(() =>
{
RenderTargetBitmap bmp = new RenderTargetBitmap(1024, 768, 96, 96, PixelFormats.Pbgra32);
bmp.Render(viewer);
PngBitmapEncoder png = new PngBitmapEncoder();
BitmapFrame frame = BitmapFrame.Create(bmp);
png.Frames.Add(frame);
using (Stream stm = File.Create(@"c:\test\test.png"))
{
png.Save(stm);
} }
), DispatcherPriority.ContextIdle, null);
}
Edit:
No exceptions - nothing - code seams to be working but PNG is empty. If I swap HelixViewport3D viewer
with control that is in XAML then PNG is generated ok. With HelixViewport3D defined in code rendering is not working and PNG is empty - size is ok - it is PNG but no content - transparent when I display it.
In this test.obj file is nothing special - when I use XAML defined HelixViewport3D - all is ok.
来源:https://stackoverflow.com/questions/63683745/why-rendering-image-in-code-behind-do-not-work