Navigation:  I Want To... > Imaging >

Manipulate a PresentationImage from a tool

Previous pageReturn to chapter overviewNext page

Use Case

You want to manipulate some property of a PresentationImage, such as the orientation, or window/level from your own custom Tool.

Relevant Architecture

Presentation Images
Spatial Transformations

Relevant Types

IPresentationImage
IImageGraphicProvider
IImageSopProvider
ISpatialTransformProvider
IVoiLutLinearProvider
etc.

Sample Code

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/ApplyMySpatialTransformTool""Apply")]

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

   // ... (other action attributes here)

   [ExtensionOf(typeof (ImageViewerToolExtensionPoint))]

   public class SpatialTransformTool : ImageViewerTool

   {

      public void Apply()

      {

         if (this.SelectedPresentationImage == null)

            return;

 

         ISpatialTransformProvider provider = this.SelectedPresentationImage as ISpatialTransformProvider;

 

         if (provider == null)

            return;

 

         ISpatialTransform transform = provider.SpatialTransform;

 

         // Do something with the transform object, such as...

         transform.FlipX = true;

 

         // update the image onscreen

         this.SelectedPresentationImage.Draw();

      }

   }

}

Remarks

Because of the convenience property SelectedSpatialTransformProvider that is available on ImageViewerTool, you would never need to write code like in the sample above. However, we do it this way to illustrate how we always manipulate the presentation image through an IXXXProvider interface.  You should never call methods on PresentationImage or its subclasses directly because that ties your tool to a particular implementation. For example, if you wrote a zoom tool that applies a zoom factor to a subclass of PresentationImage called MyPresentationImage, the tool would only work with MyPresentationImage.  It would not work with say, YourPresentationImage.  However, if MyPresentationImage and YourPresentationImage both implemented ISpatialTransformProvider, and your tool consumed that interface instead, it would work with both types of PresentationImage.

When you're done your manipulation, be sure to call Draw() on the presentation image.  Or, if you were manipulating a graphic that belongs to the presentation image's scene graph, call Draw() on it.  When to redraw the presentation image is always left to you, the tool writer. This is because redrawing can be an expensive operation, and thus should not be blindly left to the Framework to do whenever a property of the presentation image changes.

In summary, the best practices are:

Always access presentation images through an IXXXProvider interface.
Perform all your manipulations on the presentation image first, then redraw once.

Examples in Code Base

ClearCanvas.ImageViewer.Tools.Standard.WindowLevelTool
ClearCanvas.ImageViewer.Tools.Standard.RotateLeftTool