Search code examples
c#visual-studio-2010taglibid3-tag

Album artwork not encoding


Trying to create an mp3 embedder for personal use. Tried all of the solutions here on StackO, but none have worked.

Here is what I have:

TagLib.File tagFile = TagLib.File.Create("C:\\Users\\Dom\\Desktop\\song.mp3");
TagLib.Id3v2.AttachedPictureFrame pic = new TagLib.Id3v2.AttachedPictureFrame();
pic.TextEncoding = TagLib.StringType.Latin1;
pic.MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
pic.Type = TagLib.PictureType.FrontCover;
pic.Data = TagLib.ByteVector.FromPath("C:\\Users\\Dom\\Pictures\\picture.png");
tagFile.Tag.Pictures = new TagLib.IPicture[1] { pic };
tagFile.Tag.Album = "Album 1";
tagFile.Tag.Year = 1990;
tagFile.Save();   

The album tag and year tag show up fine under properties, and the program is not crashing or throwing any errors. Picture does not show up as the file icon, or in windows media player. Picture size is 300x300 pixels if that has any importance.


Solution

  • The reason is probably some conflict with existing ID3v2 Tags. Fix it like this:

        TagLib.File tagFile = TagLib.File.Create("C:\\Users\\Dom\\Desktop\\song.mp3"); 
        Tag t = tagFile.GetTag(TagTypes.Id3v2);
        tagFile.RemoveTags(TagTypes.Id3v2); 
        Tag tags = tagFile.GetTag(TagTypes.Id3v2);
        tagFile.GetTag(TagTypes.Id3v2, true); 
        tagFile.Save();
        tagFile = TagLib.File.Create("C:\\Users\\Dom\\Desktop\\song.mp3"); 
        tags = t;
    
        tagFile.GetTag(TagTypes.Id3v2).Pictures = new IPicture[] { 
        new Picture("C:\\Users\\Dom\\Pictures\\picture.png") 
           { MimeType = "image/png", Type = PictureType.FrontCover } 
        };
        tagFile.Tag.Album = "Album 1";
        tagFile.GetTag(TagTypes.Id3v2).Track = 0;
        tagFile.Tag.Year = 1990;
        tagFile.Save();