Loading Album art with TagLib sharp and then saving it to same/different file in C# causes memory error

后端 未结 3 640
余生分开走
余生分开走 2021-02-10 13:19

My application lists all MP3\'s in a directory and when the user selects a file it loads the tag info, including album art. The art is loaded into a variable to be used when the

相关标签:
3条回答
  • 2021-02-10 13:55

    Your stream stuff should be in using blocks, which will dispose of your goods automagically and close them too. Not terribly important, but easier to manage.

    Your generic GDI+ error is likely because you are trying to perform an operation or call a method on a file for which the stream is already closed.

    Check it out...

    0 讨论(0)
  • 2021-02-10 14:00

    Thanks for the help Jim but I couldn't really get it working using the 'using' blocks so I'm guessing the stream was still closing somewhere. I found another way to do what I was looking for by storing/saving a byte[] instead of an Image. And then just saving it using:

    using (MemoryStream ms = new MemoryStream(currentImageBytes))
    {
        pic.Data = TagLib.ByteVector.FromStream(ms);
        f.Tag.Pictures = new TagLib.IPicture[1] { pic };
        if (save)
            f.Save();
    }
    
    0 讨论(0)
  • 2021-02-10 14:05

    your method isn't really false, only a few things had to be changed:

    // Method to save album art
    TagLib.Picture pic = new TagLib.Picture();
    pic.Type = TagLib.PictureType.FrontCover;
    pic.MimeType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
    pic.Description = "Cover";
    MemoryStream ms = new MemoryStream();
    currentImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); // <-- Error doesn't occur anymore
    ms.Position = 0;
    pic.Data = TagLib.ByteVector.FromStream(ms);
    f.Tag.Pictures = new TagLib.IPicture[1] { pic };
    f.save();
    ms.Close();
    
    0 讨论(0)
提交回复
热议问题