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?
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.