Region
Describes the interior of a graphics shape composed of rectangles and paths. This class cannot be inherited.
using System.Drawing.Drawing2D;
using System.Runtime.CompilerServices;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Gdi;
using Windows.Win32.Graphics.GdiPlus;
namespace System.Drawing
{
[NullableContext(1)]
[Nullable(0)]
public sealed class Region : MarshalByRefObject, IDisposable, IPointer<GpRegion>
{
[Nullable(0)]
[field: Nullable(0)]
internal unsafe GpRegion* NativeRegion {
[NullableContext(0)]
get;
[NullableContext(0)]
private set;
}
unsafe IntPtr IPointer<GpRegion>.Pointer {
get {
return (IntPtr)NativeRegion;
}
}
public unsafe Region()
{
GpRegion* nativeRegion = default(GpRegion*);
CheckStatus(PInvokeGdiPlus.GdipCreateRegion(&nativeRegion));
SetNativeRegion(nativeRegion);
}
public unsafe Region(RectangleF rect)
{
GpRegion* nativeRegion = default(GpRegion*);
RectF rectF = rect;
CheckStatus(PInvokeGdiPlus.GdipCreateRegionRect(&rectF, &nativeRegion));
SetNativeRegion(nativeRegion);
}
public Region(Rectangle rect)
: this((RectangleF)rect)
{
}
public unsafe Region(GraphicsPath path)
{
ArgumentNullException.ThrowIfNull(path, "path");
GpRegion* nativeRegion = default(GpRegion*);
CheckStatus(PInvokeGdiPlus.GdipCreateRegionPath(path._nativePath, &nativeRegion));
GC.KeepAlive(path);
SetNativeRegion(nativeRegion);
}
public unsafe Region(RegionData rgnData)
{
ArgumentNullException.ThrowIfNull(rgnData, "rgnData");
GpRegion* nativeRegion = default(GpRegion*);
fixed (byte* regionData = rgnData.Data) {
CheckStatus(PInvokeGdiPlus.GdipCreateRegionRgnData(regionData, rgnData.Data.Length, &nativeRegion));
}
SetNativeRegion(nativeRegion);
}
[NullableContext(0)]
internal unsafe Region(GpRegion* nativeRegion)
{
SetNativeRegion(nativeRegion);
}
public unsafe static Region FromHrgn(IntPtr hrgn)
{
GpRegion* nativeRegion = default(GpRegion*);
Gdip.CheckStatus(PInvokeGdiPlus.GdipCreateRegionHrgn((HRGN)hrgn, &nativeRegion));
return new Region(nativeRegion);
}
[NullableContext(0)]
private unsafe void SetNativeRegion(GpRegion* nativeRegion)
{
if (nativeRegion == null)
throw new ArgumentNullException("nativeRegion");
NativeRegion = nativeRegion;
}
public unsafe Region Clone()
{
GpRegion* nativeRegion = default(GpRegion*);
CheckStatus(PInvokeGdiPlus.GdipCloneRegion(NativeRegion, &nativeRegion));
return new Region(nativeRegion);
}
public void ReleaseHrgn(IntPtr regionHandle)
{
if (regionHandle == IntPtr.Zero)
throw new ArgumentNullException("regionHandle");
PInvokeCore.DeleteObject((HRGN)regionHandle);
GC.KeepAlive(this);
}
public unsafe void Dispose()
{
if (NativeRegion != null) {
if (Gdip.Initialized)
PInvokeGdiPlus.GdipDeleteRegion(NativeRegion);
NativeRegion = null;
}
GC.SuppressFinalize(this);
}
~Region()
{
Dispose();
}
public unsafe void MakeInfinite()
{
CheckStatus(PInvokeGdiPlus.GdipSetInfinite(NativeRegion));
}
public unsafe void MakeEmpty()
{
CheckStatus(PInvokeGdiPlus.GdipSetEmpty(NativeRegion));
}
public unsafe void Intersect(RectangleF rect)
{
CheckStatus(PInvokeGdiPlus.GdipCombineRegionRect(NativeRegion, (RectF*)(&rect), global::Windows.Win32.Graphics.GdiPlus.CombineMode.CombineModeIntersect));
}
public void Intersect(Rectangle rect)
{
Intersect((RectangleF)rect);
}
public unsafe void Intersect(GraphicsPath path)
{
ArgumentNullException.ThrowIfNull(path, "path");
CheckStatus(PInvokeGdiPlus.GdipCombineRegionPath(NativeRegion, path._nativePath, global::Windows.Win32.Graphics.GdiPlus.CombineMode.CombineModeIntersect));
GC.KeepAlive(path);
}
public unsafe void Intersect(Region region)
{
ArgumentNullException.ThrowIfNull(region, "region");
CheckStatus(PInvokeGdiPlus.GdipCombineRegionRegion(NativeRegion, region.NativeRegion, global::Windows.Win32.Graphics.GdiPlus.CombineMode.CombineModeIntersect));
GC.KeepAlive(region);
}
public unsafe void Union(RectangleF rect)
{
CheckStatus(PInvokeGdiPlus.GdipCombineRegionRect(NativeRegion, (RectF*)(&rect), global::Windows.Win32.Graphics.GdiPlus.CombineMode.CombineModeUnion));
}
public void Union(Rectangle rect)
{
Union((RectangleF)rect);
}
public unsafe void Union(GraphicsPath path)
{
ArgumentNullException.ThrowIfNull(path, "path");
CheckStatus(PInvokeGdiPlus.GdipCombineRegionPath(NativeRegion, path._nativePath, global::Windows.Win32.Graphics.GdiPlus.CombineMode.CombineModeUnion));
GC.KeepAlive(path);
}
public unsafe void Union(Region region)
{
ArgumentNullException.ThrowIfNull(region, "region");
CheckStatus(PInvokeGdiPlus.GdipCombineRegionRegion(NativeRegion, region.NativeRegion, global::Windows.Win32.Graphics.GdiPlus.CombineMode.CombineModeUnion));
GC.KeepAlive(region);
}
public unsafe void Xor(RectangleF rect)
{
CheckStatus(PInvokeGdiPlus.GdipCombineRegionRect(NativeRegion, (RectF*)(&rect), global::Windows.Win32.Graphics.GdiPlus.CombineMode.CombineModeXor));
}
public void Xor(Rectangle rect)
{
Xor((RectangleF)rect);
}
public unsafe void Xor(GraphicsPath path)
{
ArgumentNullException.ThrowIfNull(path, "path");
CheckStatus(PInvokeGdiPlus.GdipCombineRegionPath(NativeRegion, path._nativePath, global::Windows.Win32.Graphics.GdiPlus.CombineMode.CombineModeXor));
GC.KeepAlive(path);
}
public unsafe void Xor(Region region)
{
ArgumentNullException.ThrowIfNull(region, "region");
CheckStatus(PInvokeGdiPlus.GdipCombineRegionRegion(NativeRegion, region.NativeRegion, global::Windows.Win32.Graphics.GdiPlus.CombineMode.CombineModeXor));
GC.KeepAlive(region);
}
public unsafe void Exclude(RectangleF rect)
{
CheckStatus(PInvokeGdiPlus.GdipCombineRegionRect(NativeRegion, (RectF*)(&rect), global::Windows.Win32.Graphics.GdiPlus.CombineMode.CombineModeExclude));
}
public void Exclude(Rectangle rect)
{
Exclude((RectangleF)rect);
}
public unsafe void Exclude(GraphicsPath path)
{
ArgumentNullException.ThrowIfNull(path, "path");
CheckStatus(PInvokeGdiPlus.GdipCombineRegionPath(NativeRegion, path._nativePath, global::Windows.Win32.Graphics.GdiPlus.CombineMode.CombineModeExclude));
GC.KeepAlive(path);
}
public unsafe void Exclude(Region region)
{
ArgumentNullException.ThrowIfNull(region, "region");
CheckStatus(PInvokeGdiPlus.GdipCombineRegionRegion(NativeRegion, region.NativeRegion, global::Windows.Win32.Graphics.GdiPlus.CombineMode.CombineModeExclude));
GC.KeepAlive(region);
}
public unsafe void Complement(RectangleF rect)
{
CheckStatus(PInvokeGdiPlus.GdipCombineRegionRect(NativeRegion, (RectF*)(&rect), global::Windows.Win32.Graphics.GdiPlus.CombineMode.CombineModeComplement));
}
public void Complement(Rectangle rect)
{
Complement((RectangleF)rect);
}
public unsafe void Complement(GraphicsPath path)
{
ArgumentNullException.ThrowIfNull(path, "path");
CheckStatus(PInvokeGdiPlus.GdipCombineRegionPath(NativeRegion, path._nativePath, global::Windows.Win32.Graphics.GdiPlus.CombineMode.CombineModeComplement));
GC.KeepAlive(path);
}
public unsafe void Complement(Region region)
{
ArgumentNullException.ThrowIfNull(region, "region");
CheckStatus(PInvokeGdiPlus.GdipCombineRegionRegion(NativeRegion, region.NativeRegion, global::Windows.Win32.Graphics.GdiPlus.CombineMode.CombineModeComplement));
GC.KeepAlive(region);
}
public unsafe void Translate(float dx, float dy)
{
CheckStatus(PInvokeGdiPlus.GdipTranslateRegion(NativeRegion, dx, dy));
}
public void Translate(int dx, int dy)
{
Translate((float)dx, (float)dy);
}
public unsafe void Transform(System.Drawing.Drawing2D.Matrix matrix)
{
ArgumentNullException.ThrowIfNull(matrix, "matrix");
CheckStatus(PInvokeGdiPlus.GdipTransformRegion(NativeRegion, matrix.NativeMatrix));
GC.KeepAlive(matrix);
}
public unsafe RectangleF GetBounds(Graphics g)
{
ArgumentNullException.ThrowIfNull(g, "g");
RectF rect = default(RectF);
CheckStatus(PInvokeGdiPlus.GdipGetRegionBounds(NativeRegion, g.NativeGraphics, &rect));
GC.KeepAlive(g);
return rect;
}
public unsafe IntPtr GetHrgn(Graphics g)
{
ArgumentNullException.ThrowIfNull(g, "g");
HRGN value = default(HRGN);
CheckStatus(PInvokeGdiPlus.GdipGetRegionHRgn(NativeRegion, g.NativeGraphics, &value));
GC.KeepAlive(g);
return value;
}
public unsafe bool IsEmpty(Graphics g)
{
ArgumentNullException.ThrowIfNull(g, "g");
BOOL value = default(BOOL);
CheckStatus(PInvokeGdiPlus.GdipIsEmptyRegion(NativeRegion, g.NativeGraphics, &value));
GC.KeepAlive(g);
return value;
}
public unsafe bool IsInfinite(Graphics g)
{
ArgumentNullException.ThrowIfNull(g, "g");
BOOL value = default(BOOL);
CheckStatus(PInvokeGdiPlus.GdipIsInfiniteRegion(NativeRegion, g.NativeGraphics, &value));
return value;
}
public unsafe bool Equals(Region region, Graphics g)
{
ArgumentNullException.ThrowIfNull(region, "region");
ArgumentNullException.ThrowIfNull(g, "g");
BOOL value = default(BOOL);
CheckStatus(PInvokeGdiPlus.GdipIsEqualRegion(NativeRegion, region.NativeRegion, g.NativeGraphics, &value));
GC.KeepAlive(g);
GC.KeepAlive(region);
return value;
}
[NullableContext(2)]
public unsafe RegionData GetRegionData()
{
uint num = default(uint);
CheckStatus(PInvokeGdiPlus.GdipGetRegionDataSize(NativeRegion, &num));
if (num == 0)
return null;
byte[] obj = new byte[num];
fixed (byte* buffer = obj) {
CheckStatus(PInvokeGdiPlus.GdipGetRegionData(NativeRegion, buffer, num, &num));
}
return new RegionData(obj);
}
public bool IsVisible(float x, float y)
{
return IsVisible(new PointF(x, y), null);
}
public bool IsVisible(PointF point)
{
return IsVisible(point, null);
}
[NullableContext(2)]
public bool IsVisible(float x, float y, Graphics g)
{
return IsVisible(new PointF(x, y), g);
}
[NullableContext(2)]
public unsafe bool IsVisible(PointF point, Graphics g)
{
BOOL value = default(BOOL);
CheckStatus(PInvokeGdiPlus.GdipIsVisibleRegionPoint(NativeRegion, point.X, point.Y, (GpGraphics*)(long)((g == null) ? ((IntPtr)(void*)null) : ((IntPtr)g.NativeGraphics)), &value));
GC.KeepAlive(g);
return value;
}
public bool IsVisible(float x, float y, float width, float height)
{
return IsVisible(new RectangleF(x, y, width, height), null);
}
public bool IsVisible(RectangleF rect)
{
return IsVisible(rect, null);
}
[NullableContext(2)]
public bool IsVisible(float x, float y, float width, float height, Graphics g)
{
return IsVisible(new RectangleF(x, y, width, height), g);
}
[NullableContext(2)]
public unsafe bool IsVisible(RectangleF rect, Graphics g)
{
BOOL value = default(BOOL);
CheckStatus(PInvokeGdiPlus.GdipIsVisibleRegionRect(NativeRegion, rect.X, rect.Y, rect.Width, rect.Height, (GpGraphics*)(long)((g == null) ? ((IntPtr)(void*)null) : ((IntPtr)g.NativeGraphics)), &value));
GC.KeepAlive(g);
return value;
}
[NullableContext(2)]
public bool IsVisible(int x, int y, Graphics g)
{
return IsVisible(new Point(x, y), g);
}
public bool IsVisible(Point point)
{
return IsVisible(point, null);
}
[NullableContext(2)]
public bool IsVisible(Point point, Graphics g)
{
return IsVisible((PointF)point, g);
}
public bool IsVisible(int x, int y, int width, int height)
{
return IsVisible(new Rectangle(x, y, width, height), null);
}
public bool IsVisible(Rectangle rect)
{
return IsVisible(rect, null);
}
[NullableContext(2)]
public bool IsVisible(int x, int y, int width, int height, Graphics g)
{
return IsVisible(new Rectangle(x, y, width, height), g);
}
[NullableContext(2)]
public bool IsVisible(Rectangle rect, Graphics g)
{
return IsVisible((RectangleF)rect, g);
}
public unsafe RectangleF[] GetRegionScans(System.Drawing.Drawing2D.Matrix matrix)
{
ArgumentNullException.ThrowIfNull(matrix, "matrix");
uint num = default(uint);
CheckStatus(PInvokeGdiPlus.GdipGetRegionScansCount(NativeRegion, &num, matrix.NativeMatrix));
if (num == 0)
return Array.Empty<RectangleF>();
RectangleF[] obj = new RectangleF[num];
fixed (RectangleF* rects = obj) {
CheckStatus(PInvokeGdiPlus.GdipGetRegionScans(NativeRegion, (RectF*)rects, (int*)(&num), matrix.NativeMatrix));
}
GC.KeepAlive(matrix);
return obj;
}
private void CheckStatus(Status status)
{
Gdip.CheckStatus(status);
GC.KeepAlive(this);
}
}
}