我的应用程序列出了一个目录中的所有 MP3,当用户选择一个文件时,它会加载标签信息,包括专辑封面。艺术品被加载到一个变量中,以便在用户保存数据时使用。艺术作品也被加载到相框中供用户查看。
// 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+ 中发生一般错误”。
如果我从这样的 url 加载图像,我的保存方法可以正常工作:
WebRequest req = WebRequest.Create(urlToImg);
WebResponse response = req.GetResponse();
Stream stream = response.GetResponseStream();
currentImage = Image.FromStream(stream);
stream.Close();
所以我猜当用户从列表框中选择 MP3 时,我将图像加载到 currentImage 的方式存在问题。
我发现了很多将图像加载和保存到 mp3 的示例,但是当他们在加载后立即尝试保存时似乎没有人遇到这个问题。