CachedBitmap
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);
}
}
}