←Select platform

WriteComment(Stream,int,RasterCommentMetadata) Method

Summary
Writes a comment to an existing stream.
Syntax
C#
Objective-C
C++/CLI
Java
Python
public void WriteComment( 
   Stream stream, 
   int pageNumber, 
   RasterCommentMetadata comment 
) 
- (BOOL)writeComment:(nullable LTRasterCommentMetadata *)comment  
            toStream:(LTLeadStream *)stream  
          pageNumber:(NSInteger)pageNumber  
               error:(NSError **)error 
public void writeComment(ILeadStream stream, int pageNumber, RasterCommentMetadata comment) 
public: 
void WriteComment(  
   Stream^ stream, 
   int pageNumber, 
   RasterCommentMetadata^ comment 
)  
def WriteComment(self,stream,pageNumber,comment): 

Parameters

stream
A Stream that contains the file data.

pageNumber
1-based index of the page at which to write the comment.

comment
A RasterCommentMetadata object containing the comment data.

Remarks

This function only applies to TIF, EXIF, JPEG, JPEG 2000, FPX, and PNG files.

When you add or remove tags, the tags array at the end of the file is re-written. When you modify existing tags, the new tag value is added to the file and the IFD is modified as necessary. In all of these cases, there is no image recompression.

Some restrictions apply to this function if you use an IFD to indicate to which page to write the comment. See the Loading and Saving Large TIFF / BigTIFF Files and Types of File Comments topics for more information.

Example
C#
Java
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.ImageProcessing; 
using Leadtools.ImageProcessing.Color; 
using Leadtools.Svg; 
 
 
 
public void CommentsExample() 
{ 
   RasterCodecs codecs = new RasterCodecs(); 
 
   string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"); 
   string destFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1_Comments.tif"); 
 
   // Convert the source file to TIF 
   Debug.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"); 
 
   Debug.WriteLine("Writing the following comment:"); 
   Debug.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); 
   Debug.WriteLine("The following comment has been read:"); 
   Debug.WriteLine(" Type:{0}, Data:{1}", readComment.Type, readComment.ToAscii()); 
 
   // Write a few comments to the file in one pass 
   IList<RasterCommentMetadata> comments = new List<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); 
 
   Debug.WriteLine("Writing the following comments to the file:"); 
   foreach (RasterCommentMetadata comment in comments) 
      Debug.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: 
   Debug.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: 
               Debug.WriteLine(comment.ToAscii()); 
               break; 
 
            case RasterCommentMetadataDataType.Byte: 
               byteData = comment.ToByte(); 
               for (int i = 0; i < byteData.Length; i++) 
                  Console.Write("{0:X} ", byteData[i]); 
               Debug.WriteLine("  "); 
               break; 
 
            case RasterCommentMetadataDataType.Int16: 
               shortData = comment.ToInt16(); 
               for (int i = 0; i < shortData.Length; i++) 
                  Console.Write("{0:X} ", shortData[i]); 
               Debug.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); 
               Debug.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); 
               Debug.WriteLine("  "); 
               break; 
         } 
      } 
   } 
 
   // Clean up 
   codecs.Dispose(); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.*; 
import java.net.*; 
import java.nio.file.Paths; 
import java.util.*; 
import java.time.Instant; 
import java.time.Duration; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.codecs.RasterCodecs.FeedCallbackThunk; 
import leadtools.drawing.internal.*; 
import leadtools.imageprocessing.*; 
import leadtools.imageprocessing.color.ChangeIntensityCommand; 
import leadtools.svg.*; 
 
 
public void commentsExample() throws IOException { 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   RasterCodecs codecs = new RasterCodecs(); 
 
   String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "Image1.cmp"); 
   String destFileName = combine(LEAD_VARS_IMAGES_DIR, "Image1_Comments.tif"); 
 
   // Convert the source file to TIF 
   System.out.println("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.setType(RasterCommentMetadataType.ARTIST); 
   writeComment.fromAscii("LEADTOOLS"); 
 
   System.out.println("Writing the following comment:"); 
   System.out.printf(" Type:%s, Data:%s%n", writeComment.getType(), writeComment.toAscii()); 
 
   ILeadStream destFileStream = LeadStreamFactory.create(destFileName); 
   codecs.writeComment(destFileStream, 1, writeComment); 
 
   // Read the comment back 
   RasterCommentMetadata readComment = codecs.readComment(destFileStream, 1, RasterCommentMetadataType.ARTIST); 
   System.out.println("The following comment has been read:"); 
   System.out.printf(" Type:%s, Data:%s%n", readComment.getType(), readComment.toAscii()); 
 
   // Write a few comments to the file in one pass 
   RasterCollection<RasterCommentMetadata> comments = new RasterCollection<RasterCommentMetadata>(); 
   writeComment = new RasterCommentMetadata(); 
   writeComment.setType(RasterCommentMetadataType.ARTIST); 
   writeComment.fromAscii("LEADTOOLS Again"); 
   comments.add(writeComment); 
 
   writeComment = new RasterCommentMetadata(); 
   writeComment.setType(RasterCommentMetadataType.COPYRIGHT); 
   writeComment.fromAscii("(c) 2006"); 
   comments.add(writeComment); 
 
   System.out.println("Writing the following comments to the file:"); 
   for (RasterCommentMetadata comment : comments) 
      System.out.printf(" Type:%s, Data:%s%n", comment.getType(), comment.toAscii()); 
 
   codecs.writeComments(destFileStream, 1, comments); 
 
   // Now get all the comments in the file and show them: 
   System.out.println("Reading all comments from the file:"); 
 
   RasterCommentMetadataType[] tifComments = { 
         RasterCommentMetadataType.ARTIST, 
         RasterCommentMetadataType.COPYRIGHT, 
         RasterCommentMetadataType.DATE_TIME, 
         RasterCommentMetadataType.DESCRIPTION, 
         RasterCommentMetadataType.HOST_COMPUTER, 
         RasterCommentMetadataType.MAKE, 
         RasterCommentMetadataType.MODEL, 
         RasterCommentMetadataType.NAME_OF_DOCUMENT, 
         RasterCommentMetadataType.NAME_OF_PAGE, 
         RasterCommentMetadataType.SOFTWARE, 
   }; 
 
   for (RasterCommentMetadataType tifComment : tifComments) { 
      RasterCommentMetadata comment = codecs.readComment(destFileStream, 1, tifComment); 
      if (comment != null) { 
         System.out.printf("Found comment, Type:%s, Data:%n", comment.getType()); 
 
         RasterCommentMetadataDataType dataType = RasterCommentMetadata.getDataType(comment.getType()); 
 
         byte[] byteData; 
         short[] shortData; 
         RasterMetadataRational[] rationalData; 
         RasterMetadataURational[] urationalData; 
 
         switch (dataType) { 
            case ASCII: 
               System.out.println(comment.toAscii()); 
               break; 
 
            case BYTE: 
               byteData = comment.toByte(); 
               for (int i = 0; i < byteData.length; i++) 
                  System.out.println(byteData[i]); 
               System.out.println("  "); 
               break; 
 
            case INT_16: 
               shortData = comment.toInt16(); 
               for (int i = 0; i < shortData.length; i++) 
                  System.out.println(shortData[i]); 
               System.out.println("  "); 
               break; 
 
            case RATIONAL: 
               rationalData = comment.toRational(); 
               for (int i = 0; i < rationalData.length; i++) 
                  System.out.println((double) rationalData[i].getNumerator() / rationalData[i].getDenominator()); 
               System.out.println("  "); 
               break; 
 
            case URATIONAL: 
               urationalData = comment.toURational(); 
               for (int i = 0; i < urationalData.length; i++) 
                  System.out.println(urationalData[i].getNumerator() / urationalData[i].getDenominator()); 
               System.out.println("  "); 
               break; 
         } 
         assertTrue("dataType is not ASCII, BYTE, INT_16, RATIONAL, or URATIONAL", 
               dataType == RasterCommentMetadataDataType.ASCII 
                     || dataType == RasterCommentMetadataDataType.BYTE 
                     || dataType == RasterCommentMetadataDataType.INT_16 
                     || dataType == RasterCommentMetadataDataType.RATIONAL || 
                     dataType == RasterCommentMetadataDataType.URATIONAL); 
      } 
   } 
   // Clean up 
   destFileStream.close(); 
   codecs.dispose(); 
} 
Requirements

Target Platforms

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Codecs Assembly
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.