Can't add image keywords if there were no keywords to begin with, Taglib-Sharp

强颜欢笑 提交于 2019-12-12 02:45:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!