From b0b1294b86a29fb99037a95aaca807127d9deb14 Mon Sep 17 00:00:00 2001 From: Thorsten Jung Date: Thu, 17 Oct 2024 21:52:09 +0200 Subject: [PATCH 1/4] Add IconExtension --- WinUI.Extensions/IconExtension.cs | 46 +++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 WinUI.Extensions/IconExtension.cs diff --git a/WinUI.Extensions/IconExtension.cs b/WinUI.Extensions/IconExtension.cs new file mode 100644 index 000000000..4d50a44f6 --- /dev/null +++ b/WinUI.Extensions/IconExtension.cs @@ -0,0 +1,46 @@ +/* + * + * + * Reference GitHub URL "tajbender.Vanara\Windows.Forms\Extensions\IconExtension.cs" + * + * + */ + +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Vanara.PInvoke; + +namespace Vanara.WinUI.Extensions; + +/// Used to determine the size of the icon returned by various shell methods. +public enum IconSize +{ + /// + /// The image size is normally 32x32 pixels. However, if the Use large icons option is selected from the Effects section of the Appearance tab in + /// Display Properties, the image is 48x48 pixels. + /// + Large = Shell32.SHIL.SHIL_LARGE, + /// The image is the Shell standard small icon size of 16x16, but the size can be customized by the user. + Small = Shell32.SHIL.SHIL_SMALL, + /// The image is the Shell standard extra-large icon size. This is typically 48x48, but the size can be customized by the user. + ExtraLarge = Shell32.SHIL.SHIL_EXTRALARGE, + /// Windows Vista and later. The image is normally 256x256 pixels. + Jumbo = Shell32.SHIL.SHIL_JUMBO +} + +/// Extension methods for . +/// This class replaces "Vanara.Windows.Forms.Extensions.IconExtension" for WinUI. +/// +public static class IconExtension +{ + //public static async Task GetWinUi3BitmapSourceFromIcon(Icon bitmapIcon) + //{ + // ArgumentNullException.ThrowIfNull(bitmapIcon); + + // return await GetWinUi3BitmapSourceFromGdiBitmap(bitmapIcon.ToBitmap()); + //} +} From 2c6861e8548ec3ce91638c3b81bc7b1a0b6676b7 Mon Sep 17 00:00:00 2001 From: Thorsten Jung Date: Fri, 18 Oct 2024 00:35:33 +0200 Subject: [PATCH 2/4] Update Vanara.WinUI.Extensions.csproj --- WinUI.Extensions/Vanara.WinUI.Extensions.csproj | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/WinUI.Extensions/Vanara.WinUI.Extensions.csproj b/WinUI.Extensions/Vanara.WinUI.Extensions.csproj index 93a0ccf76..45d653641 100644 --- a/WinUI.Extensions/Vanara.WinUI.Extensions.csproj +++ b/WinUI.Extensions/Vanara.WinUI.Extensions.csproj @@ -3,7 +3,7 @@ Extension methods and conversions from Vanara P/Invoke types and methods to UWP and WinUI types and methods. $(AssemblyName) - net45;net48;net6.0;net7.0;net8.0-windows;netcoreapp3.1 + net6.0;net7.0;net8.0-windows;netcoreapp3.1 true true Vanara.WinUI.Extensions @@ -16,6 +16,7 @@ SystemFoundationExtensions pkgreadme.md + Vanara.WinUI.Extensions @@ -26,6 +27,7 @@ SystemFoundationExtensions + \ No newline at end of file From ea46cf0dd7903ff24c98ebeb7577540651fff383 Mon Sep 17 00:00:00 2001 From: Thorsten Jung Date: Fri, 18 Oct 2024 02:42:06 +0200 Subject: [PATCH 3/4] Update IconExtension.cs --- WinUI.Extensions/IconExtension.cs | 87 ++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 8 deletions(-) diff --git a/WinUI.Extensions/IconExtension.cs b/WinUI.Extensions/IconExtension.cs index 4d50a44f6..11349c143 100644 --- a/WinUI.Extensions/IconExtension.cs +++ b/WinUI.Extensions/IconExtension.cs @@ -1,8 +1,8 @@ -/* +/* DOCS: * + * REFERENCES * - * Reference GitHub URL "tajbender.Vanara\Windows.Forms\Extensions\IconExtension.cs" - * + * * */ @@ -10,12 +10,14 @@ using System.Collections.Generic; using System.Drawing; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using Vanara.PInvoke; namespace Vanara.WinUI.Extensions; +#region Properties /// Used to determine the size of the icon returned by various shell methods. public enum IconSize { @@ -31,16 +33,85 @@ public enum IconSize /// Windows Vista and later. The image is normally 256x256 pixels. Jumbo = Shell32.SHIL.SHIL_JUMBO } +#endregion + +#region Embedded classes +public record SoftwareBitmapSource +{ + public IconSize IconSize; + + public SoftwareBitmapSource(IconSize iconSize) => IconSize = iconSize; + + public static Task FromIcon(Icon icon) + { + return IconExtension.GetWinUi3BitmapSourceFromIcon(icon); + } +} +#endregion /// Extension methods for . /// This class replaces "Vanara.Windows.Forms.Extensions.IconExtension" for WinUI. /// public static class IconExtension { - //public static async Task GetWinUi3BitmapSourceFromIcon(Icon bitmapIcon) - //{ - // ArgumentNullException.ThrowIfNull(bitmapIcon); + /// + /// + /// + /// + /// + /// + public static async Task GetWinUi3BitmapSourceFromIcon(Icon bitmapIcon, IconSize iconSize) + { + ArgumentNullException.ThrowIfNull(bitmapIcon); + + return await GetWinUi3BitmapSourceFromGdiBitmap(bitmapIcon.ToBitmap()); + } + + /* TODO: add to https://github.com/tajbender/tajbender.Vanara/blob/master/WinUI.Extensions/ShellImageSource.cs */ + + /// + /// Taken from + /// See also , which can deal with .ico natively. + /// + /// + /// + public static async Task GetWinUi3BitmapSourceFromIcon(Icon bitmapIcon) + { + ArgumentNullException.ThrowIfNull(bitmapIcon); + + return await GetWinUi3BitmapSourceFromGdiBitmap(bitmapIcon.ToBitmap()); + } + + /// + /// Taken from . + /// See also . + /// See also , which can deal with .ico natively. + /// + /// + /// + public static async Task GetWinUi3BitmapSourceFromGdiBitmap(Bitmap gdiBitmap) + { + ArgumentNullException.ThrowIfNull(gdiBitmap); + + // get pixels as an array of bytes + var data = gdiBitmap.LockBits(new System.Drawing.Rectangle(0, 0, gdiBitmap.Width, gdiBitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, gdiBitmap.PixelFormat); + var bytes = new byte[data.Stride * data.Height]; + Marshal.Copy(data.Scan0, bytes, 0, bytes.Length); + gdiBitmap.UnlockBits(data); + + // get WinRT SoftwareBitmap + var softwareBitmap = new Windows.Graphics.Imaging.SoftwareBitmap( + Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, + gdiBitmap.Width, + gdiBitmap.Height, + Windows.Graphics.Imaging.BitmapAlphaMode.Premultiplied); + softwareBitmap.CopyFromBuffer(bytes.AsBuffer()); + + // build WinUI3 SoftwareBitmapSource + var source = new SoftwareBitmapSource(); + await source.SetBitmapAsync(softwareBitmap); + return source; + } - // return await GetWinUi3BitmapSourceFromGdiBitmap(bitmapIcon.ToBitmap()); - //} } + From f8f481f3b5d9777c6ce9132ee3b3124c84b4a240 Mon Sep 17 00:00:00 2001 From: Thorsten Jung Date: Fri, 1 Nov 2024 19:32:16 +0100 Subject: [PATCH 4/4] add `ReSharper` database files --- .idea/.idea.Vanara/.idea/.gitignore | 13 ++++++ .idea/.idea.Vanara/.idea/.name | 1 + .idea/.idea.Vanara/.idea/indexLayout.xml | 8 ++++ .idea/.idea.Vanara/.idea/vcs.xml | 6 +++ .../Vanara.WinUI.Extensions.csproj | 45 +++++++++---------- 5 files changed, 49 insertions(+), 24 deletions(-) create mode 100644 .idea/.idea.Vanara/.idea/.gitignore create mode 100644 .idea/.idea.Vanara/.idea/.name create mode 100644 .idea/.idea.Vanara/.idea/indexLayout.xml create mode 100644 .idea/.idea.Vanara/.idea/vcs.xml diff --git a/.idea/.idea.Vanara/.idea/.gitignore b/.idea/.idea.Vanara/.idea/.gitignore new file mode 100644 index 000000000..171039469 --- /dev/null +++ b/.idea/.idea.Vanara/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/.idea.Vanara.iml +/projectSettingsUpdater.xml +/contentModel.xml +/modules.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.idea.Vanara/.idea/.name b/.idea/.idea.Vanara/.idea/.name new file mode 100644 index 000000000..01db66514 --- /dev/null +++ b/.idea/.idea.Vanara/.idea/.name @@ -0,0 +1 @@ +Vanara \ No newline at end of file diff --git a/.idea/.idea.Vanara/.idea/indexLayout.xml b/.idea/.idea.Vanara/.idea/indexLayout.xml new file mode 100644 index 000000000..7b08163ce --- /dev/null +++ b/.idea/.idea.Vanara/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.Vanara/.idea/vcs.xml b/.idea/.idea.Vanara/.idea/vcs.xml new file mode 100644 index 000000000..35eb1ddfb --- /dev/null +++ b/.idea/.idea.Vanara/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/WinUI.Extensions/Vanara.WinUI.Extensions.csproj b/WinUI.Extensions/Vanara.WinUI.Extensions.csproj index 45d653641..47412430f 100644 --- a/WinUI.Extensions/Vanara.WinUI.Extensions.csproj +++ b/WinUI.Extensions/Vanara.WinUI.Extensions.csproj @@ -1,33 +1,30 @@  - - Extension methods and conversions from Vanara P/Invoke types and methods to UWP and WinUI types and methods. - $(AssemblyName) - net6.0;net7.0;net8.0-windows;netcoreapp3.1 - true + + Extension methods and conversions from Vanara P/Invoke types and methods to UWP and WinUI types and methods. + $(AssemblyName) + net6.0;net7.0;net8.0-windows;netcoreapp3.1 + true true - Vanara.WinUI.Extensions - $(AssemblyName) - pinvoke;vanara;net-extensions;interop;winui;uwp;windows platform - Currently implements: + Vanara.WinUI.Extensions + $(AssemblyName) + pinvoke;vanara;net-extensions;interop;winui;uwp;windows platform + Currently implements: Classes SystemFoundationExtensions - pkgreadme.md - Vanara.WinUI.Extensions - - - - - - - - - - - - - + pkgreadme.md + Vanara.WinUI.Extensions + + + + + + + + + + \ No newline at end of file