5

I am wanting to display the album artwork of a song (accessed via the taglib-sharp library) within a Windows Forms picture box. The problem I'm running into is that the taglib-library returns an image of type TagLib.IPicture whereas the picture box requires an object of type System.Drawing.Image.

I have scoured the internet for many hours now, looking for a way to convert from an IPicture to Image, but to no avail. The best lead I have is this: http://msdn.microsoft.com/en-us/library/system.windows.forms.axhost.getpicturefromipicture.aspx, but I have yet to see a successful example of how to implement this.

Any help as to how to convert between these two types would be much appreciated. Note: IPicture is not analogous to IPictureDisp in this case.

mattkgross
  • 791
  • 2
  • 12
  • 24

1 Answers1

12

I've done the opposite before - turning an existing .jpg into an IPicture for embedding in an .mp3 file. I just tried reversing that operation and, after tweaking and testing, came up with this:

TagLib.File tagFile = TagLib.File.Create(mp3FilePath);
MemoryStream ms = new MemoryStream(tagFile.Tag.Pictures[0].Data.Data);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms);

Thanks for the question - I already know how I'm going to use this myself!

Update: Here's the other way (.jpg to IPicture that I've done before):

tagFile.Tag.Pictures = new TagLib.IPicture[]
{
    new TagLib.Picture(new TagLib.ByteVector((byte[])new System.Drawing.ImageConverter().ConvertTo(System.Drawing.Image.FromFile(jpgFilePath), typeof(byte[]))))
};
Ben Allred
  • 4,544
  • 1
  • 19
  • 20
  • Still not getting it working here. Index is always outside the bounds of the array. Debugging and opening the file in other player confirms that the file has pictures. Can you point me out in the right direction? Thanks in advance –  Jun 05 '18 at 08:37