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

ObjectScope

struct ObjectScope
Helper to scope lifetime of a GDI object. Deletes the given object (if any) when disposed.
using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Windows.Win32.Graphics.Gdi; namespace Windows.Win32 { [CompilerFeatureRequired("RefStructs")] internal readonly ref struct ObjectScope { public HGDIOBJ HGDIOBJ { get; } public ObjectScope(HGDIOBJ object) { HGDIOBJ = object; } public static implicit operator HGDIOBJ([In] [IsReadOnly] ref ObjectScope objectScope) { return objectScope.HGDIOBJ; } public void Dispose() { if (!HGDIOBJ.IsNull) PInvokeCore.DeleteObject(HGDIOBJ); } } }