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

CreatePenScope

Helper to scope the lifetime of a HPEN.
using System.Drawing; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace Windows.Win32.Graphics.Gdi { [CompilerFeatureRequired("RefStructs")] internal readonly ref struct CreatePenScope { public HPEN HPEN { get; } public bool IsNull => HPEN.IsNull; public CreatePenScope(Color color, int width = 1) { HPEN = PInvokeCore.CreatePen(PEN_STYLE.PS_COSMETIC, width, color); } public static implicit operator HPEN([In] [IsReadOnly] ref CreatePenScope scope) { return scope.HPEN; } public static implicit operator HGDIOBJ([In] [IsReadOnly] ref CreatePenScope scope) { return scope.HPEN; } public void Dispose() { if (!HPEN.IsNull) PInvokeCore.DeleteObject(HPEN); } } }