<PackageReference Include="System.Drawing.Common" Version="10.0.0-preview.6.25358.103" />

IIconExtensions

public static class IIconExtensions
using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Windows.Win32; using Windows.Win32.Foundation; using Windows.Win32.System.Ole; using Windows.Win32.UI.WindowsAndMessaging; namespace System.Drawing { public static class IIconExtensions { [NullableContext(1)] internal unsafe static PICTDESC CreatePICTDESC(this IIcon icon, bool copy) { PICTDESC pICTDESC = default(PICTDESC); pICTDESC.picType = PICTYPE.PICTYPE_ICON; PICTDESC result = pICTDESC; Size size = icon.Size; result.Anonymous.icon.hicon = (copy ? ((HICON)PInvokeCore.CopyImage(icon.Handle, GDI_IMAGE_TYPE.IMAGE_ICON, size.Width, size.Height, IMAGE_FLAGS.LR_DEFAULTCOLOR).Value) : icon.Handle); GC.KeepAlive(icon); return result; } [NullableContext(1)] internal static object CreateIPictureRCW(this IIcon icon, bool copy) { ComScope<IPicture> scope = icon.CreateIPicture(copy); try { return Marshal.GetObjectForIUnknown((IntPtr)ref scope); } finally { scope.Dispose(); } } internal unsafe static ComScope<IPicture> CreateIPicture([Nullable(1)] this IIcon icon, bool copy) { PICTDESC pICTDESC = icon.CreatePICTDESC(copy); ComScope<IPicture> scope = new ComScope<IPicture>(null); PInvokeCore.OleCreatePictureIndirect(&pICTDESC, IID.Get<IPicture>(), copy, (void**)ref scope).ThrowOnFailure((IntPtr)0); return scope; } } }