<PackageReference Include="System.Drawing.Common" Version="10.0.0-rc.2.25502.107" />

HdcHandle

sealed class HdcHandle : IDisposable, IHandle<HDC>
Used when you must keep a handle to an HDC in a field. Avoid keeping HDC handles in fields when possible.
using System; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Windows.Win32.Foundation; namespace Windows.Win32.Graphics.Gdi { internal sealed class HdcHandle : IDisposable, IHandle<HDC> { public HDC Handle { get; set; } public HdcHandle(CreateDcScope hdc) { Handle = (HDC)ref hdc; } public static implicit operator HDC([In] [IsReadOnly] ref HdcHandle handle) { return handle.Handle; } public static implicit operator IntPtr([In] [IsReadOnly] ref HdcHandle handle) { return handle.Handle; } public void Dispose() { HDC hDC = Handle; if (!hDC.IsNull) { PInvokeCore.DeleteDC(Handle); hDC = (Handle = default(HDC)); } GC.SuppressFinalize(this); } ~HdcHandle() { Dispose(); } } }