Product and Development Musings

Persisting Annotations: Saving and Branding

19. February 2010 11:43

We recently learned how the Viewer control manages annotation marks during runtime.  Now let's continue our discussion in our series of articles on annotation support in ImageMan.Net Document Edition by highlighting how to persist annotated images after you are done working with them. 

Saving Annotations

The annotation information on an image can be saved in TIFF, JPG or PNG formats. This will happen automatically when an image in the Viewer control has associated annotation information. The annotation information is stored as a separate layer of the image.

So, if you want to share or continue working with an annotated image save it as a TIFF, JPG or PNG file.  If you want to load an image with the annotation information in the proprietary ImageMan.Net format in another TIFF viewer the annotation information will not be displayed.  (In an upcoming post, we will learn how to create annotated images with a common set of annotations that can be shared with other TIFF viewers.)

We'll now add the ability to save our annotations in the sample application we built in the last few posts (Visual Studio 2008 source code: C# source, VB.Net source).

  • Open IMAnnotate1.sln for C# (or IMAnnotate1VB.sln fo VB.Net) in Visual Studio 2005 or 2008 (which ever you used last time)
  • Open Form1 class in design view
  • Drag and drop a SaveFileDialog control onto the form:

Anno2

  • Add a new menu item under the File menu item and call it Save.:

Anno1

  • Automatically add events to this submenu item by double-clicking to let Visual Studio autogenerate the event method.
  • Now, add this code to open the Save File dialog:

[C#]

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
    saveFileDialog1.Filter = RegisteredEncoders.GetFilterString();
 
    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
        annotation1.SaveInWangFormat = false;
        viewer1.Save(saveFileDialog1.FileName);
    }
}

[VB.Net]

Private Sub SaveToolStripMenuItem_Click( _
    ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles SaveToolStripMenuItem.Click
 
    SaveFileDialog1.Filter = RegisteredEncoders.GetFilterString()
 
    If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
        Annotation1.SaveInWangFormat = False
        Viewer1.Save(SaveFileDialog1.FileName.Trim())
    End If
End Sub

First we generate a list of available file types to allow the user to use and set that as the filter for the SaveFileDialog control.  Then, if the user selected a valid filename and location, we save the image using the given filename.  Notice the SaveInWangFormat property of the Annotation object? Let's hold off on that until the next topic.

  • So now run the application (by clicking F5, for instance)
  • Add a few annotation marks onto the default (white) image
  • Save that image to a known location as either a TIFF, JPG or PNG file type
  • Close the application
  • Run it again
  • Open the previously saved image
  • You should now still be able to move, edit and otherwise manipulate the previous marks

Note: If you open the file in the Windows Picture and Fax Viewer, for instance, you will not see the annotated marks.

Branding Annotations

If you need to permanently add the annotation information to the image you can use the annotation objects Brand() method to burn the annotations onto the image. Once you do this, however, you can no longer manipulate the annotations as before.

Branding is done at the image's color depth. So if you have color marks and a black and white image. Either increase the color depth of the underlying image, or understand that the color branded marks will appear differently.

  • Add another menu item on the toolstrip (under the File menu item) and call it Brand

Anno4

  • Create the code behind and add the following code:

[C#]

private void brandToolStripMenuItem_Click(object sender, EventArgs e)
{
    // Brand the current image
    annotation1.Brand(viewer1.Images.CurrentImage);
 
    // Clear all existing marks
    annotation1.Marks.Clear();
 
    // Refresh the viewer
    viewer1.Refresh();
}

[VB.Net] 

Private Sub BrandToolStripMenuItem_Click( _
    ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles BrandToolStripMenuItem.Click
 
    ' Brand the current image
    Annotation1.Brand(Viewer1.Images.CurrentImage)
 
    ' Clear all existing marks
    Annotation1.Marks.Clear()
 
    ' Refresh the viewer
    Viewer1.Refresh()
End Sub
  • Run the application
  • Add a few annotation marks onto the default (white) image
  • Brand the image using the Brand menu option
  • Now try to manipulate the current marks and notice that you can't (they aren't marks now; they are part of the image)
  • Add some new marks
  • Save that image to a known location as either a TIFF, JPG or PNG file type
  • Close the application
  • Run it again
  • Open the previously saved image
  • You should now still be able to move, edit and otherwise manipulate the new marks but the branded marks are not editable any longer

Save this project since we will continue to build on it next time for a discussion on persisting common annotations that are compatible with other TIFF viewer and (many) annotation editors.

Log in