使用标签库,以显示在WPF中图像框中封面 [英] Using taglib to display the cover art in a Image box in WPF

查看:299
本文介绍了使用标签库,以显示在WPF中图像框中封面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做的球员,我被困在一个看似简单的问题。
我需要将显示在一个图像框中歌曲的封面。
我发现这两种解决方案:

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:

这:

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窗体,我想,因为我与他们的问题

But both are to Windows Forms, I suppose, because I have problems with them.

我不知道哪种解决方案是最有意义的。任何人都可以给我一些指点?

I'm not sure which solution makes the most sense. Could anyone give me some pointers?

推荐答案

使用 System.Windows.Controls.Image 以显示您的在UI图像。您必须设置它的Source属性以提供图像数据来呈现的UI。

Use System.Windows.Controls.Image to display your images on UI. You must set it's Source property in order to provide image data to render on 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。

Then you can add/place this Image control to UI.

这篇关于使用标签库,以显示在WPF中图像框中封面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆