diff --git a/Greenshot/Forms/MainForm.cs b/Greenshot/Forms/MainForm.cs index 338da79e3..a14d6099d 100644 --- a/Greenshot/Forms/MainForm.cs +++ b/Greenshot/Forms/MainForm.cs @@ -784,13 +784,15 @@ namespace Greenshot { ToolStripMenuItem captureWindowItem = sender as ToolStripMenuItem; WindowDetails window = captureWindowItem.Tag as WindowDetails; if (thumbnailForm == null) { - thumbnailForm = new ThumbnailForm(); + thumbnailForm = new ThumbnailForm(); } - thumbnailForm.ShowThumbnail(window, captureWindowItem.GetCurrentParent().TopLevelControl); + thumbnailForm.ShowThumbnail(window, captureWindowItem.GetCurrentParent().TopLevelControl); } private void HideThumbnailOnLeave(object sender, EventArgs e) { - thumbnailForm.Hide(); + if (thumbnailForm != null) { + thumbnailForm.Hide(); + } } private void cleanupThumbnail() { diff --git a/GreenshotPlugin/Core/WindowsHelper.cs b/GreenshotPlugin/Core/WindowsHelper.cs index 3b1108b6c..0bd81dd46 100644 --- a/GreenshotPlugin/Core/WindowsHelper.cs +++ b/GreenshotPlugin/Core/WindowsHelper.cs @@ -276,6 +276,7 @@ namespace GreenshotPlugin.Core { } } + /// /// Get the icon belonging to the process /// @@ -289,11 +290,9 @@ namespace GreenshotPlugin.Core { string filename = ProcessPath; if (!iconCache.ContainsKey(filename)) { Image icon = null; - if (File.Exists(filename)) { - using (Icon appIcon = Icon.ExtractAssociatedIcon(filename)) { - if (appIcon != null) { - icon = appIcon.ToBitmap(); - } + using (Icon appIcon = Shell32.ExtractAssociatedIcon(filename)) { + if (appIcon != null) { + icon = appIcon.ToBitmap(); } } iconCache.Add(filename, icon); diff --git a/GreenshotPlugin/UnmanagedHelpers/Shell32.cs b/GreenshotPlugin/UnmanagedHelpers/Shell32.cs index 3783c4e9c..56fa74f35 100644 --- a/GreenshotPlugin/UnmanagedHelpers/Shell32.cs +++ b/GreenshotPlugin/UnmanagedHelpers/Shell32.cs @@ -21,6 +21,8 @@ using System; using System.Drawing; using System.Runtime.InteropServices; +using System.Text; +using System.IO; namespace GreenshotPlugin.UnmanagedHelpers { /// @@ -29,5 +31,42 @@ namespace GreenshotPlugin.UnmanagedHelpers { public static class Shell32 { [DllImport("shell32")] public static extern int ExtractIconEx(string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons); + [DllImport("shell32", CharSet = CharSet.Auto)] + internal static extern IntPtr ExtractAssociatedIcon(HandleRef hInst, StringBuilder iconPath, ref int index); + + /// + /// Returns an icon representation of an image contained in the specified file. + /// This function is identical to System.Drawing.Icon.ExtractAssociatedIcon, xcept this version works. + /// See: http://stackoverflow.com/questions/1842226/how-to-get-the-associated-icon-from-a-network-share-file + /// + /// The path to the file that contains an image. + /// The System.Drawing.Icon representation of the image contained in the specified file. + public static Icon ExtractAssociatedIcon(String filePath) { + int index = 0; + + Uri uri; + if (filePath == null) { + throw new ArgumentException(String.Format("'{0}' is not valid for '{1}'", "null", "filePath"), "filePath"); + } + try { + uri = new Uri(filePath); + } catch (UriFormatException) { + filePath = Path.GetFullPath(filePath); + uri = new Uri(filePath); + } + + if (uri.IsFile) { + if (File.Exists(filePath)) { + StringBuilder iconPath = new StringBuilder(1024); + iconPath.Append(filePath); + + IntPtr handle = ExtractAssociatedIcon(new HandleRef(null, IntPtr.Zero), iconPath, ref index); + if (handle != IntPtr.Zero) { + return Icon.FromHandle(handle); + } + } + } + return null; + } } }