diff --git a/PInvoke/User32/Vanara.PInvoke.User32.csproj b/PInvoke/User32/Vanara.PInvoke.User32.csproj index 33de984c..a6cb192b 100644 --- a/PInvoke/User32/Vanara.PInvoke.User32.csproj +++ b/PInvoke/User32/Vanara.PInvoke.User32.csproj @@ -1,12 +1,14 @@  - - + + user32.dll PInvoke API (methods, structures and constants) imported from Windows User32.dll. $(AssemblyName) net20;net35;net40;net45;net5.0-windows;netstandard2.0;netcoreapp2.0;netcoreapp2.1;netcoreapp3.0;netcoreapp3.1 + true + true Vanara.PInvoke.User32 $(AssemblyName) pinvoke;vanara;net-extensions;interop @@ -27,6 +29,9 @@ ICONINFO, VIDEOPARAMETERS, NMHDR, ACCEL, METAFILEPICT, CURSORINFO, DEV_BROADCAST + + + 5.0.0 diff --git a/PInvoke/User32/WinUser.Icon.cs b/PInvoke/User32/WinUser.Icon.cs index 8e5698dd..b9511db2 100644 --- a/PInvoke/User32/WinUser.Icon.cs +++ b/PInvoke/User32/WinUser.Icon.cs @@ -1122,6 +1122,38 @@ namespace Vanara.PInvoke /// A managed bitmap instance. public static Bitmap ToBitmap(this HICON hIcon) => hIcon.IsNull ? null : (Bitmap)Bitmap.FromHicon((IntPtr)hIcon).Clone(); +#if !NET20 && !NETSTANDARD2_0 && !NETCOREAPP2_0 && !NETCOREAPP2_1 + /// Creates a from an . + /// The HICON value. + /// The BitmapSource instance. If is a NULL handle, is returned. + public static System.Windows.Media.Imaging.BitmapSource ToBitmapSource(this in HICON hIcon) + { + // If hIcon is NULL handle, return null + if (hIcon.IsNull) return null; + try + { + return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon((IntPtr)hIcon, System.Windows.Int32Rect.Empty, + System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); + } + catch (System.ComponentModel.Win32Exception) + { + return null; + } + } +#endif + + /// Creates a from this HICON instance. + /// A bitmap handle. + public static SafeHBITMAP ToHBITMAP(this HICON hIcon) + { + if (hIcon.IsNull) return null; + using var icoInfo = new ICONINFO(); + Win32Error.ThrowLastErrorIfFalse(GetIconInfo(hIcon, icoInfo)); + var ret = new SafeHBITMAP((IntPtr)CopyImage((IntPtr)icoInfo.hbmColor, LoadImageType.IMAGE_BITMAP, 0, 0, CopyImageOptions.LR_CREATEDIBSECTION), true); + icoInfo.hbmColor = default; + return ret; + } + /// Creates a managed from this HICON instance. /// A managed icon instance. public static Icon ToIcon(this HICON hIcon) => hIcon.IsNull ? null : (Icon)Icon.FromHandle((IntPtr)hIcon).Clone(); @@ -1231,6 +1263,16 @@ namespace Vanara.PInvoke /// A managed bitmap instance. public Bitmap ToBitmap() => ((HICON)this).ToBitmap(); +#if !NET20 && !NETSTANDARD2_0 && !NETCOREAPP2_0 && !NETCOREAPP2_1 + /// Creates a from an . + /// The BitmapSource instance. If this is a NULL handle, is returned. + public System.Windows.Media.Imaging.BitmapSource ToBitmapSource() => ((HICON)this).ToBitmapSource(); +#endif + + /// Creates a from this HICON instance. + /// A bitmap handle. + public SafeHBITMAP ToHBITMAP() => ((HICON)this).ToHBITMAP(); + /// Creates a managed . /// A managed icon instance. public Icon ToIcon() => ((HICON)this).ToIcon();