使用 taglib 在 WPF 中的图像框中显示封面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17904184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using taglib to display the cover art in a Image box in WPF
提问by Noberto Pess?a
I'm making a player and I'm stuck in a apparently simple problem. I need to make the cover art of the song to be displayed in one Image box. I found these two solutions:
我正在制作一个播放器,但遇到了一个看似简单的问题。我需要使歌曲的封面艺术显示在一个图像框中。我找到了这两个解决方案:
This:
这个:
var file = TagLib.File.Create(filename);
if (file.Tag.Pictures.Length >= 1)
{
var bin = (byte[])(file.Tag.Pictures[0].Data.Data);
PreviewPictureBox.Image = Image.FromStream(new MemoryStream(bin)).GetThumbnailImage(100, 100, null, IntPtr.Zero);
}
and this:
和这个:
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();
}
But both are to Windows Forms, I suppose, because I have problems with them.
但我想,两者都是 Windows 窗体,因为我对它们有问题。
I'm not sure which solution makes the most sense. Could anyone give me some pointers?
我不确定哪种解决方案最有意义。谁能给我一些指点?
采纳答案by Nitesh
Use System.Windows.Controls.Imageto display your images on UI. You must set it's Source property in order to provide image data to render on UI.
使用System.Windows.Controls.Image在 UI 上显示您的图像。您必须设置它的 Source 属性才能提供要在 UI 上呈现的图像数据。
// Load you image data in MemoryStream
TagLib.IPicture pic = f.Tag.Pictures[0];
MemoryStream ms = new MemoryStream(pic.Data.Data);
ms.Seek(0, SeekOrigin.Begin);
// ImageSource for System.Windows.Controls.Image
BitmapImage bitmap= new BitmapImage();
bitmap.BeginInit();
bitmap.StreamSource = ms;
bitmap.EndInit();
// Create a System.Windows.Controls.Image control
System.Windows.Controls.Image img = new System.Windows.Controls.Image();
img.Source = bitmap;
Then you can add/place this Image control to UI.
然后您可以将此图像控件添加/放置到 UI。

