I use TagLib to convert an album picture from mp3 to byte []:

TagLib.File tagFile = TagLib.File.Create(el); IPicture Pic = tagFile.Tag.Pictures[0]; byte[] Picture = Pic.Data.Data.ToArray(); 

I write in DB, then I take out and try to convert to BitmapImage and write to Control Image FindedSongPicture in the main window:

Converting function:

 private BitmapImage Bitmap2BitmapImage(Bitmap bitmap) { BitmapImage bi = new BitmapImage(); using (MemoryStream ms = new MemoryStream()) { bitmap.Save(ms, ImageFormat.Png); ms.Position = 0; bi.BeginInit(); bi.StreamSource = ms; bi.EndInit(); return bi; } } 

The executable code where byte [] song.songPicture:

 using (var ms = new System.IO.MemoryStream(song.songPicture)) { using (var img = Image.FromStream(ms)) { FindedSongPicture.Source = Bitmap2BitmapImage(img as Bitmap); } } 

As a result, nothing is displayed in the Image.

The first piece takes the picture of the album from the mp3 file and pulls it out byte [], TagLib allows

The second piece is the conversion function from Bitmap to BitmapImage, since in the control Image on the main form you can push only BitmapImage

The 3rd piece converts byte [] to a Bitmap, calls the Bitmap-> BitmapImage function and shoves BitmapImage into the Image control in the main window, FindedSongPicture is the name of the Image control in the main window

song.songPicture this byte []

TagLib - a library for working with tags mp3 file

What is the problem?

    1 answer 1

    Something you have is too complicated. A sequence of bytes in the MemoryStream , from it in System.Windows.Forms.Bitmap , from it to another MemoryStream , from it to BitmapImage . A lot of opportunities to make a mistake on the road.

    Try easier:

     using (var ms = new MemoryStream(song.songPicture)) { FindedSongPicture.Source = BitmapFrame.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); } 

    Well, or if you want to use BitmapImage , add bi.CacheOption = BitmapCacheOption.OnLoad; before bi.EndInit() . And maybe more bi.Freeze() .

    • Again, thanks, I just converted a bunch of answers on this issue into one - zaki hatfild
    • Most solve the problem correctly, ¯ \ _ (ツ) _ / ¯ - VladD