Why rendering image in code behind do not work?

≯℡__Kan透↙ 提交于 2021-01-07 03:33:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!