Search code examples
c#tagsmp3cover

MP3 Cover Art Tagging in C#


I need to tag MP3 files with a Cover Art in C#...

Is there any easy way to do this? I found UltraID3Lib as an exampel and it is great for regular ID3 tagging but I cant handle the cover art. If someone know an easy way to do this would it be great :)


Solution

  • I do something like this:

    private static void FixAlbumArt(FileInfo MyFile)
    {
      //Find the jpeg file in the directory of the Mp3 File
      //We will embed this image into the ID3v2 tag
      FileInfo[] fiAlbumArt = MyFile.Directory.GetFiles("*.jpg");
      if (fiAlbumArt.Length < 1)
      {
        Console.WriteLine("No Album Art Found in {0}", MyFile.Directory.Name);
        return;
      }
      string AlbumArtFile = fiAlbumArt[0].FullName;
    
      //Create Mp3 Object
      UltraID3 myMp3 = new UltraID3();
      myMp3.Read(MyFile.FullName);
      ID3FrameCollection myArtworkCollection =
        myMp3.ID3v23Tag.Frames.GetFrames(MultipleInstanceFrameTypes.Picture);
    
      if (myArtworkCollection.Count > 0)
      {//Get Rid of the Bad Embedded Artwork
        #region Remove All Old Artwork
        for (int i = 0; i < myArtworkCollection.Count; i++)
        {
          ID3PictureFrame ra = (ID3PictureFrame)myArtworkCollection[0];
          try
          {
            myMp3.ID3v23Tag.Frames.Remove(FrameTypes.Picture);
          }
          catch { }
        }
        myArtworkCollection.Clear();
    
         //Save out our changes so that we are working with the
        //most up to date file and tags
        myMp3.ID3v23Tag.WriteFlag = true;
        myMp3.Write();
        myMp3.Read(MyFile.FullName);
        #endregion Remove All Old Artwork
      }
      //Create a PictureFrame object, pointing it at the image on my PC
      ID3PictureFrame AlbumArt =
        new ID3PictureFrame(
        (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile(AlbumArtFile),
        PictureTypes.CoverFront, "Attached picture", TextEncodingTypes.ISO88591);
      myMp3.ID3v23Tag.Frames.Add(AlbumArt);
      myMp3.ID3v23Tag.WriteFlag = true;
      myMp3.Write();
    
      myMp3 = null;
    }
    

    I'm at work and forgot to turn on Foldershare so I can't show you my trimmed down version where I pass in an Image object but this has everything you'd need to get the job done with a bit of hacking. Good luck.