-1

Hi when I try to get the album cover from a flac file using Taglib I get this error

Index was outside the bounds of the array.

Even though I know that the flac file contains an image. I'm using this code:

TagLib.File f = TagLib.File.Create(path); 
if(f.Tag.Pictures.Length == 0) return;
var bin = (byte[])(f.Tag.Pictures[0].Data.Data);
songData.Cover = Image.FromStream(new MemoryStream(bin));

The code works with mp3 files but not with flac. So how do I get the album cover in c#?

Edit: I have found out that the code work with some flac files but not with all?

Kyeman
  • 57
  • 1
  • 5

1 Answers1

1

If you want to access element of array you should always check if it's not null and if it contains any element:

TagLib.File f = TagLib.File.Create(path);
if(f.Tag.Pictures != null && f.Tag.Pictures.Length != 0)
{
    var bin = (byte[])(f.Tag.Pictures[0].Data.Data);
    songData.Cover = Image.FromStream(new MemoryStream(bin));
}
kmatyaszek
  • 19,016
  • 9
  • 60
  • 65
  • Yes I know but that's not the problem the problem is that it don't find the picture even though I know there is one. – Kyeman Jan 02 '15 at 13:55
  • By using any media program with support for flac files like vlc media player or foobar2000 – Kyeman Jan 02 '15 at 14:26
  • @Kyeman I think that your flac file don't have picture, probably you have some .jpg files in your folder where is your flac file and this file is loaded by media program. – kmatyaszek Jan 02 '15 at 14:32
  • flac files does support Pictures – Kyeman Jan 02 '15 at 14:55