Fixed a problem with loading the icon from a share, also preventing a NullPointerReference in the MainForm

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2418 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2013-01-08 10:20:31 +00:00
parent b9a1bd76b5
commit 5ce65ab191
3 changed files with 48 additions and 8 deletions

View file

@ -790,8 +790,10 @@ namespace Greenshot {
}
private void HideThumbnailOnLeave(object sender, EventArgs e) {
if (thumbnailForm != null) {
thumbnailForm.Hide();
}
}
private void cleanupThumbnail() {
if (thumbnailForm != null) {

View file

@ -276,6 +276,7 @@ namespace GreenshotPlugin.Core {
}
}
/// <summary>
/// Get the icon belonging to the process
/// </summary>
@ -289,13 +290,11 @@ namespace GreenshotPlugin.Core {
string filename = ProcessPath;
if (!iconCache.ContainsKey(filename)) {
Image icon = null;
if (File.Exists(filename)) {
using (Icon appIcon = Icon.ExtractAssociatedIcon(filename)) {
using (Icon appIcon = Shell32.ExtractAssociatedIcon(filename)) {
if (appIcon != null) {
icon = appIcon.ToBitmap();
}
}
}
iconCache.Add(filename, icon);
}
return iconCache[filename];

View file

@ -21,6 +21,8 @@
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
namespace GreenshotPlugin.UnmanagedHelpers {
/// <summary>
@ -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);
/// <summary>
/// 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
/// </summary>
/// <param name="filePath">The path to the file that contains an image.</param>
/// <returns>The System.Drawing.Icon representation of the image contained in the specified file.</returns>
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;
}
}
}