public void CommentsExample() { RasterCodecs.Startup(); RasterCodecs codecs = new RasterCodecs(); string srcFileName = LeadtoolsExamples.Common.ImagesPath.Path + "Image1.cmp"; string destFileName = LeadtoolsExamples.Common.ImagesPath.Path + "Image1_Comments.tif"; // Convert the source file to TIF Console.WriteLine("Converting the source file to TIF"); codecs.Convert(srcFileName, destFileName, RasterImageFormat.Tif, 0, 0, 24, null); // Add the artist comment RasterCommentMetadata writeComment = new RasterCommentMetadata(); writeComment.Type = RasterCommentMetadataType.Artist; writeComment.FromAscii("LEADTOOLS"); Console.WriteLine("Writing the following comment:"); Console.WriteLine(" Type:{0}, Data:{1}", writeComment.Type, writeComment.ToAscii()); codecs.WriteComment(destFileName, 1, writeComment); // Read the comment back RasterCommentMetadata readComment = codecs.ReadComment(destFileName, 1, RasterCommentMetadataType.Artist); Console.WriteLine("The following comment has been read:"); Console.WriteLine(" Type:{0}, Data:{1}", readComment.Type, readComment.ToAscii()); // Write a few comments to the file in one pass RasterCollection<RasterCommentMetadata> comments = new RasterCollection<RasterCommentMetadata>(); writeComment = new RasterCommentMetadata(); writeComment.Type = RasterCommentMetadataType.Artist; writeComment.FromAscii("LEADTOOLS Again"); comments.Add(writeComment); writeComment = new RasterCommentMetadata(); writeComment.Type = RasterCommentMetadataType.Copyright; writeComment.FromAscii("(c) 2006"); comments.Add(writeComment); Console.WriteLine("Writing the following comments to the file:"); foreach(RasterCommentMetadata comment in comments) Console.WriteLine(" Type:{0}, Data:{1}", comment.Type, comment.ToAscii()); codecs.WriteComments(destFileName, 1, comments); // Now get all the comments in the file and show them: Console.WriteLine("Reading all comments from the file:"); RasterCommentMetadataType[] tifComments = { RasterCommentMetadataType.Artist, RasterCommentMetadataType.Copyright, RasterCommentMetadataType.DateTime, RasterCommentMetadataType.Description, RasterCommentMetadataType.HostComputer, RasterCommentMetadataType.Make, RasterCommentMetadataType.Model, RasterCommentMetadataType.NameOfDocument, RasterCommentMetadataType.NameOfPage, RasterCommentMetadataType.Software, }; foreach(RasterCommentMetadataType tifComment in tifComments) { RasterCommentMetadata comment = codecs.ReadComment(destFileName, 1, tifComment); if(comment != null) { Console.Write("Found comment, Type:{0}, Data:", comment.Type); RasterCommentMetadataDataType dataType = RasterCommentMetadata.GetDataType(comment.Type); byte[] byteData; short[] shortData; RasterMetadataRational[] rationalData; RasterMetadataURational[] urationalData; switch(dataType) { case RasterCommentMetadataDataType.Ascii: Console.WriteLine(comment.ToAscii()); break; case RasterCommentMetadataDataType.Byte: byteData = comment.ToByte(); for(int i = 0; i < byteData.Length; i++) Console.Write("{0:X} ", byteData[i]); Console.WriteLine(); break; case RasterCommentMetadataDataType.Int16: shortData = comment.ToInt16(); for(int i = 0; i < shortData.Length; i++) Console.Write("{0:X} ", shortData[i]); Console.WriteLine(); break; case RasterCommentMetadataDataType.Rational: rationalData = comment.ToRational(); for(int i = 0; i < rationalData.Length; i++) Console.Write(@"{0}/{1) ", rationalData[i].Numerator, rationalData[i].Denominator); Console.WriteLine(); break; case RasterCommentMetadataDataType.URational: urationalData = comment.ToURational(); for(int i = 0; i < urationalData.Length; i++) Console.Write(@"{0}/{1) ", urationalData[i].Numerator, urationalData[i].Denominator); Console.WriteLine(); break; } } } // Clean up codecs.Dispose(); RasterCodecs.Shutdown(); } |