Reads all the tags stored in a TIFF file.
Syntax
Parameters
- fileName
- A String containing the input file name.
- pageNumber
- 1-based index of the page from which to read the tags.
Return Value
A collection of
RasterTagMetadata containing all the tags found in the file. If the file does not
contain any tags, an empty collection will be returned. If the file format does not support tags, an exception will be thrown.
Example
This example will load all the metadata (tags, geo-keys and comments) found in a disk file.
Visual Basic | Copy Code |
---|
Public Sub MetadataLoadExample()
Dim imageFileName As String
Using dlg As New OpenFileDialog()
dlg.Title = "Browse to file to load its meta data"
dlg.Filter = "All Files|*.*"
dlg.InitialDirectory = "C:\Users\Public\Documents\LEADTOOLS Images"
If dlg.ShowDialog() = DialogResult.OK Then
imageFileName = dlg.FileName
Else
Return
End If
End Using
RasterCodecs.Startup()
Using codecs As New RasterCodecs()
Dim format As RasterImageFormat
Using info As CodecsImageInfo = codecs.GetInformation(imageFileName, False)
format = info.Format
End Using
Dim tags As RasterCollection(Of RasterTagMetadata) = Nothing
If RasterCodecs.TagsSupported(format) Then
tags = codecs.ReadTags(imageFileName, 1)
End If
Dim comments As RasterCollection(Of RasterCommentMetadata) = Nothing
If RasterCodecs.CommentsSupported(format) Then
comments = codecs.ReadComments(imageFileName, 1)
End If
Dim geoKeys As RasterCollection(Of RasterTagMetadata) = Nothing
If RasterCodecs.GeoKeysSupported(format) Then
geoKeys = codecs.ReadGeoKeys(imageFileName, 1)
End If
Dim txtFileName As String = Path.Combine( _
Path.GetDirectoryName(imageFileName), _
Path.GetFileNameWithoutExtension(imageFileName) + "_metadata.txt")
Using writer As StreamWriter = File.CreateText(txtFileName)
WriteTags(writer, "Tags", tags)
WriteComments(writer, comments)
WriteTags(writer, "GeoKeys", geoKeys)
End Using
System.Diagnostics.Process.Start(txtFileName)
End Using
RasterCodecs.Shutdown()
End Sub
Private Shared Sub WriteTags(ByVal writer As StreamWriter, ByVal name As String, ByVal tags As RasterCollection(Of RasterTagMetadata))
writer.WriteLine("{0}:", name)
If Not IsNothing(tags) Then
For Each tag As RasterTagMetadata In tags
writer.WriteLine("Id: 0x{0}, data length: {1}", tag.Id.ToString("X"), tag.GetData().Length)
Next
Else
writer.WriteLine("Not supported")
End If
writer.WriteLine()
End Sub
Private Shared Sub WriteComments(ByVal writer As StreamWriter, ByVal comments As RasterCollection(Of RasterCommentMetadata))
writer.WriteLine("Comments:")
If Not IsNothing(comments) Then
For Each comment As RasterCommentMetadata In comments
writer.WriteLine("Type: {0}, data length: {1}", comment.Type, comment.GetData().Length)
Next
Else
writer.WriteLine("Not supported")
End If
writer.WriteLine()
End Sub |
C# | Copy Code |
---|
public void MetadataLoadExample() { // Prompt the user for an image file string imageFileName; using(OpenFileDialog dlg = new OpenFileDialog()) { dlg.Title = "Browse to file to load its meta data"; dlg.Filter = "All Files|*.*"; dlg.InitialDirectory = @"C:\Users\Public\Documents\LEADTOOLS Images"; if(dlg.ShowDialog() == DialogResult.OK) imageFileName = dlg.FileName; else return; } // Initialize LEADTOOLS RasterCodecs.Startup(); using(RasterCodecs codecs = new RasterCodecs()) { // Get the file format RasterImageFormat format; using(CodecsImageInfo info = codecs.GetInformation(imageFileName, false)) { format = info.Format; } // Load the tags RasterCollection<RasterTagMetadata> tags = null; if(RasterCodecs.TagsSupported(format)) tags = codecs.ReadTags(imageFileName, 1); // Load the comments RasterCollection<RasterCommentMetadata> comments = null; if(RasterCodecs.CommentsSupported(format)) comments = codecs.ReadComments(imageFileName, 1); // Load the geo keys RasterCollection<RasterTagMetadata> geoKeys = null; if(RasterCodecs.GeoKeysSupported(format)) geoKeys = codecs.ReadGeoKeys(imageFileName, 1); string txtFileName = Path.Combine( Path.GetDirectoryName(imageFileName), Path.GetFileNameWithoutExtension(imageFileName) + "_metadata.txt"); using(StreamWriter writer = File.CreateText(txtFileName)) { // Write the tags WriteTags(writer, "Tags", tags); // Write the comments WriteComments(writer, comments); // Write the geo keys (tags and geokeys use the same data type) WriteTags(writer, "GeoKeys", geoKeys); } // Show the text file we created System.Diagnostics.Process.Start(txtFileName); } RasterCodecs.Shutdown(); } private static void WriteTags(StreamWriter writer, string name, RasterCollection<RasterTagMetadata> tags) { writer.WriteLine("{0}:", name); if(tags != null) { foreach(RasterTagMetadata tag in tags) { writer.WriteLine("Id: 0x{0}, data length: {1}", tag.Id.ToString("X"), tag.GetData().Length); } } else { writer.WriteLine("Not supported"); } writer.WriteLine(); } private static void WriteComments(StreamWriter writer, RasterCollection<RasterCommentMetadata> comments) { writer.WriteLine("Comments:"); if(comments != null) { foreach(RasterCommentMetadata comment in comments) { writer.WriteLine("Type: {0}, data length: {1}", comment.Type, comment.GetData().Length); } } else { writer.WriteLine("Not supported"); } writer.WriteLine(); } |
Remarks
Requirements
Target Platforms: Microsoft .NET Framework 3.0, Windows XP, Windows Server 2003 family, Windows Server 2008 family
See Also