Skip to content
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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used default values, i don't need em

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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used default values, i don't need em

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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used default values, i don't need em

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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used default values, i don't need em

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
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs updates and refactoring.
@dahall overhaul threading issues please

public class SoftwareBitmapSource : Microsoft.UI.Xaml.Media.Imaging.SoftwareBitmapSource
{
    public IconSize IconSize { get; }
    public SoftwareBitmapSource(IconSize iconSize) => IconSize = iconSize;
}
public static async Task<SoftwareBitmapSource> GetWinUi3BitmapSourceFromIcon(Icon bitmapIcon, IconSize iconSize)
{
    ArgumentNullException.ThrowIfNull(bitmapIcon);
    // TODO: use iconSize...: var hIcon = Shell32.SHGetFileInfo(..., (int)iconSize);
    return await GetWinUi3BitmapSourceFromGdiBitmap(bitmapIcon.ToBitmap());
}
if (gdiBitmap.PixelFormat != PixelFormat.Format32bppArgb)
    gdiBitmap = gdiBitmap.Clone(new Rectangle(0,0,w,h), PixelFormat.Format32bppArgb);

var ms = new MemoryStream();
icon.Save(ms);
ms.Position = 0;

var bmp = new BitmapImage();
bmp.SetSource(ms.AsRandomAccessStream());
return bmp;

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;
}

}

19 changes: 17 additions & 2 deletions WinUI.Extensions/readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
## Assembly report for Vanara.WinUI.Extensions.dll
Extension methods and conversions from Vanara P/Invoke types and methods to UWP and WinUI types and methods.
Extension methods and conversions from Vanara P/Invoke types and methods to WinUI types and methods.

_UWP-Compatible types and methods are included in the Vanara.UWP.Extensions assembly._

### Classes
Class | Description
---- | ----
[Vanara.PInvoke.SystemFoundationExtensions](https://github.com/dahall/Vanara/search?l=C%23&q=SystemFoundationExtensions) | Conversion extension methods for WinUI/UWP and Vanara types.
[Vanara.WinUI.Extensions.SystemFoundationExtensions](https://github.com/dahall/Vanara/search?l=C%23&q=SystemFoundationExtensions) | Conversion extension methods for WinUI3 and Vanara types.
[Vanara.WinUI.Extensions.SystemFoundationExtensions](https://github.com/dahall/Vanara/search?l=C%23&q=SystemFoundationExtensions) | Conversion extension methods for WinUI3 and Vanara types.

### Namespaces
Namespace | Description
--------- | -----------

### **Vanara.WinUI.Extensions**


docs\icons\Vanara64x64.png

[]