private TagLib.IPicture WriteArtwork(IITTrack track, IITArtwork artwork) { string suffix; // ReSharper disable once SwitchStatementMissingSomeCases switch (artwork.Format) { case ITArtworkFormat.ITArtworkFormatBMP: suffix = ".bmp"; break; case ITArtworkFormat.ITArtworkFormatJPEG: suffix = ".jpg"; break; case ITArtworkFormat.ITArtworkFormatPNG: suffix = ".png"; break; default: suffix = ".jpg"; break; } var temp = Path.GetTempPath() + GetPersistentId(track) + suffix; if (File.Exists(temp)) { File.Delete(temp); } artwork.SaveArtworkToFile(temp); TagLib.IPicture picture = new TagLib.Picture(temp); File.Delete(temp); return(picture); }
private void App_OnPlayerPlayingTrackChangedEvent(object sender) { IITTrack track = this.iTunesApplication.CurrentTrack; if (!string.IsNullOrEmpty(track.Artist) && !string.IsNullOrEmpty(track.Name) && string.IsNullOrEmpty(this.iTunesApplication.CurrentStreamTitle)) { if (Globals.SaveAlbumArtwork) { try { IITArtworkCollection artworkCollection = track.Artwork; IITArtwork artwork = artworkCollection[1]; artwork.SaveArtworkToFile(this.DefaultArtworkFilePath); } catch { this.SaveBlankImage(); throw; } } TextHandler.UpdateText(track.Name, track.Artist, track.Album); } else if (!string.IsNullOrEmpty(this.iTunesApplication.CurrentStreamTitle)) { TextHandler.UpdateText(this.iTunesApplication.CurrentStreamTitle); } }
internal Track(IITTrack track) { Album = track.Album; Artist = track.Artist; Name = track.Name; _track = track; if (track.Artwork.Count > 0) { // iTunes starts at 1(!) _artwork = track.Artwork[1]; } }
public static IITArtwork GetTrackArtwork(IITTrack t) { IITArtwork theArtwork = null; if (t.Artwork.Count > 1) { long maxFileSize = 0; foreach (IITArtwork a in t.Artwork) { //bool isDownloaded = a.IsDownloadedArtwork; //string desc = a.Description; //ITArtworkFormat af = a.Format; //Type ty = af.GetType(); try { string tempFile = Path.GetTempFileName(); a.SaveArtworkToFile(tempFile); FileInfo fiTempFile = new FileInfo(tempFile); if (fiTempFile.Exists) { fiTempFile.Delete(); } long fileSize = fiTempFile.Length; if (fileSize > maxFileSize) { maxFileSize = fileSize; theArtwork = a; } } catch (Exception) { } } } if (theArtwork == null) { foreach (IITArtwork a in t.Artwork) { theArtwork = a; } } return(theArtwork); }
private void GetAlbumArt() { try { iTunesApp app = new iTunesAppClass(); IITArtworkCollection art1 = app.CurrentTrack.Artwork; IITArtwork art2 = art1[1]; art2.SaveArtworkToFile(String.Concat(Environment.CurrentDirectory, @"\Cover.jpg")); Stream r = File.Open(String.Concat(Environment.CurrentDirectory, @"\Cover.jpg"), FileMode.Open); Image temp = Image.FromStream(r); r.Close(); albumArtBox.Image = temp; _imageDirectory = String.Concat(Environment.CurrentDirectory, @"\Cover.jpg"); } catch (Exception) { albumArtBox.Image = Properties.Resources.art; _imageDirectory = String.Concat(Environment.CurrentDirectory, @"\No_Cover.jpg"); } }
public Artwork(IITArtwork artwork) { _artwork = artwork; }
private void SaveArtworkFile(IITArtwork art, string name) { try { string ext; switch (art.Format) { case ITArtworkFormat.ITArtworkFormatJPEG: ext = "jpg"; break; case ITArtworkFormat.ITArtworkFormatPNG: ext = "png"; break; case ITArtworkFormat.ITArtworkFormatBMP: ext = "bmp"; break; default: return; } name = name.NormalizeFilename(); string filepath = Path.Combine(_artfolder, name + "."); // don't write a jpg if there's already a png, no matter how different they look to CompareImages. if (!File.Exists(filepath + "jpg") && !File.Exists(filepath + "png") && !File.Exists(filepath + "bmp")) { art.SaveArtworkToFile(filepath + ext); int[] hash; using (Bitmap bmp = new Bitmap(filepath + ext)) { hash = bmp.GetHash(10); } bool found = false; foreach (var h in _hashes) { if (HashCollection.CompareHashes(hash, h.Hash) >= 97) { found = true; File.Delete(filepath + ext); } } if (!found) { _hashes.Add(new HashEntry { Filename = filepath + ext, Hash = hash }); _hashes.Write(_hashFile); } } else { string fname = File.Exists(filepath + "jpg") ? filepath + "jpg" : File.Exists(filepath + "png") ? filepath + "png" : filepath + "bmp"; int[] hash; using (Bitmap bmp = new Bitmap(fname)) { hash = bmp.GetHash(10); } if (!_hashes.Any(h => HashCollection.CompareHashes(h.Hash, hash) >= 97)) { _hashes.Add(new HashEntry { Filename = fname, Hash = hash }); _hashes.Write(_hashFile); } } } catch (Exception ex) { if (!ex.Message.Contains("track has been deleted")) { MessageBox.Show("Exception in iTunesRatingControl: " + ex.Message); } } }
private static OutputMessage ExportDirectoryArtwork(iTunesLibrary library, List <Track> trackList, string directory) { List <Track> directoryTrackList = trackList.Where(t => t.Directory == directory).ToList(); int trackCount = directoryTrackList.Count(); if (trackCount == 0) { return(new OutputMessage("No tracks found in the library for the directory: \"" + directory + "\".", MessageSeverity.Error)); } // Minimum # of tracks in a directory (regardless of whether they have artwork attached) int minTracks = iTunes_Artwork_Export.Properties.Settings.Default.MinTracks; if (trackCount < minTracks) { return(new OutputMessage("Not enough tracks in the following directory: \"" + directory + "\" (track count is " + trackCount.ToString() + "; minimum is " + minTracks.ToString() + ").", MessageSeverity.Warning)); } // Check that at least 1 track has artwork attached if (directoryTrackList.Where(t => t.ArtworkCount > 0).Count() == 0) { return(new OutputMessage("No artwork found for the tracks in directory: \"" + directory + "\".", MessageSeverity.Warning)); } Track artworkTrack = null; try { artworkTrack = GetArtworkTrack(directoryTrackList); } catch (ArgumentException argEx) { return(new OutputMessage(argEx.Message, MessageSeverity.Error)); } catch (ApplicationException appEx) { return(new OutputMessage(appEx.Message, MessageSeverity.Warning)); } catch (Exception ex) { return(new OutputMessage(ex.Message, MessageSeverity.Error)); } if (artworkTrack == null) { return(new OutputMessage("Error: No artwork track returned for directory \"" + directory + "\".", MessageSeverity.Error)); } PersistentID artworkTrackPersistentID = artworkTrack.PersistentID; IITTrack itArtworkTrack = null; try { itArtworkTrack = ExportArtwork.GetTrackByPersistentID(library, artworkTrackPersistentID); } catch (Exception ex) { return(new OutputMessage("Error when retrieving track by Persistent ID: " + ex.Message + ". Track: " + artworkTrack.ToString(true, true), MessageSeverity.Error)); } if (itArtworkTrack != null) { // Found the iTunes Track in the library with the Artwork Track's persistent ID. IITArtwork iaArt = GetTrackArtwork(itArtworkTrack); if (iaArt != null) { // Found the artwork on that track. DirectoryInfo trackDirectoryInfo = new DirectoryInfo(directory); if (trackDirectoryInfo.Exists) { string outputFileName = directory + "\\" + iTunes_Artwork_Export.Properties.Settings.Default.FileName; try { FileInfo fiOutputFile = new FileInfo(outputFileName); if (fiOutputFile.Exists) { return(new OutputMessage("File \"" + outputFileName + "\" already exists!", MessageSeverity.Debug)); } else { try { iaArt.SaveArtworkToFile(outputFileName); return(new OutputMessage("Saved artwork to: \"" + outputFileName + "\". Track: " + artworkTrack.ToString(true, false), MessageSeverity.Success)); } catch (Exception ex) { return(new OutputMessage("Error when attempting to save artwork to: \"" + outputFileName + "\": " + ex.Message, MessageSeverity.Error)); } } } catch (Exception ex) { return(new OutputMessage("Could not output artwork to \"" + outputFileName + "\": " + ex.Message + ". Track: " + artworkTrack.ToString(true, false), MessageSeverity.Error)); } } else { return(new OutputMessage("Could not output artwork to directory \"" + directory + "\": directory does not exist. Track: " + artworkTrack.ToString(true, false), MessageSeverity.Error)); } } else { return(new OutputMessage("Could not retrieve artwork for Track: " + artworkTrack.ToString(true, false), MessageSeverity.Error)); } } else { return(new OutputMessage("Could not retrieve Track by Persistent ID: " + artworkTrack.ToString(true, true), MessageSeverity.Error)); } }
private void timerUIUpdater_Tick(object sender, EventArgs e) { if (!_inStarButton) { lblTrackName.Focus(); } IITTrack track = null; try { track = _itunes.CurrentTrack as IITTrack; } catch { try { NewITunesAppClass(); } catch { return; } } if (track == null) { return; } try { switch (_timerOp) { case TimerOperation.LookForDuplicates: _timerOp = TimerOperation.CheckForNewTrack; iTunesApp itunes = new iTunesApp(); IITLibraryPlaylist library = itunes.LibraryPlaylist; int i = library.Tracks.Count; var curname = track.Name.ToLower(); var curartist = track.Artist.ToLower(); var curfile = track.Kind == ITTrackKind.ITTrackKindFile ? ((dynamic)track).Location : ""; foreach (IITTrack t in library.Tracks) { var tname = t.Name.ToLower(); var tartist = t.Artist.ToLower(); var tfile = t.Kind == ITTrackKind.ITTrackKindFile ? ((dynamic)t).Location : ""; if ((tname == curname || tname.Contains(curname) || curname.Contains(tname)) && (tartist == curartist || tartist.Contains(curartist) || curartist.Contains(tartist)) && curfile != tfile) { var dupform = new DuplicateTrack(BackgroundImage, track, t); dupform.ShowDialog(); } } break; default: // Get the location and size of the iTunes window. var processes = Process.GetProcessesByName("iTunes"); if (processes.Length == 0) { return; } var process = processes[0]; var title = process.MainWindowTitle; if (title.ToUpper() != "MINIPLAYER") { Visible = false; return; } Visible = true; var winhandle = process.MainWindowHandle; var rect = new Rectangle(); GetWindowRect(winhandle, ref rect); Point location = rect.Location; Size size = new Size(rect.Width - rect.X, rect.Height - rect.Y); Location = new Point(location.X, location.Y + size.Height); // Set the size of my window to sit underneath the iTunes window. Size = new Size(size.Width, Size.Height); // Adjust the font size as needed to get the track name to fit properly. float stringwidth; using (var gfx = lblTrackName.CreateGraphics()) { stringwidth = gfx.MeasureString(lblTrackName.Text, lblTrackName.Font).Width; } if (stringwidth + lblTrackName.Left > Width) { lblTrackName.Font = new Font(lblTrackName.Font.FontFamily, lblTrackName.Font.Size - 0.1F); } else if (lblTrackName.Font.Size < 21.75 && stringwidth + lblTrackName.Left + 5 < Width) { lblTrackName.Font = new Font(lblTrackName.Font.FontFamily, lblTrackName.Font.Size + 0.1F); } // Get the currently playing track. if (!string.IsNullOrEmpty(_curTrackPath) && string.Compare(_curTrackPath, track.Artist + "/" + track.Album + "/" + track.Name, StringComparison.OrdinalIgnoreCase) == 0) { return; } //**** //**** If we get here, there was a track change since the last timer event. //**** _timerOp = TimerOperation.LookForDuplicates; // Set the current track path. _curTrackPath = track.Artist + "/" + track.Album + "/" + track.Name; // Update the track info lblTrackName.Text = track.Name; lblArtist.Text = track.Artist; lblAlbum.Text = track.Album; lblTrackName.Visible = lblArtist.Visible = lblAlbum.Visible = true; SetStars(track.Rating, lblTrackName.ForeColor); _curRating = track.Rating; // Get the artwork. IITArtworkCollection artcollection = track.Artwork; if (artcollection == null || artcollection.Count < 1) { return; } IITArtwork art = artcollection[1]; // Save the artwork to a temporary file. string filename; switch (art.Format) { case ITArtworkFormat.ITArtworkFormatJPEG: filename = "temp.jpg"; break; case ITArtworkFormat.ITArtworkFormatPNG: filename = "temp.png"; break; default: filename = "temp.bmp"; break; } filename = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\" + filename; art.SaveArtworkToFile(filename); Color darkest = Color.Black, lightest = Color.White; int avgR, avgG, avgB; using (Image img = Image.FromFile(filename)) { using (var img2 = new Bitmap(img, new Size(10, 10))) { int darkestindex = darkest.R + darkest.G + darkest.B; int lightestindex = lightest.R + lightest.G + lightest.B; int totR = 0, totG = 0, totB = 0; for (int y = 0; y < 10; ++y) { for (int x = 0; x < 10; ++x) { var color = img2.GetPixel(x, y); int index = color.R + color.G + color.B; totR += color.R; totG += color.G; totB += color.B; if (index < darkestindex) { darkestindex = index; darkest = color; } if (index > lightestindex) { lightestindex = index; lightest = color; } } } avgR = (int)Math.Round(totR / 100F); avgG = (int)Math.Round(totG / 100F); avgB = (int)Math.Round(totB / 100F); } using (var img2 = new Bitmap(img, new Size(50, 50))) { for (int x = 0; x < 50; ++x) { int totR = 0, totG = 0, totB = 0; for (int y = 0; y < 50; ++y) { var color = img2.GetPixel(x, y); totR += color.R; totG += color.G; totB += color.B; } Color c = Color.FromArgb(160, (int)Math.Round(totR / 50F), (int)Math.Round(totG / 50F), (int)Math.Round(totB / 50F)); for (int y = 0; y < 50; ++y) { img2.SetPixel(x, y, c); } } BackgroundImage = new Bitmap(img2); } } Color forecolor = Color.FromArgb(Math.Min(Math.Max(0, 255 - avgR), 255), Math.Min(Math.Max(0, 255 - avgG), 255), Math.Min(Math.Max(0, 255 - avgB), 255)); Color backcolor = Color.FromArgb(Math.Min(Math.Max(0, avgR), 255), Math.Min(Math.Max(0, avgG), 255), Math.Min(Math.Max(0, avgB), 255)); if (ColorDist(forecolor, backcolor) < 210) { forecolor = ColorDist(backcolor, lightest) < ColorDist(backcolor, darkest) ? darkest : lightest; } //lblTrackName.OutlineColor = lightest; lblTrackName.ForeColor = lblAlbum.ForeColor = lblArtist.ForeColor = forecolor; foreach (var btn in _starsButtons) { btn.ForeColor = forecolor; } BackColor = backcolor; break; } } catch { // do nothing. } }