Click here to Skip to main content
15,886,519 members
Articles / Multimedia / GDI+
Article

The Annotated Image Control in 2006

Rate me:
Please Sign up or sign in to vote.
4.28/5 (9 votes)
27 Jun 2006LGPL33 min read 93.7K   1.5K   56   19
An article on the Annotated Image control in VS 2005
Screenshot - sRGBToolbox.jpg

Introduction

It's been about two years since I updated my Annotated Image control and an overhaul is long overdue: removal of a plethora of bugs, migration to VS2005, architectural changes, speed improvements, extra functionality, etc. For those who haven't read my other articles on previous incarnations of this control, here is briefly what it will do:

  • Display an image and zoom it
  • Display a mask in overlay
  • Display annotations in overlay
  • Create annotations interactively and save/load them
  • Edit annotations: move annotation, move and delete points...

Background

My older articles on this subject might still help.

Using the Code

The control should be added to another project and a bitmap assigned to it. Annotations and masks can then be added/loaded/created and added to their respective collections.

VB
Public Sub New() 
    MyBase.New() 
    InitializeComponent() 

    'Add the custom control 
    Me._annotatedImage = New AnnotatedImage
    Me.Controls.Add(Me.AnnotatedImage) 

    'Make sure image form is maximized 
    'so we can measure the maximal clientsize 
    Me.SuspendLayout() 
    Me.WindowState = FormWindowState.Normal 
    Dim rectScreen As Rectangle = _
            Screen.PrimaryScreen.WorkingArea 
    Dim objCurrentSize As Size = Me.Size 
    Me.Width = rectScreen.Width 
    'Dunno why, but we need to subtract 20 
    Me.Height = rectScreen.Height - 40
    Me.AnnotatedImage.MaximimumSize = Me.ClientSize 
    Me.ClientSize = objCurrentSize 

        'Load an image, e.g. from a command line argument
        Me.AnnotatedImage.LoadBitmap(new URI("C:\temp\anImage.jpg"))

    'Show annotations manager and add some menus 
    Me.AnnotatedImage.AddContextMenuItem("Measure", _
        AnnotatedImage.ContextMenus.Annotation, _
        New System.EventHandler(AddressOf _
        Me.MeasureAnnotationEventHandler)) 
    Me.AnnotatedImage.AddContextMenuItem("Measure", _
        AnnotatedImage.ContextMenus.AnnotationContainer, _
        New System.EventHandler(AddressOf _
        Me.MeasureAnnotationEventHandler)) 
    Me.AnnotatedImage.AddContextMenuItem("Morphology", _
        AnnotatedImage.ContextMenus.Mask, _
        New System.EventHandler(AddressOf _
        Me.MaskMorpholgyEventHandler)) 
    Me.AnnotatedImage.AddContextMenuItem("Save", _
        AnnotatedImage.ContextMenus.Mask, _
        New System.EventHandler(AddressOf Me.MaskSaveEventHandler)) 

    Me.ResumeLayout()
End Sub

The annotated image control communicates with its host using a series of events (see also the downloadable help file). The events list:

Screenshot - Events.png

Event handlers:

VB
Private Sub _annotatedImage_ItemSelected(ByVal sender _
    As Object) Handles _annotatedImage.ItemSelected
    ...
End Sub

Private Sub _annotatedImage_Updated(ByVal sender _
    As Object) Handles _annotatedImage.Updated
    ...
End Sub

Private Sub AnnotatedImageResize(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles _annotatedImage.Resize
    'Resize parent form to user control. Causes refresh ....
    Debug.WriteLine("Received resize event from AnnotatedImage," & _ 
                    " resizing client area", "AnnotatedsRGBImageResize")
    Dim iControlWidth As Integer = AnnotatedImage.Width
    Dim iControlHeight As Integer = AnnotatedImage.Height
    AnnotatedImage.Location = New Point(0, 0)
    Me.stbImage.Location = New Point(0, iControlHeight)
    Me.stbImage.Width = iControlWidth
    Me.ClientSize = New Size(Math.Max(iControlWidth, 600), _
        iControlHeight + stbImage.Height)
End Sub

Private Sub annotatedImage_BitmapChanged(ByVal sender As Object) _
    Handles _annotatedImage.BitmapChanged
    ...
End Sub
...

It is very important to turn off redrawing while performing extensive updates on the collections of the annotatedImage control by setting the Updating property to True. When resetting this property to False, the Updated event will be fired, allowing the host application to perform necessary updates. Also, avoid reacting to other events when the Updating property is still True, unless absolutely necessary!

An example of not handling events during updating of the internal collections is a control drawing a treeview of these collections:

VB
Private Sub annotatedImage_CollectionsChanged(ByVal sender As Object, _
    ByVal e As CollectionChangedEventArgs) _ 
    Handles annotatedImage.MaskCollectionChanged, _
    annotatedImage.AnnotationCollectionChanged, _ 
    annotatedImage.AnnotationContainerCollectionChanged

    If Me.annotatedImage.Updating = False Then
        InvokeSynchronizeTree()
End Sub

Key Bindings, Interactive Creation and Manipulation

Some of the default key bindings are:

  • Mouse wheel, Z, Shift-Z: Zoom
  • Middle mouse button drag: Move the image when it is larger than the desktop. If it is smaller and the zoom is smaller than 1, it will bring up a magnifying glass.

The annotated image control has bindings for interactive creation and manipulation of annotations. Right-clicking brings up a context-sensitive menu that allows the creation of annotations and annotation containers.

Screenshot - StartAnnotationCreation.jpg

When editing an annotation, the default action is selecting an annotation. You can then move it by dragging it or you can delete it using the delete key. Double-clicking will launch its properties dialog. If you use the Shift key during selection, the whole annotation will be selected, if you use the Control key, only the points can be selected.

Image 4

Image 5

Image 6

Demo

The included sRGBViewer application is a simple viewer that allows creation and editing of annotations, and will save them. Note that the images can actually reside (and be saved back) on a website. This is actually how I use it: as a smart client for a web-based medical picture album system.

Screenshot - sRGBViewer.jpg

Points of Interest

It really is about time the GDI+ implementation used hardware acceleration, as announced years ago, and this is now a reality in Vista. I haven't tried it yet, though. The implementation of Regions is still terribly slow when testing repeatedly whether a point is inside it (hit testing).

History

Version of June 27, 2006: First versions of this control date from 2003, but the articles related to the older versions are no longer relevant due to fundamental changes.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior)
Belgium Belgium
Physicist, Biomedical Engineer, Phd in engineering. Specific expertise is in medical photography and it's related image processing, and in colourimetry.

Comments and Discussions

 
QuestionHow to extend this framework? Pin
romias27-Oct-10 14:57
romias27-Oct-10 14:57 
AnswerRe: How to extend this framework? Pin
yvdh27-Oct-10 21:36
yvdh27-Oct-10 21:36 
GeneralRe: How to extend this framework? [modified] Pin
romias28-Oct-10 5:23
romias28-Oct-10 5:23 
GeneralRe: How to extend this framework? Pin
yvdh1-Nov-10 6:21
yvdh1-Nov-10 6:21 
GeneralLatest Version Pin
coolpjmartin29-Jun-09 22:18
coolpjmartin29-Jun-09 22:18 
GeneralLatest version at Codeplex Pin
yvdh17-Jun-07 7:30
yvdh17-Jun-07 7:30 
GeneralRe: Latest version at Codeplex [modified] Pin
yvdh23-Apr-09 2:04
yvdh23-Apr-09 2:04 
GeneralRe: Latest version at Codeplex Pin
aarrabi7616-Jun-09 23:56
aarrabi7616-Jun-09 23:56 
GeneralSelection Info Pin
amb980012-Jun-07 6:11
amb980012-Jun-07 6:11 
GeneralRe: Selection Info Pin
yvdh12-Jun-07 13:53
yvdh12-Jun-07 13:53 
GeneralRe: Selection Info Pin
amb980020-Jun-07 16:18
amb980020-Jun-07 16:18 
GeneralRe: Selection Info Pin
yvdh20-Jun-07 22:16
yvdh20-Jun-07 22:16 
GeneralNOT COMPLETE!! Pin
Luca Crisi, MCP11-May-07 1:02
Luca Crisi, MCP11-May-07 1:02 
GeneralRe: NOT COMPLETE!! Pin
yvdh11-May-07 2:21
yvdh11-May-07 2:21 
GeneralRe: NOT COMPLETE!! Pin
Luca Crisi, MCP11-May-07 2:47
Luca Crisi, MCP11-May-07 2:47 
GeneralUse in C# Pin
Reanalyse12-Jun-05 12:53
Reanalyse12-Jun-05 12:53 
GeneralRe: Use in C# Pin
yvdh12-Jun-05 22:55
yvdh12-Jun-05 22:55 
There should be no problem using the control in C# (indeed, some parts are even in C# as I wrote some image processing routines that use pointers). The code you present cannot work, as the AnnotatedImage control has its own Bitmap property which you need to set, and which will display the image and its overlaying annotations. Check out the sRGBViewer application for an example.

Good luck

GeneralAWESOME! Pin
onyak3-Dec-04 8:58
onyak3-Dec-04 8:58 
GeneralGreat, this just comes in time Pin
Marc Sommer2-Dec-04 21:28
Marc Sommer2-Dec-04 21:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.