Save a jpg image of my scene
#4
The "Cannot render to bitmap because TargetViewport3D is null" is thrown because the TargetViewport3D on the camera is not set. 

If you do not manually set the TargetViewport3D, then the code in the camera class tries to find the Viewport3D in the Loaded event. It is recommended to manually set the TargetViewport3D - this can be done in code or in XAML with using binding (for example: TargetViewport3D="{Binding ElementName=MainViewport}").

For a complete sample please check the RenderOfflineViewportButton_OnClick method in the RenderToBitmapSample sample in Ab3d.PowerToys samples project.


To make a simple demonstration on how to create a sequence of bitmaps with changes the camera and object, you can add the following code to the end of the RenderOfflineViewportButton_OnClick method:


Code:
           for (int i = 1; i < 5; i++)
           {
               targetPositionCamera.Heading = i * 20;
               boxVisual3D.Transform = new ScaleTransform3D(1, (double)i / 4, 1);

               var bitmap = RenderToBitmap(targetPositionCamera);

               using (var fileStream = new FileStream($"c:\\temp\\bitmap_{i}.png", FileMode.Create))
               {
                   BitmapEncoder encoder = new PngBitmapEncoder();
                   encoder.Frames.Add(BitmapFrame.Create(bitmap));
                   encoder.Save(fileStream);
               }
           }

You will also need to update the RenderToBitmap to return BitmapSource object (and you can also comment calling the SaveBitmap method).

Note that this method renders does not render a teapot that is shown in the sample application but a custom 3D scene where the Viewport3D and its objects are created in code.
Andrej Benedik
  


Messages In This Thread
Save a jpg image of my scene - by z8195aa - 04-19-2019, 04:17 AM
RE: Save a jpg image of my scene - by abenedik - 04-19-2019, 08:35 AM
RE: Save a jpg image of my scene - by z8195aa - 05-12-2019, 03:55 AM
RE: Save a jpg image of my scene - by abenedik - 05-13-2019, 11:51 AM
RE: Save a jpg image of my scene - by z8195aa - 06-03-2019, 01:15 AM
RE: Save a jpg image of my scene - by abenedik - 06-03-2019, 10:26 AM
RE: Save a jpg image of my scene - by z8195aa - 06-03-2019, 10:56 PM

Forum Jump:


Users browsing this thread:
1 Guest(s)