Send comments on this topic. | Back to Introduction - All Topics | Help Version 15.12.21
Implementing Exif Features

The Exif file format supports more complex comments than other formats. For a list of the comments, refer to RasterCommentMetadataType Enumeration.

This example demonstrates how to handle Exif comments that are not stored as strings. (For a simpler example for saving strings, refer to the WriteComment method.)

This example does the following:

1.

Loads an image.

2.

Updates the comment array by loading comments from the existing file.

3.

Reverses the image and creates a RasterCommentMetadataType.UserComment comment that says that the image has been reversed.

4.

Saves the file. (All of the comments are saved, including the one that we updated.)

5.

Reads and displays two of the comments from the saved file. One of them is the comment that we created. The other is comment that uses the Exif RasterTagMetadataDataType.URational data type.

6.

Cleans up the comment array.


Visual Basic


Private Sub WriteCommentsTest()
   RasterCodecs.Startup()
   Dim codecs As New RasterCodecs()

   Dim srcFileName As String = "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Image1.cmp"
   Dim destFileName As String = "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Exif.tif"

   Dim tempImage As RasterImage = codecs.Load(srcFileName)

   ' Flip the image.
   Dim flipCmd As New FlipCommand(True)
   flipCmd.Run(tempImage)

   ' Add a user comment to the file and save it in another name.
   Dim comment As New RasterCommentMetadata()
   comment.Type = RasterCommentMetadataType.UserComment
   Dim comments As String = "The image has been flipped"
   Dim data() As Char = comments.ToCharArray(0, comments.Length)
   Dim dataComments(8 + comments.Length - 1) As Byte

   ' when you need to write a user comment the first 8 bytes 
   ' must contain the "Ascii" word.
   dataComments(0) = 41
   dataComments(1) = 53
   dataComments(2) = 43
   dataComments(3) = 49
   dataComments(4) = 49
   dataComments(5) = 0
   dataComments(6) = 0
   dataComments(7) = 0

   For i As Integer = 0 To comments.Length - 1
      dataComments(8 + i) = CType(AscW(data(i)), Byte)
   Next

   ' set the data property in the comment object to the dataComments array.
   comment.FromByte(dataComments)

   ' add the user comment to the comments collection of the image
   tempImage.Comments.Add(comment)
   codecs.Options.Save.Comments = True
   codecs.Save(tempImage, destFileName, RasterImageFormat.Exif, 24)
   tempImage.Dispose()

   ' read the user comments and display it
   comment = codecs.ReadComment(destFileName, 1, RasterCommentMetadataType.UserComment)
   dataComments = comment.GetData()
   Dim msg As String = "User comments = "
   For i As Integer = 0 To dataComments.Length - 7
      msg = msg + dataComments(i + 8).ToString
   Next

   MessageBox.Show(msg, "Notice")

   RasterCodecs.Shutdown()
End Sub

C#


private void WriteCommentsTest()
{
   RasterCodecs.Startup();
   RasterCodecs codecs = new RasterCodecs();

   string srcFileName = @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Image1.cmp";
   string destFileName = @"C:\Program Files\LEAD Technologies\LEADTOOLS 15\Images\Exif.tif";

   RasterImage tempImage = codecs.Load(srcFileName);

   // Flip the image.
   FlipCommand flipCmd = new FlipCommand(true);
   flipCmd.Run(tempImage);
   
   // Add a user comment to the file and save it in another name.
   RasterCommentMetadata comment = new RasterCommentMetadata();
   comment.Type = RasterCommentMetadataType.UserComment;
   string comments = "The image has been flipped";
   char[] data = comments.ToCharArray(0, comments.Length);
   byte[] dataComments = new byte[8 + comments.Length];

   // when you need to write a user comment the first 8 bytes 
   // must contain the "Ascii" word.
   dataComments[0] = 41;
   dataComments[1] = 53;
   dataComments[2] = 43;
   dataComments[3] = 49;
   dataComments[4] = 49;
   dataComments[5] = 0;
   dataComments[6] = 0;
   dataComments[7] = 0;
   
   for(int i = 0; i < comments.Length; i++)
      dataComments[8 + i] = (byte)data[i];

   // set the data property in the comment object to the dataComments array.
   comment.FromByte(dataComments);

   // add the user comment to the comments collection of the image
   tempImage.Comments.Add(comment);
   codecs.Options.Save.Comments = true;
   codecs.Save(tempImage, destFileName, RasterImageFormat.Exif, 24);
   tempImage.Dispose();
   
   // read the user comments and display it
   comment = codecs.ReadComment(destFileName, 1, RasterCommentMetadataType.UserComment);
   dataComments = comment.GetData();
   string msg =  "User comments = ";
   for(int i = 0; i < (dataComments.Length - 8); i++)
      msg += (char)dataComments[i + 8];

   MessageBox.Show(msg, "Notice");

   RasterCodecs.Shutdown();
}