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:
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: