/// <summary> /// Downloads a cover and sets it to a picturebox. /// </summary> /// <param name="coverArtBox"> /// The picturebox to display the cover. /// </param> /// <param name="coverID"> /// The cover id. /// </param> /// <param name="drawReflection"> /// The draw reflection. /// </param> public void SetCoverArt(Image coverArtBox, string coverID, Constants.CoverType coverType) { string url = null; string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + Constants.AlbumCoverPath; switch (coverType) { case Constants.CoverType.Album: url = _server + "getCoverArt.view?id=" + coverID + _authentication + "size=" + Constants.MaxCoverSize; break; case Constants.CoverType.Artist: path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + Constants.ArtistCoverPath; break; } string imagePath = path + coverID + ".png"; if (File.Exists(imagePath)) { coverArtBox.Source = new BitmapImage(new Uri(imagePath)); coverArtBox.Stretch = Stretch.UniformToFill; return; } if (coverType == Constants.CoverType.Artist) { url = LastFm.GetLastFmArtistUrl(coverID); } if (string.IsNullOrEmpty(url)) { url = _server + "getCoverArt.view?id=" + "1" + _authentication + "size=" + Constants.MaxCoverSize; } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var webClient = new WebClient(); //webClient.DownloadDataCompleted += delegate(object sender, DownloadDataCompletedEventArgs e) //{ //byte[] graphicData = e.Result; byte[] graphicData = webClient.DownloadData(url); System.Drawing.Image img = null; using (var ms = new MemoryStream(graphicData, 0, graphicData.Length)) { ms.Write(graphicData, 0, graphicData.Length); try { img = System.Drawing.Image.FromStream(ms, true); img.Save(imagePath, ImageFormat.Png); } catch (ArgumentException) { img = null; } catch (Exception e) { MessageBox.Show(e.Message); } } if (img == null) { return; } coverArtBox.Source = new BitmapImage(new Uri(imagePath)); coverArtBox.Stretch = Stretch.UniformToFill; //}; //webClient.DownloadDataAsync(new Uri(url)); }
public string GetCoverArtPath(MediaItem mediaItem, Constants.CoverType coverType) { string url = null; var assemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); string path = Path.Combine(assemblyPath, "cache"); switch (coverType) { case Constants.CoverType.Album: path = Path.Combine(path, "albums"); break; case Constants.CoverType.Artist: path = Path.Combine(path, "artists"); break; } if (!path.EndsWith("\\")) { path = path + "\\"; } string fileName = GeneralExtensions.CleanFileName(mediaItem.AlbumName + ".png"); string imagePath = GeneralExtensions.CleanPath(path) + fileName; if (!File.Exists(imagePath)) { if (coverType == Constants.CoverType.Artist) { url = LastFm.GetLastFmArtistUrl(mediaItem.ArtistName); } else { url = LastFm.GetLastFmAlbumUrl(mediaItem); } // last fm does not know this artist if (string.IsNullOrEmpty(url)) { string backupFile = GeneralExtensions.CleanPath(path) + nameof(Resources.unknownartist) + ".png"; if (!File.Exists(backupFile)) { if (Resources.unknownartist != null) { File.WriteAllBytes(backupFile, (byte[])converter.ConvertTo(Resources.unknownartist, typeof(byte[]))); } } return(backupFile); } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var webClient = new WebClient(); //webClient.DownloadDataCompleted += delegate(object sender, DownloadDataCompletedEventArgs e) //{ //byte[] graphicData = e.Result; byte[] graphicData = webClient.DownloadData(url); System.Drawing.Image img = null; using (var ms = new MemoryStream(graphicData, 0, graphicData.Length)) { ms.Write(graphicData, 0, graphicData.Length); try { img = System.Drawing.Image.FromStream(ms, true); img.Save(imagePath, ImageFormat.Png); } catch (Exception e) { MessageBox.Show(e.Message); } } } return(imagePath); }