【问题标题】:Using taglib to display the cover art in a Image box in WPF使用 taglib 在 WPF 中的图像框中显示封面艺术
【发布时间】:2013-07-28 01:42:26
【问题描述】:

我正在制作一个播放器,但我遇到了一个看似简单的问题。 我需要将歌曲的封面艺术显示在一个图像框中。 我找到了这两个解决方案:

这个:

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 窗体,因为我对它们有问题。

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

【问题讨论】:

  • 你有什么问题?

标签: c# wpf taglib cover


【解决方案1】:

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

【讨论】:

  • 好的!我得到了它!我只是在歌曲没有翻唱时遇到问题,但我使用 try-catch 修复了它。非常感谢!
  • 是的,你可以添加一个if条件来检查cover是否为空。
猜你喜欢
  • 1970-01-01
  • 2011-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-03
  • 2015-04-23
  • 2010-09-25
  • 1970-01-01
相关资源
最近更新 更多