Navigation:  I Want To... > Imaging >

Add a vector graphic

Previous pageReturn to chapter overviewNext page

Use Case

You want to add a vector-based graphic overlay to an image.

Relevant Architecture

Graphics
Vector Graphics

Relevant Types

VectorGraphic
IVectorGraphic
CompositeGraphic
LinePrimitive
BoundableGraphic
IBoundableGraphic
RectanglePrimitive
EllipsePrimitive
ArcPrimitive
PointPrimitive
IArcGraphic
InvariantBoundablePrimitive
InvariantRectanglePrimitive
InvariantEllipsePrimitive
InvariantArcPrimitive

Sample Code

using System.Drawing;

using ClearCanvas.Common;

using ClearCanvas.Desktop;

using ClearCanvas.Desktop.Actions;

using ClearCanvas.ImageViewer;

using ClearCanvas.ImageViewer.BaseTools;

using ClearCanvas.ImageViewer.Graphics;

 

// ... (other using namespace statements here)

 

namespace MyPlugin.Imaging

{

   [ButtonAction("apply""global-toolbars/ToolbarStandard/AddVectorGraphicTool""Apply")]

   [IconSet("apply", IconScheme.Colour, "Icons.MyToolSmall.png""Icons.MyToolMedium.png""Icons.MyToolLarge.png")]

   // ... (other action attributes here)

   [ExtensionOf(typeof (ImageViewerToolExtensionPoint))]

   public class AddVectorGraphicTool : ImageViewerTool

   {

      public void Apply()

      {

         if (this.SelectedOverlayGraphicsProvider == null)

            return;

 

         // Create a composite graphic

         CompositeGraphic composite = new CompositeGraphic();

 

         // Create a rectangle

         RectanglePrimitive rect = new RectanglePrimitive();

         rect.TopLeft = new PointF(510);

         rect.BottomRight = new PointF(2025);

 

         // Create a line

         LinePrimitive line = new LinePrimitive();

         line.Point1 = new PointF(525);

         line.Point2 = new PointF(2010);

 

         // Add the rectangle and line to the composite

         composite.Graphics.Add(rect);

         composite.Graphics.Add(line);

 

         // Add the composite to the selected presentation image's scene graph      

         this.SelectedOverlayGraphicsProvider.OverlayGraphics.Add(composite);

 

         // Now render

         this.SelectedPresentationImage.Draw();

      }

   }

}

Remarks

Adding a static (i.e., non-interactive) vector overlay can be achieved by simply creating the appropriate VectorGraphics and adding them to the scene graph.  If you want to control, say, the visibility of a group of VectorGraphics, add them to a CompositeGraphic, then add the CompositeGraphic to the scene graph.  In the example above, a rectangle and line are added to a CompositeGraphic, and thus can be manipulated as a group.

Examples in Code Base

No examples of a static vector overlay, but ClearCanvas.ImageViewer.Graphics.PolylineGraphic shows how a graphic can be built up from a number of VectorGraphics.