问题
I'm trying to add metadata keywords to image files using Taglib-Sharp. My problem is that the following code:
image.ImageTag.Keywords = (string[])newTags.ToArray(typeof(string));
image.Save();
does nothing if there are no keywords in the file to begin with. If I open the file properties in Explorer and add tags manually first, it works just fine and adds the tags to the file.
I've tried setting a breakpoint at the top of this method and I can see that image.ImageTag.Keywords
has a length of zero, as it should at first, and the snippet (string[])newTags.ToArray(typeof(string))
does successfully return a populated string array, but after that line the image.ImageTag.Keywords
variable still has a length of zero.
I'm totally stuck here, if I manually add at least one tag first everything works great. The problem only occurs when the file has no tags in it to start with.
回答1:
Okay, so after hours of debugging I've come to the conclusion that this problem is a bug in TagLib itself. If a new image file is created and the meta keyword property for that file was NEVER changed manually, then TagLib seems to just ignore setting the image.ImageTag.Keywords
array and just skips over it. In order to get it to change the Keywords in the file, I must first manually add a tag to the file and then remove it. This initializes the tag property in the file itself and therefore allows it to be set by TagLib. If this property was never initialized, TagLib simply ignores trying to set the image.ImageTag.Keywords
variable altogether.
So could anyone add to this and tell me if it's possible to get around this?
来源:https://stackoverflow.com/questions/35414734/cant-add-image-keywords-if-there-were-no-keywords-to-begin-with-taglib-sharp