Prod.: Engine, ver.: 6, ID: 60001534, HowTo : How can I produce a bitmap of a specific area of my drawing

HowTo : How can I produce a bitmap of a specific area of my drawing

Article60001534
TypeHowTo
ProductEngine
Version6
Date Added11/9/2011
Submitted byLuca Mancusi [Blumatica]
Keywords

Subject

How can I produce a bitmap of a specific area of my drawing

Summary

I need to produce a bitmap of a specific area of my drawing that is defined by a polyline. How can I do this ?

Solution

The code below that does the following
1) Get the polyline that you want to clip the drawing
2) Create a layout
3) Create a viewport and add it to the layout
4) Set the clipping object of the viewport and its properties so the ciewport clips exactly the drawing as we want
5) RenderToGraphics the layout that contains only the viewport and the desired part clipped of the drawing.
6) Show the Image in a pictureBox object of visual studio

Please check the code below and adjust it to needs.
vdDocument doc = vdFramedControl1.BaseControl.ActiveDocument;
//Get the polyline with a method , I just take the last entity in my example where I know it is the clipping polyline.
vdPolyline poly = doc.Model.Entities[vdFramedControl1.BaseControl.ActiveDocument.Model.Entities.Count - 1] as vdPolyline;

//Create a Layout and Add a viewport
// vdLayout lay = new vdLayout(doc, "temp"); // you don't need to add the layout to the document.
vdLayout lay = new vdLayout();
lay.SetUnregisterDocument(doc);
lay.SetDocumentDefaults();
lay.DisableShowPrinterPaper = true;
lay.BkColorEx = doc.Model.BkColorEx;
vdViewport vport = new vdViewport(doc);
vport.ClipObj = poly;

//Set the viewport to show what we want.
vport.ViewSize = poly.BoundingBox.Height;
vport.ViewCenter = poly.BoundingBox.MidPoint;
lay.Entities.AddItem(vport);
lay.ZoomExtents();

//RenderToGraphics the layout that has only this viewport
Bitmap bmp = null;
bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics gr = Graphics.FromImage(bmp);
lay.RenderToGraphics(gr, null, bmp.Width, bmp.Height);
gr.Dispose();

//Show the Image in a pictureBox for debuging.
pictureBox1.Image = bmp;
pictureBox1.Update();