void GetIcon(ISession session, string iconExtension) { var iconHandle = IntPtr.Zero; try { for (var i = 0; i < 4; i++) { var shinfo = new Api.SHFILEINFO(); Api.SHGetFileInfo(iconExtension, Api.FILE_ATTRIBUTE_NORMAL, ref shinfo, (uint)Marshal.SizeOf(shinfo), (int)(Api.SHGetFileInfoConstants.SHGFI_ICON | Api.SHGetFileInfoConstants.SHGFI_SMALLICON | Api.SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES | Api.SHGetFileInfoConstants.SHGFI_TYPENAME)); if (shinfo.hIcon == IntPtr.Zero) { var watt = Marshal.GetLastWin32Error(); if (watt == 997) { Thread.Sleep(20); continue; } throw new Exception("Fehler"); } iconHandle = shinfo.hIcon; break; } using (var icon = Icon.FromHandle(iconHandle)) { using (var bitmap = icon.ToBitmap()) { var ms = new System.IO.MemoryStream(); bitmap.Save(ms, ImageFormat.Png); ms.Position = 0; session.SendStream(ms, "image/png", Constants.NotModified, false); } } } catch (Exception) { session.SendError("Nicht gefunden", "Nicht gefunden", 404, "Not Found"); } finally { Api.DestroyIcon(iconHandle); } }
public static ImageBrush ExtractIcon(string name) { var shinfo = new Api.SHFILEINFO(); var ptr = Api.SHGetFileInfo(name, Api.FILE_ATTRIBUTE_NORMAL, ref shinfo, (uint)Marshal.SizeOf(shinfo), (int)(Api.SHGetFileInfoConstants.SHGFI_ICON | Api.SHGetFileInfoConstants.SHGFI_SMALLICON | Api.SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES | Api.SHGetFileInfoConstants.SHGFI_TYPENAME)); var icon = Icon.FromHandle(shinfo.hIcon).Clone() as Icon; Api.DestroyIcon(shinfo.hIcon); var brush = CreateBrush(icon); icon.Dispose(); return(brush); }