CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2010
    Posts
    2

    TagLib # Writing Images to File

    Hello,
    I am using TagLib # to read and write tags to mp3 files. Most of the data is very easy to read/write, but I am having trouble with the Album Art. I can get the Images from the file with no problem using the following code:

    TagLib.File TagLibFile = TagLib.File.Create(strFilePath);
    ImageConverter ic = new ImageConverter();
    int iNumberOfPictures = TagLibFile.Tag.Pictures.Length;
    Image[] images = new Image[iNumberOfPictures];

    for (int i = 0; i < iNumberOfPictures; i++)
    {
    images[i] = (Image)ic.ConvertFrom(TagLibFile.Tag.Pictures[i].Data.Data);
    }
    return images;

    I am also able to write an image back to the file with this code:

    TagLib.File TagLibFile = TagLib.File.Create(strFilePath);
    TagLib.Picture picture = TagLib.Picture.CreateFromPath("C:\\Pic.jpg");
    TagLib.Id3v2.AttachedPictureFrame albumCoverPictFrame = new TagLib.Id3v2.AttachedPictureFrame(picture);
    albumCoverPictFrame.MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
    albumCoverPictFrame.Type = TagLib.PictureType.FrontCover;
    TagLib.IPicture[] pictFrames = new IPicture[1];
    pictFrames[0] = (IPicture)albumCoverPictFrame;
    TagLibFile.Tag.Pictures = pictFrames;
    TagLibFile.Save();

    I would like to write the images to the file from an Image or byte[] instead of having to give TagLib a path. Is there a way to convert an Image (or byte[]) to an IPicture (opposite of the first code sample)? I was also trying to figure out the TagLib.Id3v2.AttachedPictureFrame(ByteVector data, byte version), but I wasn't sure if this is what I needed or how to use it. Thanks for your help!

    Jen

  2. #2
    Join Date
    Dec 2010
    Posts
    2

    Re: TagLib # Writing Images to File

    I found an answer to my question. Here is the code:

    TagLib.File TagLibFile = TagLib.File.Create(strFilePath);
    TagLib.IPicture[] pictFrames = new IPicture[imgAlbumArt.Length];

    for (int i = 0; i < imgAlbumArt.Length; i++)
    {
    MemoryStream ms = new MemoryStream();
    imgAlbumArt[i].Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    byte[] myBytes = ms.ToArray();
    ByteVector byteVector = new ByteVector(myBytes, myBytes.Length);
    TagLib.Picture picture = new Picture(byteVector);
    TagLib.Id3v2.AttachedPictureFrame albumCoverPictFrame = new TagLib.Id3v2.AttachedPictureFrame(picture);
    albumCoverPictFrame.MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
    albumCoverPictFrame.Type = TagLib.PictureType.FrontCover;
    pictFrames[i] = (IPicture)albumCoverPictFrame;
    }
    TagLibFile.Tag.Pictures = pictFrames;
    TagLibFile.Save();

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured