/// <summary> /// Get Cover Art via Amazon Webservice /// </summary> private void GetCoverArtThread() { log.Trace(">>>"); //Make calls to Tracksgrid Threadsafe if (tracksGrid.InvokeRequired) { ThreadSafeGridDelegate d = GetCoverArtThread; tracksGrid.Invoke(d, new object[] { }); return; } int count = 0; int trackCount = tracksGrid.SelectedRows.Count; SetProgressBar(trackCount); AmazonAlbum amazonAlbum = null; bool isMultipleArtistAlbum = false; string savedArtist = ""; string savedAlbum = ""; string savedFolder = ""; Core.Common.Picture folderThumb = null; // Find out, if we deal with a multiple artist album and submit only the album name // If we have different artists, then it is a multiple artist album. // BUT: if the album is different, we don't have a multiple artist album and should submit the artist as well foreach (DataGridViewRow row in tracksGrid.Rows) { ClearStatusColumn(row.Index); if (!row.Selected) { continue; } TrackData track = Options.Songlist[row.Index]; if (savedArtist == "") { savedArtist = track.Artist; savedAlbum = track.Album; } if (savedArtist != track.Artist) { isMultipleArtistAlbum = true; } if (savedAlbum != track.Album) { isMultipleArtistAlbum = false; break; } } if (isMultipleArtistAlbum) { log.Debug("CoverArt: Album contains Multiple Artists, just search for the album"); } Dictionary<string, AmazonAlbum> savedCoverCash = new Dictionary<string, AmazonAlbum>(); foreach (DataGridViewRow row in tracksGrid.Rows) { ClearStatusColumn(row.Index); if (!row.Selected) { continue; } count++; try { Application.DoEvents(); _main.progressBar1.Value += 1; if (_progressCancelled) { ResetProgressBar(); return; } TrackData track = Options.Songlist[row.Index]; Util.SendProgress(string.Format("Search coverart for {0}", track.FileName)); log.Debug("CoverArt: Retrieving coverart for: {0} - {1}", track.Artist, track.Album); // Should we take an existing folder.jpg instead of searching the web if (Options.MainSettings.EmbedFolderThumb && !Options.MainSettings.OnlySaveFolderThumb) { if (folderThumb == null || Path.GetDirectoryName(track.FullFileName) != savedFolder) { savedFolder = Path.GetDirectoryName(track.FullFileName); folderThumb = GetFolderThumb(savedFolder); } if (folderThumb != null) { // Only write a picture if we don't have a picture OR Overwrite Pictures is set if (track.Pictures.Count == 0 || Options.MainSettings.OverwriteExistingCovers) { if (Options.MainSettings.ChangeCoverSize && Picture.ImageFromData(folderThumb.Data).Width > Options.MainSettings.MaxCoverWidth) { folderThumb.Resize(Options.MainSettings.MaxCoverWidth); } log.Debug("CoverArt: Using existing folder.jpg"); // First Clear all the existingPictures track.Pictures.Clear(); track.Pictures.Add(folderThumb); SetBackgroundColorChanged(row.Index); track.Changed = true; Options.Songlist[row.Index] = track; _itemsChanged = true; _main.SetGalleryItem(); } continue; } } // If we don't have an Album don't do any query if (track.Album == "") continue; string coverSearchString = track.Artist + track.Album; if (isMultipleArtistAlbum) { coverSearchString = track.Album; } bool foundInCash = savedCoverCash.ContainsKey(coverSearchString); if (foundInCash) { amazonAlbum = savedCoverCash[coverSearchString]; } else { amazonAlbum = null; } // Only retrieve the Cover Art, if we don't have it yet) if (!foundInCash || amazonAlbum == null) { CoverSearch dlgAlbumResults = new CoverSearch(); dlgAlbumResults.Artist = isMultipleArtistAlbum ? "" : track.Artist; dlgAlbumResults.Album = track.Album; dlgAlbumResults.FileDetails = track.FullFileName; dlgAlbumResults.Owner = _main; amazonAlbum = null; DialogResult dlgResult = _main.ShowModalDialog(dlgAlbumResults); if (dlgResult == DialogResult.OK) { if (dlgAlbumResults.SelectedAlbum != null) { amazonAlbum = dlgAlbumResults.SelectedAlbum; } } else if (dlgResult == DialogResult.Abort) { log.Debug("CoverArt: Search for all albums cancelled"); break; } else { log.Debug("CoverArt: Album Selection cancelled"); continue; } dlgAlbumResults.Dispose(); } // Now update the Cover Art if (amazonAlbum != null) { if (!savedCoverCash.ContainsKey(coverSearchString)) { savedCoverCash.Add(coverSearchString, amazonAlbum); } // Only write a picture if we don't have a picture OR Overwrite Pictures is set); if ((track.Pictures.Count == 0 || Options.MainSettings.OverwriteExistingCovers) && !Options.MainSettings.OnlySaveFolderThumb) { track.Pictures.Clear(); ByteVector vector = amazonAlbum.AlbumImage; if (vector != null) { MPTagThat.Core.Common.Picture pic = new MPTagThat.Core.Common.Picture(); pic.MimeType = "image/jpg"; pic.Description = "Front Cover"; pic.Type = PictureType.FrontCover; pic.Data = vector.Data; if (Options.MainSettings.ChangeCoverSize && Picture.ImageFromData(pic.Data).Width > Options.MainSettings.MaxCoverWidth) { pic.Resize(Options.MainSettings.MaxCoverWidth); } track.Pictures.Add(pic); } // And also set the Year from the Release Date delivered by Amazon // only if not present in Track if (amazonAlbum.Year != null) { string strYear = amazonAlbum.Year; if (strYear.Length > 4) strYear = strYear.Substring(0, 4); int year = 0; try { year = Convert.ToInt32(strYear); } catch (Exception) { } if (year > 0 && track.Year == 0) track.Year = year; } SetBackgroundColorChanged(row.Index); track.Changed = true; Options.Songlist[row.Index] = track; _itemsChanged = true; _main.SetGalleryItem(); } } // If the user has selected to store only the folder thumb, without touching the file if (amazonAlbum != null && Options.MainSettings.OnlySaveFolderThumb) { ByteVector vector = amazonAlbum.AlbumImage; if (vector != null) { string fileName = Path.Combine(Path.GetDirectoryName(track.FullFileName), "folder.jpg"); try { MPTagThat.Core.Common.Picture pic = new MPTagThat.Core.Common.Picture(); if (Options.MainSettings.ChangeCoverSize && Picture.ImageFromData(pic.Data).Width > Options.MainSettings.MaxCoverWidth) { pic.Resize(Options.MainSettings.MaxCoverWidth); } Image img = Picture.ImageFromData(vector.Data); // Need to make a copy, otherwise we have a GDI+ Error Bitmap bmp = new Bitmap(img); bmp.Save(fileName, ImageFormat.Jpeg); FileInfo fi = new FileInfo(fileName); _nonMusicFiles.RemoveAll(f => f.Name == fi.Name); _nonMusicFiles.Add(fi); _main.MiscInfoPanel.AddNonMusicFiles(_nonMusicFiles); } catch (Exception ex) { log.Error("Exception Saving picture: {0} {1}", fileName, ex.Message); } break; } } } catch (Exception ex) { Options.Songlist[row.Index].Status = 2; AddErrorMessage(row, ex.Message); } } Util.SendProgress(""); tracksGrid.Refresh(); tracksGrid.Parent.Refresh(); _main.TagEditForm.FillForm(); ResetProgressBar(); log.Trace("<<<"); }
/// <summary> /// Get the Cover Image from Amazon /// </summary> /// <param name = "sender"></param> /// <param name = "e"></param> private void buttonGetPictureInternet_Click(object sender, EventArgs e) { string searchArtist = ""; string searchAlbum = ""; if (_isMultiTagEdit) { searchArtist = cbArtist.Text.Trim(); searchAlbum = cbAlbum.Text.Trim(); } else { searchArtist = tbArtist.Text.Trim(); searchAlbum = tbAlbum.Text.Trim(); } if (searchAlbum.Length == 0) { return; } Cursor = Cursors.WaitCursor; CoverSearch dlgAlbumResults = new CoverSearch(); dlgAlbumResults.Artist = searchArtist; dlgAlbumResults.Album = searchAlbum; dlgAlbumResults.Owner = main; Album album = null; if (main.ShowModalDialog(dlgAlbumResults) == DialogResult.OK) { if (dlgAlbumResults.SelectedAlbum != null) { album = dlgAlbumResults.SelectedAlbum; } } else { log.Debug("CoverArt: Album Selection cancelled"); } dlgAlbumResults.Dispose(); if (album == null) { return; } if (checkBoxRemoveExistingPictures.Checked) { dataGridViewPicture.Rows.Clear(); _pictures.Clear(); } ByteVector vector = album.AlbumImage; if (vector != null) { _pic = new Picture(); _pic.MimeType = "image/jpg"; _pic.Description = ""; _pic.Type = TagLib.PictureType.FrontCover; _pic.Data = vector.Data; AddPictureToList(); AddPictureToPictureBox(); _pictureIsChanged = true; } Cursor = Cursors.Default; }
public override bool Execute(ref TrackData track, int rowIndex) { Util.SendProgress(string.Format("Search coverart for {0}", track.FileName)); Log.Debug("CoverArt: Retrieving coverart for: {0} - {1}", track.Artist, track.Album); // Should we take an existing folder.jpg instead of searching the web if (Options.MainSettings.EmbedFolderThumb && !Options.MainSettings.OnlySaveFolderThumb) { if (_folderThumb == null || Path.GetDirectoryName(track.FullFileName) != _savedFolder) { _savedFolder = Path.GetDirectoryName(track.FullFileName); _folderThumb = Util.GetFolderThumb(_savedFolder); } if (_folderThumb != null) { // Only write a picture if we don't have a picture OR Overwrite Pictures is set if (track.Pictures.Count == 0 || Options.MainSettings.OverwriteExistingCovers) { if (Options.MainSettings.ChangeCoverSize && Core.Common.Picture.ImageFromData(_folderThumb.Data).Width > Options.MainSettings.MaxCoverWidth) { _folderThumb.Resize(Options.MainSettings.MaxCoverWidth); } Log.Debug("CoverArt: Using existing folder.jpg"); // First Clear all the existingPictures track.Pictures.Clear(); track.Pictures.Add(_folderThumb); TracksGrid.MainForm.SetGalleryItem(); } return true; } } // If we don't have an Album don't do any query if (track.Album == "") return true; string coverSearchString = track.Artist + track.Album; if (_isMultipleArtistAlbum) { coverSearchString = track.Album; } bool foundInCash = _savedCoverCash.ContainsKey(coverSearchString); if (foundInCash) { _album = _savedCoverCash[coverSearchString]; } else { _album = null; } // Only retrieve the Cover Art, if we don't have it yet) if (!foundInCash || _album == null) { CoverSearch dlgAlbumResults = new CoverSearch(); dlgAlbumResults.Artist = _isMultipleArtistAlbum ? "" : track.Artist; dlgAlbumResults.Album = track.Album; dlgAlbumResults.FileDetails = track.FullFileName; dlgAlbumResults.Owner = TracksGrid.MainForm; dlgAlbumResults.StartPosition = FormStartPosition.CenterParent; _album = null; DialogResult dlgResult = dlgAlbumResults.ShowDialog(); if (dlgResult == DialogResult.OK) { if (dlgAlbumResults.SelectedAlbum != null) { _album = dlgAlbumResults.SelectedAlbum; } } else if (dlgResult == DialogResult.Abort) { Log.Debug("CoverArt: Search for all albums cancelled"); ProgressCancelled = true; return false; } else { Log.Debug("CoverArt: Album Selection cancelled"); return false; } dlgAlbumResults.Dispose(); } // Now update the Cover Art if (_album != null) { if (!_savedCoverCash.ContainsKey(coverSearchString)) { _savedCoverCash.Add(coverSearchString, _album); } // Only write a picture if we don't have a picture OR Overwrite Pictures is set); if ((track.Pictures.Count == 0 || Options.MainSettings.OverwriteExistingCovers) && !Options.MainSettings.OnlySaveFolderThumb) { track.Pictures.Clear(); ByteVector vector = _album.AlbumImage; if (vector != null) { var pic = new Core.Common.Picture(); pic.MimeType = "image/jpg"; pic.Description = "Front Cover"; pic.Type = PictureType.FrontCover; pic.Data = vector.Data; if (Options.MainSettings.ChangeCoverSize && Core.Common.Picture.ImageFromData(pic.Data).Width > Options.MainSettings.MaxCoverWidth) { pic.Resize(Options.MainSettings.MaxCoverWidth); } track.Pictures.Add(pic); } // And also set the Year from the Release Date delivered by Amazon // only if not present in Track if (_album.Year != null) { string strYear = _album.Year; if (strYear.Length > 4) strYear = strYear.Substring(0, 4); int year = 0; try { year = Convert.ToInt32(strYear); } catch (Exception) { // ignored } if (year > 0 && track.Year == 0) track.Year = year; } TracksGrid.MainForm.SetGalleryItem(); } } // If the user has selected to store only the folder thumb, without touching the file if (_album != null && Options.MainSettings.OnlySaveFolderThumb) { ByteVector vector = _album.AlbumImage; if (vector != null) { string fileName = Path.Combine(Path.GetDirectoryName(track.FullFileName), "folder.jpg"); try { Core.Common.Picture pic = new Core.Common.Picture(); if (Options.MainSettings.ChangeCoverSize && Core.Common.Picture.ImageFromData(pic.Data).Width > Options.MainSettings.MaxCoverWidth) { pic.Resize(Options.MainSettings.MaxCoverWidth); } Image img = Core.Common.Picture.ImageFromData(vector.Data); // Need to make a copy, otherwise we have a GDI+ Error Bitmap bmp = new Bitmap(img); bmp.Save(fileName, ImageFormat.Jpeg); FileInfo fi = new FileInfo(fileName); TracksGrid.NonMusicFiles.RemoveAll(f => f.Name == fi.Name); TracksGrid.NonMusicFiles.Add(fi); TracksGrid.MainForm.MiscInfoPanel.AddNonMusicFiles(TracksGrid.NonMusicFiles); } catch (Exception ex) { Log.Error("Exception Saving picture: {0} {1}", fileName, ex.Message); } return false; } } return true; }