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/IconExtension.cs b/WinUI.Extensions/IconExtension.cs new file mode 100644 index 000000000..11349c143 --- /dev/null +++ b/WinUI.Extensions/IconExtension.cs @@ -0,0 +1,117 @@ +/* DOCS: + * + * REFERENCES + * + * + * + */ + +using System; +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 +{ + /// + /// 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 +} +#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, 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; + } + +} + diff --git a/WinUI.Extensions/Vanara.WinUI.Extensions.csproj b/WinUI.Extensions/Vanara.WinUI.Extensions.csproj index 93a0ccf76..47412430f 100644 --- a/WinUI.Extensions/Vanara.WinUI.Extensions.csproj +++ b/WinUI.Extensions/Vanara.WinUI.Extensions.csproj @@ -1,31 +1,30 @@  - - 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 - 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 - - - - - - - - - - - - + pkgreadme.md + Vanara.WinUI.Extensions + + + + + + + + + + \ No newline at end of file