Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# TagLib Set Album Cover for Mp3

I have an mp3 file and I want to add the album art to it. The art has been saved into a temp folder, I have check this and it is there and is a jpeg. This is the code I gave:

        public void AddMp3Tags()
        {
            TagLib.File file = TagLib.File.Create(OutputPath + OutputName + "." + Format);
            SetAlbumArt(Art, file);
            file.Tag.Title = SongTitle;
            file.Tag.Performers = Artists.Split(',');
            file.Tag.Album = Album;           
            file.Tag.Track = (uint)TrackNumber;
            file.Tag.Year = (uint)Convert.ToInt32(Regex.Match(Year, @"(\d)(\d)(\d)(\d)").Value);            
            file.Save();
        }

        public void SetAlbumArt(string url, TagLib.File file)
        {     
            string path = string.Format(@"{0}temp\{1}.jpg", OutputPath, Guid.NewGuid().ToString());
            using (WebClient client = new WebClient())
            {

                client.DownloadFile(new Uri(url), path);
            }


            TagLib.Picture pic = new TagLib.Picture
            {
                Type = TagLib.PictureType.FrontCover,
                Description = "Cover",
                MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg
            };
            MemoryStream ms = new MemoryStream();
            Image image = Image.FromFile(path);
            image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            ms.Position = 0;
            pic.Data = TagLib.ByteVector.FromStream(ms);
            file.Tag.Pictures = new TagLib.IPicture[] { pic };
            file.Save();
            ms.Close();

        }

All of the tags are set correctly except the art work which just shows a black box: Black box cover art in windows media player.

I have tried many things, what am I doing wrong?

like image 365
Laurence Rawlings Avatar asked Oct 19 '25 14:10

Laurence Rawlings


2 Answers

So I did some more research and it turns out that by default WMP tries to use a web service to get album artwork, I opened the song in VLC and the artwork was shown. The album code was correctly written as shown here: Mp3Tag Viewer/Editor

Another thing that I found out is that my tags were using Id3v2.4 and Id3v1. WMP does not like this for some reason so I forced TagLib to use Id3v2.3. I also changed the text encoding to UFT16 because UFT8 wasn't working. The album art is now showing in WMP and windows explorer.

I also found a way not to write the image to the disk by downloading the data from the web page and saving it to memory.

This was my final code:

public void AddMp3Tags()
{
    TagLib.Id3v2.Tag.DefaultVersion = 3;
    TagLib.Id3v2.Tag.ForceDefaultVersion = true;
    TagLib.File file = TagLib.File.Create(OutputPath + OutputName + ".mp3");
    SetAlbumArt(Art, file);
    file.Tag.Title = SongTitle;
    file.Tag.Performers = Artists.Split(',');
    file.Tag.Album = Album;           
    file.Tag.Track = (uint)TrackNumber;
    file.Tag.Year = (uint)Convert.ToInt32(Regex.Match(Year, @"(\d)(\d)(\d)(\d)").Value);
    file.RemoveTags(file.TagTypes & ~file.TagTypesOnDisk);
    file.Save();
}

public void SetAlbumArt(string url, TagLib.File file)
{            
    string path = string.Format(@"{0}temp\{1}.jpg", OutputPath, Guid.NewGuid().ToString());
    byte[] imageBytes;
    using (WebClient client = new WebClient())
    {
        imageBytes = client.DownloadData(url);
    }

    TagLib.Id3v2.AttachedPictureFrame cover = new TagLib.Id3v2.AttachedPictureFrame
    {
        Type = TagLib.PictureType.FrontCover,
        Description = "Cover",
        MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg,
        Data = imageBytes,
        TextEncoding = TagLib.StringType.UTF16


    };
    file.Tag.Pictures = new TagLib.IPicture[] { cover };
}

I hope this helps anyone that has the same problem as me and doesn't need to spend as much time figuring this out as I did.

like image 65
Laurence Rawlings Avatar answered Oct 22 '25 04:10

Laurence Rawlings


Make sure your file downloaded successfully and try this:

public void SetAlbumArt(string url, TagLib.File file)
{     
    string path = string.Format(@"{0}temp\{1}.jpg", OutputPath, Guid.NewGuid().ToString());
    using (WebClient client = new WebClient())
    {

        client.DownloadFile(new Uri(url), path);
    }

    file.Tag.Pictures = new TagLib.IPicture[]
    {
        new TagLib.Picture(new TagLib.ByteVector((byte[])new System.Drawing.ImageConverter().ConvertTo(System.Drawing.Image.FromFile(path), typeof(byte[]))))
        {
            Type = TagLib.PictureType.FrontCover,
            Description = "Cover",
            MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg
        }
    };

    file.Save();
}
like image 42
Hossein Golshani Avatar answered Oct 22 '25 03:10

Hossein Golshani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!