Write image keywords with tag lib sharp library

2

I want to write image tags (keywords tag) with Taglib# library (GitHub, NuGet Install-Package taglib), so I used this example and this tutorial. Based on it I wrote this method:

using TagLib;

    public void AddTagsToImage() 
    {
        string path = @"C:\image.jpg";
        string[] tagset = new string[] { "tag1", "tag2", "tag3" };
        TagLib.File tagFile = TagLib.File.Create(path);
        var image = tagFile as TagLib.Image.File;
        image.ImageTag.Keywords = tagset;
        image.Save();
    }

It compiles, but does not work as expected. Image file opens alright, then we identify it as image, but whet I try to set image.ImageTag.Keywords property nothing happens (It is string[] as is tagset). Saving image therefore modifies nothing. So, how do I need to modify to make this work?

EDIT. The problem was due to error 0x88982F72 or, apparently, if image metadata does not exists/damaged Taglib# library may not work correctly.

c#
image
file
taglib-sharp
asked on Stack Overflow Dec 27, 2013 by rfg • edited May 23, 2017 by Community

1 Answer

0

I have tried your code and works just fine for me.

    static void Main(string[] args)
    {
        string path = @"C:\image.jpg";
        string[] tagset = new string[] { "tag1", "tag2", "tag3" };
        TagLib.File tagFile = TagLib.File.Create(path);
        var image = tagFile as TagLib.Image.File;

        Console.WriteLine("Rating          : " + image.ImageTag.Rating);
        Console.WriteLine("DateTime        : " + image.ImageTag.DateTime);
        Console.WriteLine("Orientation     : " + image.ImageTag.Orientation);
        Console.WriteLine("Software        : " + image.ImageTag.Software);
        Console.WriteLine("ExposureTime    : " + image.ImageTag.ExposureTime);
        Console.WriteLine("FNumber         : " + image.ImageTag.FNumber);
        Console.WriteLine("ISOSpeedRatings : " + image.ImageTag.ISOSpeedRatings);
        Console.WriteLine("FocalLength     : " + image.ImageTag.FocalLength);
        Console.WriteLine("FocalLength35mm : " + image.ImageTag.FocalLengthIn35mmFilm);
        Console.WriteLine("Make            : " + image.ImageTag.Make);
        Console.WriteLine("Model           : " + image.ImageTag.Model);

        image.ImageTag.Keywords = tagset;
        image.Save();

        foreach (var item in image.ImageTag.Keywords)
            Console.WriteLine("> Tag: " + item);                

        Console.ReadLine();

    }

Be aware that for testing this code you need to actually have an image in the path: C:\image.jpg

answered on Stack Overflow Dec 27, 2013 by jfeston

User contributions licensed under CC BY-SA 3.0