public static void ShowInExplorerExec(object sender, ExecutedRoutedEventArgs e) { string filePath = e.Parameter as string; if (!String.IsNullOrEmpty(filePath)) { //TODO: Validation that this is a valid file path? if (EmbeddedArtHelpers.IsEmbeddedArtPath(filePath)) { int ignored; EmbeddedArtHelpers.SplitToFilenameAndIndex(filePath, out filePath, out ignored); } System.Diagnostics.Process.Start("explorer.exe", "/select,\"" + filePath + "\""); } }
protected override void SearchInternal(string artist, string album, AlbumArtDownloader.Scripts.IScriptResults results) { //Add the pattern used to the history list. CustomSettingsUI.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.DataBind, new System.Threading.ThreadStart(delegate { ((LocalFilesSourceSettings)CustomSettingsUI).mSearchPathPatternBox.AddPatternToHistory(); })); int lastMinImageSizeSet = 0; //Avoid duplicates StringDictionary addedFiles = new StringDictionary(); string pathPattern = GetSearchPath(artist, album); foreach (string alternate in pathPattern.Split('|')) { int? embeddedIndex = null; string unembeddedPathPattern = alternate; if (EmbeddedArtHelpers.IsEmbeddedArtPath(alternate)) { //TODO: Allow a pattern to specify multiple embedded images as "<?>" or similar. int i; EmbeddedArtHelpers.SplitToFilenameAndIndex(alternate, out unembeddedPathPattern, out i); embeddedIndex = i; } //Match path with wildcards foreach (string filename in Common.ResolvePathPattern(unembeddedPathPattern)) { if (!addedFiles.ContainsKey(filename)) //Don't re-add a file that's already been added { addedFiles.Add(filename, null); if (embeddedIndex.HasValue) { //Read embedded image from file, rather than the file itself as an image TagLib.File fileTags = null; try { fileTags = TagLib.File.Create(filename, TagLib.ReadStyle.None); var embeddedPictures = fileTags.Tag.Pictures; if (embeddedIndex.Value == -1) //Special value indicating "all embedded images" { for (int i = 0; i < embeddedPictures.Length; i++) { AddEmbeddedPictureToResults(results, embeddedPictures, i, filename); } } else if (embeddedPictures.Length > embeddedIndex.Value) { //Found the embedded image AddEmbeddedPictureToResults(results, embeddedPictures, embeddedIndex.Value, filename); } else { System.Diagnostics.Trace.WriteLine("Skipping file missing specified embedded image in local file search: " + EmbeddedArtHelpers.GetEmbeddedFilePath(filename, embeddedIndex.Value)); } } catch (Exception e) { System.Diagnostics.Trace.WriteLine("Skipping unreadable embedded image from file in local file search: " + EmbeddedArtHelpers.GetEmbeddedFilePath(filename, embeddedIndex.Value)); System.Diagnostics.Trace.Indent(); System.Diagnostics.Trace.WriteLine(e.Message); System.Diagnostics.Trace.Unindent(); } finally { if (fileTags != null) { fileTags.Mode = TagLib.File.AccessMode.Closed; } } } else { //Each filename is potentially an image, so try to load it try { using (var filestream = File.OpenRead(filename)) { var decoder = BitmapDecoder.Create(filestream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None); //Don't cache, only want the width and height, and delay creation as no need to actually read the data. var frame = decoder.Frames[0]; int width = frame.PixelWidth; int height = frame.PixelHeight; results.Add(File.OpenRead(filename), Path.GetFileName(filename), filename, width, height, null); if (SetMinimumSizeFilter) { var minImageSize = Math.Min(width, height); if (lastMinImageSizeSet == 0) { Properties.Settings.Default.UseMinimumImageSize = true; Properties.Settings.Default.MinimumImageSize = lastMinImageSizeSet = minImageSize; } else if (Properties.Settings.Default.UseMinimumImageSize && Properties.Settings.Default.MinimumImageSize == lastMinImageSizeSet) // Don't override during a search if the user has made manual changes { Properties.Settings.Default.MinimumImageSize = Math.Max(lastMinImageSizeSet, minImageSize); // If this image is bigger, use it as the minimum size } } } } catch (Exception e) { System.Diagnostics.Trace.WriteLine("Skipping unreadable file in local file search: " + filename); System.Diagnostics.Trace.Indent(); System.Diagnostics.Trace.WriteLine(e.Message); System.Diagnostics.Trace.Unindent(); } } } } } }
protected override void SearchInternal(string artist, string album, AlbumArtDownloader.Scripts.IScriptResults results) { //Add the pattern used to the history list. CustomSettingsUI.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.DataBind, new System.Threading.ThreadStart(delegate { ((LocalFilesSourceSettings)CustomSettingsUI).mSearchPathPatternBox.AddPatternToHistory(); })); //Avoid duplicates StringDictionary addedFiles = new StringDictionary(); string pathPattern = GetSearchPath(artist, album); foreach (string alternate in pathPattern.Split('|')) { int? embeddedIndex = null; string unembeddedPathPattern = alternate; if (EmbeddedArtHelpers.IsEmbeddedArtPath(alternate)) { //TODO: Allow a pattern to specify multiple embedded images as "<?>" or similar. int i; EmbeddedArtHelpers.SplitToFilenameAndIndex(alternate, out unembeddedPathPattern, out i); embeddedIndex = i; } //Match path with wildcards foreach (string filename in Common.ResolvePathPattern(unembeddedPathPattern)) { if (!addedFiles.ContainsKey(filename)) //Don't re-add a file that's already been added { addedFiles.Add(filename, null); if (embeddedIndex.HasValue) { //Read embedded image from file, rather than the file itself as an image TagLib.File fileTags = null; try { fileTags = TagLib.File.Create(filename, TagLib.ReadStyle.None); var embeddedPictures = fileTags.Tag.Pictures; if (embeddedIndex.Value == -1) //Special value indicating "all embedded images" { for (int i = 0; i < embeddedPictures.Length; i++) { AddEmbeddedPictureToResults(results, embeddedPictures, i, filename); } } else if (embeddedPictures.Length > embeddedIndex.Value) { //Found the embedded image AddEmbeddedPictureToResults(results, embeddedPictures, embeddedIndex.Value, filename); } else { System.Diagnostics.Trace.WriteLine("Skipping file missing specified embedded image in local file search: " + EmbeddedArtHelpers.GetEmbeddedFilePath(filename, embeddedIndex.Value)); } } catch (Exception e) { System.Diagnostics.Trace.WriteLine("Skipping unreadable embedded image from file in local file search: " + EmbeddedArtHelpers.GetEmbeddedFilePath(filename, embeddedIndex.Value)); System.Diagnostics.Trace.Indent(); System.Diagnostics.Trace.WriteLine(e.Message); System.Diagnostics.Trace.Unindent(); } finally { if (fileTags != null) { fileTags.Mode = TagLib.File.AccessMode.Closed; } } } else { //Each filename is potentially an image, so try to load it try { IntPtr hBitmap; int status = GdipCreateBitmapFromFile(filename, out hBitmap); GdipDisposeImage(new HandleRef(this, hBitmap)); if (status == 0) { //Successfully opened as image //Create an in-memory copy so that the bitmap file isn't in use, and can be replaced byte[] fileBytes = File.ReadAllBytes(filename); //Read the file, closing it after use Bitmap bitmap = new Bitmap(new MemoryStream(fileBytes)); //NOTE: Do not dispose of MemoryStream, or it will cause later saving of the bitmap to throw a generic GDI+ error (annoyingly) results.Add(bitmap, Path.GetFileName(filename), filename, bitmap.Width, bitmap.Height, null); } else { System.Diagnostics.Trace.WriteLine("Skipping non-bitmap file in local file search: " + filename); } } catch (Exception e) { System.Diagnostics.Trace.WriteLine("Skipping unreadable file in local file search: " + filename); System.Diagnostics.Trace.Indent(); System.Diagnostics.Trace.WriteLine(e.Message); System.Diagnostics.Trace.Unindent(); } } } } } }
/// <summary> /// Sets all properties related to an album having a file present: /// <see cref="ArtFile"/> set to <paramref name="filePath"/> /// <see cref="ArtFileStatus"/> set to <see cref="ArtFileStatus.Present"/> /// <see cref="ArtFileSize"/> set to the file size (or 0 if it could not be determined) /// <see cref="ArtFileWidth"/> and <see cref="ArtFileHeight"/> to the file image dimensions (or 0 if they could not be determined) /// </summary> /// <param name="filePath"></param> public void SetArtFile(string filePath) { ArtFile = filePath; ArtFileStatus = ArtFileStatus.Present; if (EmbeddedArtHelpers.IsEmbeddedArtPath(filePath)) { //Get the size of the embedded image, not the size of the file itself try { var embeddedArt = EmbeddedArtHelpers.GetEmbeddedArt(filePath); if (embeddedArt != null) { int ignored; string unembeddedFilePath; EmbeddedArtHelpers.SplitToFilenameAndIndex(filePath, out unembeddedFilePath, out ignored); ArtFileDate = File.GetLastWriteTime(unembeddedFilePath); ArtFileSize = embeddedArt.Data.Count; using (var dataStream = new MemoryStream(embeddedArt.Data.Data, false)) { var bitmapDecoder = BitmapDecoder.Create(dataStream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None); ArtFileWidth = bitmapDecoder.Frames[0].PixelWidth; ArtFileHeight = bitmapDecoder.Frames[0].PixelHeight; } } } catch (Exception) { //Ignore exceptions when reading the embedded artwork; it's not important at this stage ArtFileSize = 0; ArtFileWidth = ArtFileHeight = 0; ArtFileDate = null; } } else { //Not an embedded image, but an image file itself try { var fileInfo = new FileInfo(filePath); ArtFileSize = fileInfo.Length; ArtFileDate = fileInfo.LastWriteTime; } catch (Exception) { //Ignore exceptions when reading the filesize and date it's not important ArtFileSize = 0; ArtFileDate = null; } //Attempt to get the image dimesions try { using (var fileStream = File.OpenRead(filePath)) { var bitmapDecoder = BitmapDecoder.Create(fileStream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None); ArtFileWidth = bitmapDecoder.Frames[0].PixelWidth; ArtFileHeight = bitmapDecoder.Frames[0].PixelHeight; } } catch (Exception) { //Ignore exceptions when reading the dimensions, they aren't important ArtFileWidth = ArtFileHeight = 0; } } }