mirror of
https://github.com/greenshot/greenshot
synced 2025-07-16 10:03:44 -07:00
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:
parent
b9a1bd76b5
commit
5ce65ab191
3 changed files with 48 additions and 8 deletions
|
@ -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() {
|
||||
|
|
|
@ -276,6 +276,7 @@ namespace GreenshotPlugin.Core {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Get the icon belonging to the process
|
||||
/// </summary>
|
||||
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue