3

I am using C# WPF and I'm trying to render a 2D view from a 3D model that I could use as a preview image for my ui items.

To accomplish that I'm trying to use properly this class:

RenderTargetBitmap Class(https://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap(v=vs.110).aspx)

This is how I try to use it :

Irrelevant since update

public void SetModelImage()
    {
        RenderTargetBitmap bmp = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Pbgra32);
        PngBitmapEncoder png = new PngBitmapEncoder();
        Viewport3D myViewport3d = new Viewport3D();
        ModelVisual3D myModelVisual3D = new ModelVisual3D();
        Model3DGroup myModel3DGroup = new Model3DGroup();
        GeometryModel3D myGeometryModel = new GeometryModel3D();
        this.previewModel = new PngBitmapEncoder();
        PerspectiveCamera myPCamera = new PerspectiveCamera();

        myModel3DGroup.Children.Add(this.content);// ajoute le modele
        myModelVisual3D.Content = myModel3DGroup;
        myModelVisual3D.Content = this.content;
        myViewport3d.Children.Add(myModelVisual3D);
        bmp.Render(myViewport3d);

        this.previewModel.Frames.Add(BitmapFrame.Create(bmp));

        string filepath = "C:\\Users\\Remi\\Desktop\\toto.png";
        using (Stream stm = File.Create(filepath))
        {

            this.previewModel.Save(stm);

        }

    }

The problem is that the generated Bitpmap is totaly black.

Full file pastbin: http://paste.isomorphis.me/jgz

Thanks for your time ! :)

UPDATE: Thanks to an advice I've corrected the way I was initializing the layout with measure arrange and InvalidateVisual but I still have a black bitmap generated. enter image description here

Folder
  • 139
  • 2
  • 10

1 Answers1

1

You have to run an initial layout of the Viewport3D element by calling its Measure and Arrange methods. You may also need to call InvalidateVisual.

myViewport3d.Children.Add(myModelVisual3D);
myViewport3d.Measure(new Size(200, 200));
myViewport3d.Arrange(new Rect(0, 0, 200, 200));
myViewport3d.InvalidateVisual();
bmp.Render(myViewport3d);
Clemens
  • 123,504
  • 12
  • 155
  • 268