public static BitmapSource?GetIcon(string fullPath, IconType iconType) { var info = new SHFILEINFO(true); var flags = NativeConsts.SHGFI.Icon; if (iconType == IconType.Large) { flags |= NativeConsts.SHGFI.LargeIcon; } else if (iconType == IconType.Small) { flags |= NativeConsts.SHGFI.SmallIcon; } var fileAttributes = NativeConsts.FILE_ATTRIBUTE.Normal; lock (lockObject) { if (NativeMethods.SHGetFileInfo(fullPath, fileAttributes, out info, (uint)Marshal.SizeOf(info), flags) != 0 && info.hIcon != IntPtr.Zero) { return(BitmapSourceConverters.ToBitmapSource(info.hIcon)); } } return(null); }
private static object?GetWindowIconSource(IntPtr hWnd, bool isIntern) { try { var iconHandle = default(IntPtr); iconHandle = NativeMethods.SendMessage(hWnd, (int)NativeConsts.Message.WM_GETICON, NativeConsts.ICON_SMALL2, IntPtr.Zero); if (iconHandle == IntPtr.Zero) { iconHandle = NativeMethods.GetClassLongPtr(hWnd, NativeConsts.GCL_HICON); } if (iconHandle == IntPtr.Zero) { iconHandle = NativeMethods.LoadIcon(IntPtr.Zero, (IntPtr)0x7F00 /*IDI_APPLICATION*/); } if (iconHandle != IntPtr.Zero) { return(BitmapSourceConverters.ToBitmapSource(iconHandle, !isIntern)); } return(null); } catch (Exception) { return(null); } }