mirror of https://github.com/dahall/Vanara.git
Added conversion methods from HICON to HBITMAP and BitmapSource.
parent
c904517bf7
commit
47cbd0be0b
|
@ -1,12 +1,14 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
|
||||||
<ProjectExtensions>
|
<ProjectExtensions>
|
||||||
<SupportedDlls>user32.dll</SupportedDlls>
|
<SupportedDlls>user32.dll</SupportedDlls>
|
||||||
</ProjectExtensions>
|
</ProjectExtensions>
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Description>PInvoke API (methods, structures and constants) imported from Windows User32.dll.</Description>
|
<Description>PInvoke API (methods, structures and constants) imported from Windows User32.dll.</Description>
|
||||||
<AssemblyTitle>$(AssemblyName)</AssemblyTitle>
|
<AssemblyTitle>$(AssemblyName)</AssemblyTitle>
|
||||||
<TargetFrameworks>net20;net35;net40;net45;net5.0-windows;netstandard2.0;netcoreapp2.0;netcoreapp2.1;netcoreapp3.0;netcoreapp3.1</TargetFrameworks>
|
<TargetFrameworks>net20;net35;net40;net45;net5.0-windows;netstandard2.0;netcoreapp2.0;netcoreapp2.1;netcoreapp3.0;netcoreapp3.1</TargetFrameworks>
|
||||||
|
<UseWindowsForms>true</UseWindowsForms>
|
||||||
|
<UseWPF>true</UseWPF>
|
||||||
<AssemblyName>Vanara.PInvoke.User32</AssemblyName>
|
<AssemblyName>Vanara.PInvoke.User32</AssemblyName>
|
||||||
<PackageId>$(AssemblyName)</PackageId>
|
<PackageId>$(AssemblyName)</PackageId>
|
||||||
<PackageTags>pinvoke;vanara;net-extensions;interop</PackageTags>
|
<PackageTags>pinvoke;vanara;net-extensions;interop</PackageTags>
|
||||||
|
@ -27,6 +29,9 @@ ICONINFO, VIDEOPARAMETERS, NMHDR, ACCEL, METAFILEPICT, CURSORINFO, DEV_BROADCAST
|
||||||
<ProjectReference Include="..\Kernel32\Vanara.PInvoke.Kernel32.csproj" />
|
<ProjectReference Include="..\Kernel32\Vanara.PInvoke.Kernel32.csproj" />
|
||||||
<ProjectReference Include="..\Gdi32\Vanara.PInvoke.Gdi32.csproj" />
|
<ProjectReference Include="..\Gdi32\Vanara.PInvoke.Gdi32.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup Condition=" $(TargetFramework.StartsWith('net3')) Or $(TargetFramework.StartsWith('net4')) ">
|
||||||
|
<Reference Include="PresentationCore" />
|
||||||
|
</ItemGroup>
|
||||||
<ItemGroup Condition=" $(TargetFramework.StartsWith('netstandard')) Or $(TargetFramework.StartsWith('netcore')) Or $(TargetFramework.StartsWith('net5')) ">
|
<ItemGroup Condition=" $(TargetFramework.StartsWith('netstandard')) Or $(TargetFramework.StartsWith('netcore')) Or $(TargetFramework.StartsWith('net5')) ">
|
||||||
<PackageReference Include="System.Drawing.Common">
|
<PackageReference Include="System.Drawing.Common">
|
||||||
<Version>5.0.0</Version>
|
<Version>5.0.0</Version>
|
||||||
|
|
|
@ -1122,6 +1122,38 @@ namespace Vanara.PInvoke
|
||||||
/// <returns>A managed bitmap instance.</returns>
|
/// <returns>A managed bitmap instance.</returns>
|
||||||
public static Bitmap ToBitmap(this HICON hIcon) => hIcon.IsNull ? null : (Bitmap)Bitmap.FromHicon((IntPtr)hIcon).Clone();
|
public static Bitmap ToBitmap(this HICON hIcon) => hIcon.IsNull ? null : (Bitmap)Bitmap.FromHicon((IntPtr)hIcon).Clone();
|
||||||
|
|
||||||
|
#if !NET20 && !NETSTANDARD2_0 && !NETCOREAPP2_0 && !NETCOREAPP2_1
|
||||||
|
/// <summary>Creates a <see cref="System.Windows.Media.Imaging.BitmapSource"/> from an <see cref="HICON"/>.</summary>
|
||||||
|
/// <param name="hIcon">The HICON value.</param>
|
||||||
|
/// <returns>The BitmapSource instance. If <paramref name="hIcon"/> is a <c>NULL</c> handle, <see langword="null"/> is returned.</returns>
|
||||||
|
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
|
||||||
|
|
||||||
|
/// <summary>Creates a <see cref="SafeHBITMAP"/> from this HICON instance.</summary>
|
||||||
|
/// <returns>A bitmap handle.</returns>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Creates a managed <see cref="System.Drawing.Icon"/> from this HICON instance.</summary>
|
/// <summary>Creates a managed <see cref="System.Drawing.Icon"/> from this HICON instance.</summary>
|
||||||
/// <returns>A managed icon instance.</returns>
|
/// <returns>A managed icon instance.</returns>
|
||||||
public static Icon ToIcon(this HICON hIcon) => hIcon.IsNull ? null : (Icon)Icon.FromHandle((IntPtr)hIcon).Clone();
|
public static Icon ToIcon(this HICON hIcon) => hIcon.IsNull ? null : (Icon)Icon.FromHandle((IntPtr)hIcon).Clone();
|
||||||
|
@ -1231,6 +1263,16 @@ namespace Vanara.PInvoke
|
||||||
/// <returns>A managed bitmap instance.</returns>
|
/// <returns>A managed bitmap instance.</returns>
|
||||||
public Bitmap ToBitmap() => ((HICON)this).ToBitmap();
|
public Bitmap ToBitmap() => ((HICON)this).ToBitmap();
|
||||||
|
|
||||||
|
#if !NET20 && !NETSTANDARD2_0 && !NETCOREAPP2_0 && !NETCOREAPP2_1
|
||||||
|
/// <summary>Creates a <see cref="System.Windows.Media.Imaging.BitmapSource"/> from an <see cref="SafeHICON"/>.</summary>
|
||||||
|
/// <returns>The BitmapSource instance. If this is a <c>NULL</c> handle, <see langword="null"/> is returned.</returns>
|
||||||
|
public System.Windows.Media.Imaging.BitmapSource ToBitmapSource() => ((HICON)this).ToBitmapSource();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/// <summary>Creates a <see cref="SafeHBITMAP"/> from this HICON instance.</summary>
|
||||||
|
/// <returns>A bitmap handle.</returns>
|
||||||
|
public SafeHBITMAP ToHBITMAP() => ((HICON)this).ToHBITMAP();
|
||||||
|
|
||||||
/// <summary>Creates a managed <see cref="System.Drawing.Icon"/>.</summary>
|
/// <summary>Creates a managed <see cref="System.Drawing.Icon"/>.</summary>
|
||||||
/// <returns>A managed icon instance.</returns>
|
/// <returns>A managed icon instance.</returns>
|
||||||
public Icon ToIcon() => ((HICON)this).ToIcon();
|
public Icon ToIcon() => ((HICON)this).ToIcon();
|
||||||
|
|
Loading…
Reference in New Issue