How to Convert IPicture to Image - .NET 4.5 TagLib Sharp

ⅰ亾dé卋堺 提交于 2019-12-10 06:26:52

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/18176757/how-to-convert-ipicture-to-image-net-4-5-taglib-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!