GpBitmapExtensions
using System;
using System.Drawing;
using System.Runtime.CompilerServices;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Gdi;
using Windows.Win32.System.Ole;
namespace Windows.Win32.Graphics.GdiPlus
{
[NullableContext(1)]
[Nullable(0)]
internal static class GpBitmapExtensions
{
public unsafe static void LockBits(this IPointer<GpBitmap> bitmap, Rectangle rect, ImageLockMode flags, PixelFormat format, ref BitmapData data)
{
PInvokeGdiPlus.GdipBitmapLockBits(bitmap.GetPointer(), (Rect*)(long)(rect.IsEmpty ? ((IntPtr)(void*)null) : ((IntPtr)(&rect))), (uint)flags, (int)format, (BitmapData*)Unsafe.AsPointer(ref data)).ThrowIfFailed();
GC.KeepAlive(bitmap);
}
public unsafe static void UnlockBits(this IPointer<GpBitmap> bitmap, ref BitmapData data)
{
PInvokeGdiPlus.GdipBitmapUnlockBits(bitmap.GetPointer(), (BitmapData*)Unsafe.AsPointer(ref data)).ThrowIfFailed();
GC.KeepAlive(bitmap);
}
public static HBITMAP GetHBITMAP(this IPointer<GpBitmap> bitmap)
{
return bitmap.GetHBITMAP(global::System.Drawing.Color.LightGray);
}
public unsafe static HBITMAP GetHBITMAP(this IPointer<GpBitmap> bitmap, global::System.Drawing.Color background)
{
HBITMAP result = default(HBITMAP);
PInvokeGdiPlus.GdipCreateHBITMAPFromBitmap(bitmap.GetPointer(), &result, (uint)ColorTranslator.ToWin32(background)).ThrowIfFailed();
GC.KeepAlive(bitmap);
return result;
}
public static PICTDESC CreatePICTDESC(this IPointer<GpBitmap> bitmap, HPALETTE paletteHandle = default(HPALETTE))
{
PICTDESC pICTDESC = default(PICTDESC);
pICTDESC.picType = PICTYPE.PICTYPE_BITMAP;
PICTDESC result = pICTDESC;
result.Anonymous.bmp.hbitmap = bitmap.GetHBITMAP();
result.Anonymous.bmp.hpal = paletteHandle;
return result;
}
}
}