<PackageReference Include="System.Drawing.Common" Version="9.0.0-rc.2.24474.1" />

CachedBitmap

public sealed class CachedBitmap : IDisposable
Represents a device-dependent copy of a Bitmap matching a specified Graphics object's current device (display) settings. Avoids reformatting step when rendering, which can significantly improve performance.
using System.Runtime.CompilerServices; using System.Threading; using Windows.Win32; using Windows.Win32.Graphics.GdiPlus; namespace System.Drawing.Imaging { public sealed class CachedBitmap : IDisposable { private IntPtr _handle; internal IntPtr Handle => _handle; [NullableContext(1)] public unsafe CachedBitmap(Bitmap bitmap, Graphics graphics) { ArgumentNullException.ThrowIfNull(bitmap, "bitmap"); ArgumentNullException.ThrowIfNull(graphics, "graphics"); GpCachedBitmap* handle = default(GpCachedBitmap*); PInvoke.GdipCreateCachedBitmap(bitmap.Pointer(), graphics.Pointer(), &handle); _handle = (IntPtr)handle; } private unsafe void Dispose(bool disposing) { IntPtr intPtr = Interlocked.Exchange(ref _handle, (IntPtr)0); if (intPtr != (IntPtr)0) { Status status = PInvoke.GdipDeleteCachedBitmap((GpCachedBitmap*)(long)intPtr); if (disposing) Gdip.CheckStatus(status); } } ~CachedBitmap() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } } }