装载专辑封面与标签库锋利,然后将其保存在C#相同/不同的文件会导致内存错误 [英] Loading Album art with TagLib sharp and then saving it to same/different file in C# causes memory error

查看:146
本文介绍了装载专辑封面与标签库锋利,然后将其保存在C#相同/不同的文件会导致内存错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序将列出所有的MP3在一个目录,当用户选择它加载标签信息,包括专辑封面的文件。在本领域被装载到变量当用户将数据保存到使用。艺术也被加载到用户看到一个相框。

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 user saves the data. The art is also loaded into a picture frame for the user to see.

// Global to all methods
System.Drawing.Image currentImage = null;

// In method onclick of the listbox showing all mp3's
TagLib.File f = new TagLib.Mpeg.AudioFile(file);
if (f.Tag.Pictures.Length > 0)
{
      TagLib.IPicture pic = f.Tag.Pictures[0];
      MemoryStream ms = new MemoryStream(pic.Data.Data);
      if (ms != null && ms.Length > 4096)
      {
           currentImage = System.Drawing.Image.FromStream(ms);
           // Load thumbnail into PictureBox
           AlbumArt.Image = currentImage.GetThumbnailImage(100,100, null, System.IntPtr.Zero);
      }
      ms.Close();
}

// 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, ImageFormat.Jpeg); // <-- Error occurs on this line
ms.Position = 0;
pic.Data = TagLib.ByteVector.FromStream(ms);
f.Tag.Pictures = new TagLib.IPicture[1] { pic };
f.save();
ms.Close();

如果我加载图像,并试图挽救它的时候了我得到这个尝试读取或写入受保护的内存。这通常是指示其他内存已损坏。如果我尝试currentImage保存为ImageFormat.Bmp我得到GDI +中发生一般性错误。这

If I load the image and try to save it right away I get this "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." If I try to save currentImage as a ImageFormat.Bmp I get this "A generic error occurred in GDI+."

我保存方法正常工作,如果我从一个加载图像网址是这样的:

My save method works correctly if I load an image from a url like this:

WebRequest req = WebRequest.Create(urlToImg);
WebResponse response = req.GetResponse();
Stream stream = response.GetResponseStream();
currentImage = Image.FromStream(stream);
stream.Close();



所以我猜有一个与我加载图像到currentImage时的方式的一个问题用户从列表框中的MP3。

So I'm guessing there is an issue with the way that I am loading the image into currentImage when the user selects an MP3 from the listbox.

我已经找到了很多装载的例子,并保存图像为MP3的,但似乎没有人,当他们尝试是具有这个问题保存立即加载后。

I have found a lot of examples of loading and saving images to mp3's but no one seems to be having this issue when they try to save the are immediately after loading it.

推荐答案

感谢您的帮助吉姆,但我不能真正得到它的工作使用'使用'块所以我猜流还在收盘某处。我发现另一种方法做什么,我是通过存储/保存一个byte [],而不是图像寻找。然后只用保存:

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();
}

这篇关于装载专辑封面与标签库锋利,然后将其保存在C#相同/不同的文件会导致内存错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆