Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IconExtension for WinUI #493

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .idea/.idea.Vanara/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.idea.Vanara/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/.idea.Vanara/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/.idea.Vanara/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

117 changes: 117 additions & 0 deletions WinUI.Extensions/IconExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/* DOCS:
*
* REFERENCES
*
* <seealso cref="tajbender.Vanara\Windows.Forms\Extensions\IconExtension.cs"/>
*
*/

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
/// <summary>Used to determine the size of the icon returned by various shell methods.</summary>
public enum IconSize
{
/// <summary>
/// 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.
/// </summary>
Large = Shell32.SHIL.SHIL_LARGE,
/// <summary>The image is the Shell standard small icon size of 16x16, but the size can be customized by the user.</summary>
Small = Shell32.SHIL.SHIL_SMALL,
/// <summary>The image is the Shell standard extra-large icon size. This is typically 48x48, but the size can be customized by the user.</summary>
ExtraLarge = Shell32.SHIL.SHIL_EXTRALARGE,
/// <summary>Windows Vista and later. The image is normally 256x256 pixels.</summary>
Jumbo = Shell32.SHIL.SHIL_JUMBO
}
#endregion

#region Embedded classes
public record SoftwareBitmapSource
{
public IconSize IconSize;

public SoftwareBitmapSource(IconSize iconSize) => IconSize = iconSize;

public static Task<SoftwareBitmapSource> FromIcon(Icon icon)
{
return IconExtension.GetWinUi3BitmapSourceFromIcon(icon);
}
}
#endregion

/// <summary>Extension methods for <see cref="Icon"/>.
/// This class replaces "Vanara.Windows.Forms.Extensions.IconExtension" for WinUI.
/// </summary>
public static class IconExtension
{
/// <summary>
///
/// </summary>
/// <param name="bitmapIcon"></param>
/// <param name="iconSize"></param>
/// <returns></returns>
public static async Task<SoftwareBitmapSource> 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 */

/// <summary>
/// Taken from <see href="https://stackoverflow.com/questions/76640972/convert-system-drawing-icon-to-microsoft-ui-xaml-imagesource"/>
/// See also <see href="https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.imaging.bitmapimage?view=windows-app-sdk-1.6"/>, which can deal with .ico natively.
/// </summary>
/// <param name="bitmapIcon"></param>
/// <returns></returns>
public static async Task<SoftwareBitmapSource> GetWinUi3BitmapSourceFromIcon(Icon bitmapIcon)
{
ArgumentNullException.ThrowIfNull(bitmapIcon);

return await GetWinUi3BitmapSourceFromGdiBitmap(bitmapIcon.ToBitmap());
}

/// <summary>
/// Taken from <see href="https://stackoverflow.com/questions/76640972/convert-system-drawing-icon-to-microsoft-ui-xaml-imagesource"/>.
/// See also <see href="https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.controls.image.source?view=windows-app-sdk-1.5#microsoft-ui-xaml-controls-image-source"/>.
/// See also <see href="https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.media.imaging.bitmapimage?view=windows-app-sdk-1.6"/>, which can deal with .ico natively.
/// </summary>
/// <param name="gdiBitmap"></param>
/// <returns></returns>
public static async Task<SoftwareBitmapSource?> 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;
}

}

43 changes: 21 additions & 22 deletions WinUI.Extensions/Vanara.WinUI.Extensions.csproj
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>Extension methods and conversions from Vanara P/Invoke types and methods to UWP and WinUI types and methods.</Description>
<AssemblyTitle>$(AssemblyName)</AssemblyTitle>
<TargetFrameworks>net45;net48;net6.0;net7.0;net8.0-windows;netcoreapp3.1</TargetFrameworks>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
<PropertyGroup>
<Description>Extension methods and conversions from Vanara P/Invoke types and methods to UWP and WinUI types and methods.</Description>
<AssemblyTitle>$(AssemblyName)</AssemblyTitle>
<TargetFrameworks>net6.0;net7.0;net8.0-windows;netcoreapp3.1</TargetFrameworks>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
<UseWPF>true</UseWPF>
<AssemblyName>Vanara.WinUI.Extensions</AssemblyName>
<PackageId>$(AssemblyName)</PackageId>
<PackageTags>pinvoke;vanara;net-extensions;interop;winui;uwp;windows platform</PackageTags>
<PackageReleaseNotes>Currently implements:
<AssemblyName>Vanara.WinUI.Extensions</AssemblyName>
<PackageId>$(AssemblyName)</PackageId>
<PackageTags>pinvoke;vanara;net-extensions;interop;winui;uwp;windows platform</PackageTags>
<PackageReleaseNotes>Currently implements:

Classes
SystemFoundationExtensions

</PackageReleaseNotes>
<PackageReadmeFile>pkgreadme.md</PackageReadmeFile>
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net48' ">
<Reference Include="netstandard" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PInvoke\Gdi32\Vanara.PInvoke.Gdi32.csproj" />
<ProjectReference Include="..\PInvoke\User32\Vanara.PInvoke.User32.csproj" />
<ProjectReference Include="..\Windows.Shell.Common\Vanara.Windows.Shell.Common.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Runtime.WindowsRuntime" Version="4.7.0" />
</ItemGroup>
<PackageReadmeFile>pkgreadme.md</PackageReadmeFile>
<RootNamespace>Vanara.WinUI.Extensions</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\PInvoke\Gdi32\Vanara.PInvoke.Gdi32.csproj" />
<ProjectReference Include="..\PInvoke\User32\Vanara.PInvoke.User32.csproj" />
<ProjectReference Include="..\Windows.Shell.Common\Vanara.Windows.Shell.Common.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.6.240923002" />
<PackageReference Include="System.Runtime.WindowsRuntime" Version="4.7.0" />
</ItemGroup>
</Project>