使用taglib在WPF的“图像”框中显示封面图样

我正在制作一名玩家而且我陷入了一个看似简单的问题。 我需要将歌曲的封面艺术显示在一个图像框中。 我找到了这两个解决方案:

这个:

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

还有这个:

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

但我认为两者都适用于Windows Forms,因为我遇到了问题。

我不确定哪种解决方案最有意义。 谁能给我一些指示?

使用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; 

然后,您可以将此Image控件添加/放置到UI。