私のアプリケーションは、ディレクトリ内のすべての 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にロードして保存する例をたくさん見つけましたが、ロードした直後に画像を保存しようとすると、この問題が発生しているようには見えません。