public ImageSource GetIconImage(ResourceID id, bool icon) { IntPtr iconName; using (var d = id.GetAsIntPtr(out iconName)) { var hIcon = Win32.LoadImage(_hModule, iconName, icon ? Win32.ImageType.Icon : Win32.ImageType.Cursor, 0, 0, Win32.LoadImageFlags.None); if (hIcon == IntPtr.Zero) { var bytes = GetResourceContent(id, icon ? ResourceID.Icon : ResourceID.Cursor); hIcon = Win32.CreateIconFromResource(bytes, bytes.Length, icon, 0x30000); if (hIcon == IntPtr.Zero) { return(null); } } var source = Imaging.CreateBitmapSourceFromHIcon(hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); if (icon) { Win32.DestroyIcon(hIcon); } else { Win32.DestroyCursor(hIcon); } return(source); } }
public byte[] GetResourceContent(ResourceID name, ResourceID type) { IntPtr iname, itype; using (var d1 = name.GetAsIntPtr(out iname)) { using (var d2 = type.GetAsIntPtr(out itype)) { var hResource = Win32.FindResource(_hModule, iname, itype); if (hResource == IntPtr.Zero) { return(null); } var hGlobal = Win32.LoadResource(_hModule, hResource); if (hGlobal == IntPtr.Zero) { return(null); } var size = Win32.SizeofResource(_hModule, hResource); var ptr = Win32.LockResource(hGlobal); var buffer = new byte[size]; Marshal.Copy(ptr, buffer, 0, buffer.Length); return(buffer); } } }
public ImageSource GetBitmapImage(ResourceID id) { IntPtr bitmapName; using (var d = id.GetAsIntPtr(out bitmapName)) { var hBitmap = Win32.LoadImage(_hModule, bitmapName, Win32.ImageType.Bitmap, 0, 0, Win32.LoadImageFlags.CreateDibSection); if (hBitmap == IntPtr.Zero) { return(null); } var source = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); Win32.DeleteObject(hBitmap); return(source); } }