DrawtoBitmap with my own created RhinoViewport - c# RhinoCommon



It’s possible to draw own created RhinoViewport with DisplayPipeline.DrawToBitmap method to Bitmap?
With standard Rhino Viewports DrawToBitmap works perfect, but when I create my own viewport then DrawToBitmap return null bitmap.

RhinoViewport rviewport = new RhinoViewport();
Bitmap vportBitmap = DisplayPipeline.DrawToBitmap(rviewport,200,200);

Generating custom thumbnails
(Dale Fugier) #2

Hi Marcin,

Can you explain a little bit more details about what you want to do and why? What problem are you trying to solve?


– Dale


I want to generate a preview Image of i.e MeshObject, something like Preview in MaterialEditor.
I draws the object on Viewport through DisplayConduit:

if(Viewport.Id == MyViewport.Id)

The problem is that when I call the method DrawToBitmap (MyViewport) then return null Bitmap.


I found a simple solution (maybe not elegant) just simply create a InstanceDefinition from object, then create preview with InstanceDefinition.CreatePreviewBitmap. After create bitmap simply purge InstanceDefinition from InstanceDefinitionTable.

  List<Rhino.DocObjects.ObjectAttributes> objattrs = new List<Rhino.DocObjects.ObjectAttributes>();
            List<Rhino.Geometry.GeometryBase> geometries = new List<Rhino.Geometry.GeometryBase>();

            Rhino.DocObjects.ObjectAttributes objattr = new Rhino.DocObjects.ObjectAttributes();
            objattr.ObjectColor = System.Drawing.Color.Red;
            geometries.Add((Rhino.Geometry.GeometryBase) new Rhino.Geometry.Torus(Rhino.Geometry.Plane.WorldXY,100,PenSize.Value.Value).ToNurbsSurface());

            int idefindex = Rhino.RhinoDoc.ActiveDoc.InstanceDefinitions.Add("for preview", "preview image", new Rhino.Geometry.Point3d(0, 0, 0),geometries, objattrs);

        previewBitmap = `Rhino.RhinoDoc.ActiveDoc.InstanceDefinitions[idefindex].CreatePreviewBitmap(DefinedViewportProjection.Perspective, Rhino.DocObjects.DisplayMode.RenderPreview, new System.Drawing.Size(200,200));`