0

Newbie here trying to figure this out. Any help, please, would be greatly appreciated!

I have a .net V4.8 console app utilizing the TagLibSharp V2.2.0 library which updates a JPG with a comment string. If I update the JPG comment with a string with an even number of characters and then look at the comment string in Windows File Explorer, I see the comment in English. If I update the JPG comment with a string with an odd number of characters and then look at the comment string in Windows File Explorer, I see what looks like Chinese characters. The number of Chinese characters is half the number of characters in the string. In both cases, odd or even number of characters in the string, when I retrieve the comment string using TagLibSharp, it appears in English.

Is this some kind of encoding problem? And if so, how do I solve it so that Windows File Explorer will always display the comment as English text regardless of whether the string has an even or odd number of characters in it?

Thanks for any kind and gentle guidance.

using System;
using System.IO;

namespace CommentApic
{
    class Program
    {
        public static readonly string UserDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        public static readonly string originalPic = Path.Combine(UserDocuments, "OriginalPhoto.jpg");
        public static readonly string newPic = Path.Combine(UserDocuments, "NewPhoto.jpg");

        static void Main(string[] args)
        {
            if (args.Length == 1)
            {
                // Display starting tags
                Console.WriteLine("\n\tOriginal comments:");
                Console.WriteLine(string.Format("\t\toriginalPic: \"{0}\"", getJPGComment(originalPic)));

                // Copy to working copy
                Console.WriteLine("\n\tCopying originalPic to newPic");
                File.Copy(originalPic, newPic, true);

                // Remvoe all tags
                Console.WriteLine("\n\tRemoving tags on newPic");
                removeAllTags(newPic);

                // Display tags
                Console.WriteLine("\n\tThere should be no comments now:");
                Console.WriteLine(string.Format("\t\tnewPic: \"{0}\"", getJPGComment(newPic)));

                // Set the comment tag
                Console.WriteLine("\n\tSetting comments on newPic");
                setJPGComment(newPic, args[0]);

                // Show the comments
                Console.WriteLine("\n\tComments Now:");
                Console.WriteLine(string.Format("\t\tnewPic: \"{0}\"", getJPGComment(newPic)));
            }
            else
            {
                Console.WriteLine("Please supply a comment to add to the JPG");
            }
        }

        ///////////////////////////////////////////////////////////////////////
        public static string getJPGComment(string file)
        {
            // Local variables
            string comment = "";

            using (TagLib.File tagFile = TagLib.File.Create(file))
            {
                // Get the image tags
                TagLib.Image.File image = tagFile as TagLib.Image.File;

                // Ensure all tags are avaialble
                image.EnsureAvailableTags();

                // Get the tag
                comment = image.ImageTag.Comment;
            }

            // Return comment to caller
            return comment;
        }

        ///////////////////////////////////////////////////////////////////////
        public static void removeAllTags(string file)
        {
            using (var taglibFile = TagLib.File.Create(file))
            {
                taglibFile.RemoveTags(TagLib.TagTypes.AllTags);
                taglibFile.Save();
            }
        }

        ///////////////////////////////////////////////////////////////////////
        public static void setJPGComment(string file, string comment)
        {
            using (TagLib.File tagFile = TagLib.File.Create(file))
            {
                // Get the image tags
                TagLib.Image.File image = tagFile as TagLib.Image.File;

                // Ensure all tags are availble
                image.EnsureAvailableTags();

                // Set the tag value
                image.ImageTag.Comment = comment;

                // Save the tag
                tagFile.Save();
            }
        }
    }
}

Sample Output:

CommentApic.exe A

        Original comments:
                originalPic: "New photo comment"

        Copying originalPic to newPic

        Removing tags on newPic

        There should be no comments now:
                newPic: ""

        Setting comments on newPic

        Comments Now:
                newPic: "A"

Windows File Explorer shows this:

enter image description here

CommentApic.exe AA

        Original comments:
                originalPic: "New photo comment"

        Copying originalPic to newPic

        Removing tags on newPic

        There should be no comments now:
                newPic: ""

        Setting comments on newPic

        Comments Now:
                newPic: "AA"

Windows File Explorer shows this:

enter image description here

Dan
  • 41
  • 4

1 Answers1

0

I managed to get this working with this code:

public static void setPhotoComment(string file, string comment)
{
    // Open the file
    using (TagLib.File tfile = TagLib.File.Create(file, "taglib/jpg", TagLib.ReadStyle.None))
    {
        // Remove all tags
        tfile.RemoveTags(TagLib.TagTypes.AllTags);

        // Create an empty tag
        TagLib.IFD.IFDTag tag = (TagLib.IFD.IFDTag)tfile.GetTag(TagLib.TagTypes.TiffIFD, true);

        // Was something returned
        if (tag != null)
        {
            // Get the tag structure
            if (tag.Structure != null)
            {
                // Save a pointer to the structure
                TagLib.IFD.IFDStructure tagStructure = tag.Structure;

                // Create our byte vector
                TagLib.ByteVector commentBytes = TagLib.ByteVector.FromString(comment + "\0", TagLib.StringType.UTF16);

                // Create our byte vector entry
                TagLib.IFD.Entries.ByteVectorIFDEntry commentEntry = new TagLib.IFD.Entries.ByteVectorIFDEntry(JPGtag,
                    commentBytes);

                // Add comment entry
                tagStructure.SetEntry(0, commentEntry);

                // Save the comment
                tfile.Save();
            }
        }
    }
}
Dan
  • 41
  • 4
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 26 '22 at 20:45