static IntPtr getSizedIcon(int iImage) { IImageList spiml = null; Guid guil = new Guid(IID_IImageList2);//or IID_IImageList Shell32.SHGetImageList(Program.selectedSize, ref guil, ref spiml); IntPtr hIcon = IntPtr.Zero; spiml.GetIcon(iImage, Shell32.ILD_TRANSPARENT | Shell32.ILD_IMAGE, ref hIcon); return(hIcon); }
public Icon ExtractAssociatedIcon(string filename) { if (!System.IO.Directory.Exists(filename) && !System.IO.File.Exists(filename)) { return(null); } int index = 0; StringBuilder sbFilename = new StringBuilder(260); sbFilename.Append(filename); IntPtr hico = Shell32.ExtractAssociatedIcon(IntPtr.Zero, sbFilename, ref index); return(Icon.FromHandle(hico)); }
static string getIconAsBase64(string path) { if (useShortcutPath && path.EndsWith(".lnk")) { path = getShortcutTarget(path); } Icon iconForPath = SystemIcons.Application; if (System.IO.File.Exists(path)) { //iconForPath = Icon.ExtractAssociatedIcon(path); IntPtr hIcon = getSizedIcon(getIconIndex(path)); iconForPath = (Icon)Icon.FromHandle(hIcon).Clone(); Shell32.DestroyIcon(hIcon); // don't forget to cleanup } ImageConverter vert = new ImageConverter(); byte[] data = (byte[])vert.ConvertTo(iconForPath.ToBitmap(), typeof(byte[])); return(Convert.ToBase64String(data)); }
public Bitmap LoadImageFromExtractIcon(string filename) { if (!System.IO.Directory.Exists(filename) && !System.IO.File.Exists(filename)) { return(null); } Bitmap bm1 = null; IntPtr hbitmap = IntPtr.Zero; IShellFolder desktop = null; IntPtr pidlParent = IntPtr.Zero; IShellFolder shellFolder = null; IUnknown iunk = null; IExtractIcon extractIcon = null; try { Debug.Write("Trying IExtractIcon\r\n"); Shell32.SHGetDesktopFolder(ref desktop); int cParsed = 0; int pdwAttrib = 0; string filePath = filename; if (System.IO.File.Exists(filename)) { filePath = System.IO.Path.GetDirectoryName(filename); } desktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filePath, out cParsed, out pidlParent, out pdwAttrib); Guid uuidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046"); desktop.BindToObject(pidlParent, IntPtr.Zero, ref uuidShellFolder, out shellFolder); Guid uuidExtractIcon = new Guid("000214eb-0000-0000-c000-000000000046"); iunk = GetUIObject(shellFolder, filename, uuidExtractIcon); if (iunk == null) { Debug.Write(" IExtractIcon not supported\r\n"); return(null); } int hres = 0; extractIcon = (IExtractIcon)iunk; StringBuilder location = new StringBuilder(260, 260); int iconIndex = 0; GILFLAGS2 flags = GILFLAGS2.GIL_None; hres = extractIcon.GetIconLocation(0, location, location.Capacity, out iconIndex, out flags); IntPtr iconLarge = IntPtr.Zero; IntPtr iconSmall = IntPtr.Zero; uint iconSize = (16 | (48 << 16)); hres = extractIcon.Extract(location, iconIndex, out iconLarge, out iconSmall, iconSize); Icon ico = Icon.FromHandle(iconLarge); bm1 = ico.ToBitmap(); } catch (Exception e) { Debug.Write(" Exception extracting image from IExtractIcon: " + e.Message + "\r\n"); } finally { if (extractIcon != null) { Marshal.ReleaseComObject(extractIcon); extractIcon = null; } if (iunk != null) { Marshal.ReleaseComObject(iunk); iunk = null; } if (shellFolder != null) { Marshal.ReleaseComObject(shellFolder); shellFolder = null; } if (pidlParent != IntPtr.Zero) { Allocator.Free(pidlParent); pidlParent = IntPtr.Zero; } if (desktop != null) { Marshal.ReleaseComObject(desktop); desktop = null; } if (_allocator != null) { Marshal.ReleaseComObject(_allocator); _allocator = null; } } return(bm1); }
public Bitmap LoadThumbnailFromExtractImage(string filename, Size requestedSize) { if (!System.IO.Directory.Exists(filename) && !System.IO.File.Exists(filename)) { return(null); } Bitmap bm1 = null; IntPtr hbitmap = IntPtr.Zero; SIZE sz = new SIZE(requestedSize.Width, requestedSize.Height); IShellFolder desktop = null; IntPtr pidlParent = IntPtr.Zero; IShellFolder shellFolder = null; IUnknown iunk = null; IExtractImage extractImage = null; try { Debug.Write("Trying IExtractImage\r\n"); Shell32.SHGetDesktopFolder(ref desktop); int cParsed = 0; int pdwAttrib = 0; string filePath = filename; if (System.IO.File.Exists(filename)) { filePath = System.IO.Path.GetDirectoryName(filename); } desktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filePath, out cParsed, out pidlParent, out pdwAttrib); Guid uuidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046"); desktop.BindToObject(pidlParent, IntPtr.Zero, ref uuidShellFolder, out shellFolder); Guid uuidExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1"); iunk = GetUIObject(shellFolder, filename, uuidExtractImage); if (iunk == null) { Debug.Write(" IExtractImage not supported\r\n"); return(null); } extractImage = (IExtractImage)iunk; StringBuilder location = new StringBuilder(260, 260); int priority = 0; EIEIFLAG flags = EIEIFLAG.IEIFLAG_ORIGSIZE; int uFlags = (int)flags; extractImage.GetLocation(location, location.Capacity, ref priority, ref sz, 32, ref uFlags); extractImage.Extract(out hbitmap); bm1 = GetBitmapFromHbitmap(hbitmap); } catch (Exception e) { Debug.Write(" Exception extracting image from IExtractImage: " + e.Message + "\r\n"); } finally { if (extractImage != null) { Marshal.ReleaseComObject(extractImage); extractImage = null; } if (iunk != null) { Marshal.ReleaseComObject(iunk); iunk = null; } if (shellFolder != null) { Marshal.ReleaseComObject(shellFolder); shellFolder = null; } if (pidlParent != IntPtr.Zero) { Allocator.Free(pidlParent); pidlParent = IntPtr.Zero; } if (desktop != null) { Marshal.ReleaseComObject(desktop); desktop = null; } if (_allocator != null) { Marshal.ReleaseComObject(_allocator); _allocator = null; } } return(bm1); }
public Bitmap LoadThumbnailFromImageFactory(string filename, Size requestedSize, bool onlyThumbnail) { if (!System.IO.Directory.Exists(filename) && !System.IO.File.Exists(filename)) { return(null); } Bitmap bm1 = null; IntPtr hbitmap = IntPtr.Zero; SIZE sz = new SIZE(requestedSize.Width, requestedSize.Height); IShellFolder desktop = null; IntPtr pidlMain = IntPtr.Zero; IShellItem shellItem = null; IShellItemImageFactory ppsiShellItemImageFactory = null; try { Shell32.SHGetDesktopFolder(ref desktop); int cParsed = 0; int pdwAttrib = 0; desktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filename, out cParsed, out pidlMain, out pdwAttrib); //Shell32.SHCreateItemFromIDList(pidlMain, uuidIShellItem, out shellItem); Shell32.SHCreateShellItem(IntPtr.Zero, null, pidlMain, out shellItem); ppsiShellItemImageFactory = (IShellItemImageFactory)shellItem; if (onlyThumbnail) { ppsiShellItemImageFactory.GetImage(sz, SIIGBF.SIIGBF_BIGGERSIZEOK | SIIGBF.SIIGBF_THUMBNAILONLY, out hbitmap); } else { ppsiShellItemImageFactory.GetImage(sz, SIIGBF.SIIGBF_BIGGERSIZEOK, out hbitmap); } bm1 = GetBitmapFromHbitmap(hbitmap); } catch (InvalidCastException) { Debug.Write(" Did not support IShellItemImageFactory\r\n"); } catch (Exception e) { Debug.Write(" Exception extracting image from IShellItemImageFactory: " + e.Message + "\r\n"); } finally { if (ppsiShellItemImageFactory != null) { Marshal.ReleaseComObject(ppsiShellItemImageFactory); ppsiShellItemImageFactory = null; } if (shellItem != null) { Marshal.ReleaseComObject(shellItem); shellItem = null; } if (pidlMain != IntPtr.Zero) { Allocator.Free(pidlMain); pidlMain = IntPtr.Zero; } if (desktop != null) { Marshal.ReleaseComObject(desktop); desktop = null; } if (_allocator != null) { Marshal.ReleaseComObject(_allocator); _allocator = null; } } return(bm1); }