public byte[] GetAlbumArtByAlbum(Album album) { byte[] imageData = null; Track foundTrack = null; iTunesApp iTunes = null; try { iTunes = new iTunesApp(); foreach (IITFileOrCDTrack track in iTunes.LibraryPlaylist.Search(album.Title, ITPlaylistSearchField.ITPlaylistSearchFieldAlbums)) { if (!string.Equals(track.Artist, album.ArtistName)) { continue; } foundTrack = track.GetTrack(); break; } if (foundTrack != null && File.Exists(foundTrack.FilePath)) { using (FileStream fileStream = new FileStream(foundTrack.FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { try { TagContainer tags = new Id3TagManager().ReadV2Tag(fileStream); if (tags != null) { IFrame apicFrame = tags.SearchFrame("APIC"); if (apicFrame != null) { PictureFrame pictureFrame = FrameUtilities.ConvertToPictureFrame(apicFrame); imageData = pictureFrame.PictureData.ToArray<byte>(); } } } catch (Id3TagException) { /* Do nothing */} } } return imageData; } catch (Exception ex) { LogUtility.LogException(ex); throw; } finally { // Marshall.ReleaseComObject? } }
public byte[] GetAlbumArtByTrack(Track track) { byte[] imageData = null; iTunesApp iTunes = null; try { iTunes = new iTunesApp(); if (track != null && File.Exists(track.FilePath)) { using (FileStream fileStream = new FileStream(track.FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { try { TagContainer tags = new Id3TagManager().ReadV2Tag(fileStream); if (tags != null) { IFrame apicFrame = tags.SearchFrame("APIC"); if (apicFrame != null) { PictureFrame pictureFrame = FrameUtilities.ConvertToPictureFrame(apicFrame); imageData = pictureFrame.PictureData.ToArray<byte>(); } } } catch (Id3TagException) { /* Do nothing */} } } return imageData; } catch (Exception ex) { LogUtility.LogException(ex); throw; } finally { // Marshall.ReleaseComObject? } }
private byte[] GetTrackArt(WindowsMediaPlayer player, IWMPMedia track, string collectionId) { byte[] imageData = null; if (!string.IsNullOrEmpty(track.sourceURL) && File.Exists(track.sourceURL)) { // Try and find the album art that WMP, WMC, Zune creates. I'd much rather read from // a local JPEG than try to read from media that's currently playing and probably locked FileInfo pathInfo = new FileInfo(track.sourceURL); //LogUtility.LogMessage(pathInfo.FullName + " exists"); FileInfo albumArtInfo = new FileInfo(string.Format("{0}{1}AlbumArt_{2}_Large.jpg", pathInfo.DirectoryName, Path.DirectorySeparatorChar, collectionId)); if (!albumArtInfo.Exists) { albumArtInfo = new FileInfo(string.Format("{0}{1}AlbumArt_{2}_Small.jpg", pathInfo.DirectoryName, Path.DirectorySeparatorChar, collectionId)); } if (!albumArtInfo.Exists) { albumArtInfo = new FileInfo(string.Format("{0}{1}ZuneAlbumArt.jpg", pathInfo.DirectoryName, Path.DirectorySeparatorChar)); } //LogUtility.LogMessage("Album art path is " + albumArtInfo.FullName); if (albumArtInfo.Exists) { //LogUtility.LogMessage("Album art exists"); using (FileStream artStream = albumArtInfo.OpenRead()) { byte[] artInfo = new byte[artStream.Length]; artStream.Read(artInfo, 0, (int)artStream.Length); imageData = artInfo; } } } // Couldn't get the art from the folder.jpg, so now try to read the id3 tag if (File.Exists(track.sourceURL)) { using (FileStream fileStream = new FileStream(track.sourceURL, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { try { TagContainer tags = new Id3TagManager().ReadV2Tag(fileStream); if (tags != null) { IFrame apicFrame = tags.SearchFrame("APIC"); if (apicFrame != null) { PictureFrame pictureFrame = FrameUtilities.ConvertToPictureFrame(apicFrame); imageData = pictureFrame.PictureData.ToArray<byte>(); } } } catch (Id3TagException) { /* Do nothing */} } } return imageData; }