←Select platform

SaveToTag Method

Summary

Saves the annotation objects in a container as a TIFF tag data.

Syntax

C#
VB
C++
public byte[] SaveToTag( 
   AnnContainer container, 
   AnnCodecsTagFormat format 
) 
  
Public Function SaveToTag( _ 
   ByVal container As Leadtools.Windows.Annotations.AnnContainer, _ 
   ByVal format As Leadtools.Windows.Annotations.AnnCodecsTagFormat _ 
) As Byte() 

Parameters

container
The AnnContainer containing the objects to be saved.

format
An AnnCodecsTagFormat value that specifies the format used when creating the tag data.

Return Value

A byte array that can be saved into a TIFF file using System.Windows.Media.Imaging.BitmapMetadata.SetQuery(System.String,System.Object) method.

Remarks

This method will return byte array that can be saved to Tiff file using System.Windows.Media.Imaging.BitmapMetadata.SetQuery(System.String,System.Object) method passing the query '/ifd/{uint=32932}'.

This method can return null (Nothing in VB) if you try to save unsupported objects to certain formats (for example, if you try to save an AnnRubberStampObject to a Wang format).

For more information, refer to WPF Annotation Files.

Example

C#
VB
using Leadtools.Windows.Controls; 
using Leadtools.Windows.Annotations; 
using Leadtools.Demos; 
using Leadtools.Help; 
 
public void AnnCodecs_SaveToTag(ImageViewer viewer, string tifFileName) 
{ 
   // first create a container and add a few objects 
   AnnContainer container = new AnnContainer(); 
   container.Width = 400; 
   container.Height = 400; 
 
   AnnLineObject lineObj = new AnnLineObject(); 
   lineObj.Start = new Point(100, 100); 
   lineObj.End = new Point(200, 200); 
   lineObj.Stroke = Colors.Red; 
   lineObj.StrokeThickness = 1.0; 
   container.Children.Add(lineObj); 
 
   AnnRectangleObject rectObj = new AnnRectangleObject(); 
 
   rectObj.Rect = new Rect(100, 100, 200, 200); 
   rectObj.Stroke = Colors.Red; 
   rectObj.Fill = Colors.White; 
 
   rectObj.StrokeThickness = 1.0; 
   container.Children.Add(rectObj); 
 
   // create a new instance of the AnnCodecs object 
   AnnCodecs ann = new AnnCodecs(); 
 
   // save this container as a TIFF tag 
   byte[] tagData = ann.SaveToTag(container, AnnCodecsTagFormat.Serialize); 
 
   if (tagData != null) 
   { 
      FileStream fs = new FileStream(tifFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); 
      BitmapMetadata bmd = new BitmapMetadata("tiff"); 
      bmd.SetQuery("/ifd/{uint=32932}", tagData); 
      BitmapFrame bf = BitmapFrame.Create(viewer.Source as BitmapSource, null, bmd, null); 
 
      TiffBitmapEncoder tiffEncoder = new TiffBitmapEncoder(); 
      tiffEncoder.Frames.Add(bf); 
      tiffEncoder.Save(fs); 
 
      fs.Close(); 
   } 
 
   // clear the container and the tag 
   container.Children.Clear(); 
   tagData = null; 
 
   // re-load the tag 
   { 
      FileStream fs = new FileStream(tifFileName, FileMode.Open); 
      BitmapFrame bf = BitmapFrame.Create(fs); 
      InPlaceBitmapMetadataWriter tiffInplace = null; 
      try 
      { 
         tiffInplace = bf.CreateInPlaceBitmapMetadataWriter(); 
      } 
      catch 
      { 
      } 
      finally 
      { 
         if (tiffInplace != null && tiffInplace.ContainsQuery("/ifd/{uint=32932}")) 
         { 
            tagData = (byte[])tiffInplace.GetQuery("/ifd/{uint=32932}"); 
         } 
         fs.Close(); 
      } 
   } 
 
   // load the container 
   ann.LoadFromTag(tagData, container); 
} 
Imports Leadtools.Windows.Controls 
Imports Leadtools.Windows.Annotations 
 
Public Sub AnnCodecs_SaveToTag(ByVal viewer As ImageViewer, ByVal tifFileName As String) 
   ' first create a container and add a few objects 
   Dim container As AnnContainer = New AnnContainer() 
   container.Width = 400 
   container.Height = 400 
 
   Dim lineObj As AnnLineObject = New AnnLineObject() 
   lineObj.Start = New Point(100, 100) 
   lineObj.End = New Point(200, 200) 
   lineObj.Stroke = Colors.Red 
   lineObj.StrokeThickness = 1.0 
   container.Children.Add(lineObj) 
 
   Dim rectObj As AnnRectangleObject = New AnnRectangleObject() 
   rectObj.Rect = New Rect(100, 100, 200, 200) 
   rectObj.Stroke = Colors.Red 
   rectObj.StrokeThickness = 1.0 
   rectObj.Fill = Colors.White 
   container.Children.Add(rectObj) 
 
   ' create a new instance of the AnnCodecs object 
   Dim ann As AnnCodecs = New AnnCodecs() 
 
   ' save this container as a TIFF tag 
   Dim tagData As Byte() = ann.SaveToTag(container, AnnCodecsTagFormat.Serialize) 
 
   If Not tagData Is Nothing Then 
      Dim fs1 As FileStream = New FileStream(tifFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite) 
      Dim bmd As BitmapMetadata = New BitmapMetadata("tiff") 
      bmd.SetQuery("/ifd/{uint=32932}", tagData) 
      Dim bf1 As BitmapFrame = BitmapFrame.Create(DirectCast(viewer.Source, BitmapSource), Nothing, bmd, Nothing) 
 
      Dim tiffEncoder As TiffBitmapEncoder = New TiffBitmapEncoder() 
      tiffEncoder.Frames.Add(bf1) 
      tiffEncoder.Save(fs1) 
 
      fs1.Close() 
   End If 
 
   ' clear the container and the tag 
   container.Children.Clear() 
   tagData = Nothing 
 
   ' re-load the tag 
   Dim fs As FileStream = New FileStream(tifFileName, FileMode.Open) 
   Dim bf As BitmapFrame = BitmapFrame.Create(fs) 
   Dim tiffInplace As InPlaceBitmapMetadataWriter = Nothing 
   Try 
      tiffInplace = bf.CreateInPlaceBitmapMetadataWriter() 
   Catch 
   Finally 
      If Not tiffInplace Is Nothing AndAlso tiffInplace.ContainsQuery("/ifd/{uint=32932}") Then 
         tagData = CType(tiffInplace.GetQuery("/ifd/{uint=32932}"), Byte()) 
      End If 
      fs.Close() 
   End Try 
 
   ' load the container 
   ann.LoadFromTag(tagData, container) 
End Sub 

Requirements

Target Platforms

See Also

Reference

AnnCodecs Class

AnnCodecs Members

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Windows.Annotations Assembly