-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathRegion.cs
More file actions
428 lines (334 loc) · 14.7 KB
/
Region.cs
File metadata and controls
428 lines (334 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Drawing.Drawing2D;
using System.Drawing.Internal;
using System.Globalization;
using System.Runtime.InteropServices;
using Gdip = System.Drawing.SafeNativeMethods.Gdip;
namespace System.Drawing
{
public sealed class Region : MarshalByRefObject, IDisposable
{
#if FINALIZATION_WATCH
private string allocationSite = Graphics.GetAllocationStack();
#endif
internal IntPtr NativeRegion { get; private set; }
public Region()
{
Gdip.CheckStatus(Gdip.GdipCreateRegion(out IntPtr region));
SetNativeRegion(region);
}
public Region(RectangleF rect)
{
Gdip.CheckStatus(Gdip.GdipCreateRegionRect(ref rect, out IntPtr region));
SetNativeRegion(region);
}
public Region(Rectangle rect)
{
Gdip.CheckStatus(Gdip.GdipCreateRegionRectI(ref rect, out IntPtr region));
SetNativeRegion(region);
}
public Region(GraphicsPath path)
{
ArgumentNullException.ThrowIfNull(path);
Gdip.CheckStatus(Gdip.GdipCreateRegionPath(new HandleRef(path, path._nativePath), out IntPtr region));
SetNativeRegion(region);
}
public Region(RegionData rgnData)
{
ArgumentNullException.ThrowIfNull(rgnData);
Gdip.CheckStatus(Gdip.GdipCreateRegionRgnData(
rgnData.Data,
rgnData.Data.Length,
out IntPtr region));
SetNativeRegion(region);
}
internal Region(IntPtr nativeRegion) => SetNativeRegion(nativeRegion);
public static Region FromHrgn(IntPtr hrgn)
{
Gdip.CheckStatus(Gdip.GdipCreateRegionHrgn(hrgn, out IntPtr region));
return new Region(region);
}
private void SetNativeRegion(IntPtr nativeRegion)
{
if (nativeRegion == IntPtr.Zero)
throw new ArgumentNullException(nameof(nativeRegion));
NativeRegion = nativeRegion;
}
public Region Clone()
{
Gdip.CheckStatus(Gdip.GdipCloneRegion(new HandleRef(this, NativeRegion), out IntPtr region));
return new Region(region);
}
public void ReleaseHrgn(IntPtr regionHandle)
{
if (regionHandle == IntPtr.Zero)
{
throw new ArgumentNullException(nameof(regionHandle));
}
Interop.Gdi32.DeleteObject(new HandleRef(this, regionHandle));
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
#if FINALIZATION_WATCH
if (!disposing && nativeRegion != IntPtr.Zero)
Debug.WriteLine("**********************\nDisposed through finalization:\n" + allocationSite);
#endif
if (NativeRegion != IntPtr.Zero)
{
try
{
#if DEBUG
int status = !Gdip.Initialized ? Gdip.Ok :
#endif
Gdip.GdipDeleteRegion(new HandleRef(this, NativeRegion));
#if DEBUG
Debug.Assert(status == Gdip.Ok, $"GDI+ returned an error status: {status.ToString(CultureInfo.InvariantCulture)}");
#endif
}
catch (Exception ex) when (!ClientUtils.IsSecurityOrCriticalException(ex))
{
}
finally
{
NativeRegion = IntPtr.Zero;
}
}
}
~Region() => Dispose(false);
public void MakeInfinite()
{
Gdip.CheckStatus(Gdip.GdipSetInfinite(new HandleRef(this, NativeRegion)));
}
public void MakeEmpty()
{
Gdip.CheckStatus(Gdip.GdipSetEmpty(new HandleRef(this, NativeRegion)));
}
public void Intersect(RectangleF rect)
{
Gdip.CheckStatus(Gdip.GdipCombineRegionRect(new HandleRef(this, NativeRegion), ref rect, CombineMode.Intersect));
}
public void Intersect(Rectangle rect)
{
Gdip.CheckStatus(Gdip.GdipCombineRegionRectI(new HandleRef(this, NativeRegion), ref rect, CombineMode.Intersect));
}
public void Intersect(GraphicsPath path)
{
ArgumentNullException.ThrowIfNull(path);
Gdip.CheckStatus(Gdip.GdipCombineRegionPath(new HandleRef(this, NativeRegion), new HandleRef(path, path._nativePath), CombineMode.Intersect));
}
public void Intersect(Region region)
{
ArgumentNullException.ThrowIfNull(region);
Gdip.CheckStatus(Gdip.GdipCombineRegionRegion(new HandleRef(this, NativeRegion), new HandleRef(region, region.NativeRegion), CombineMode.Intersect));
}
public void Union(RectangleF rect)
{
Gdip.CheckStatus(Gdip.GdipCombineRegionRect(new HandleRef(this, NativeRegion), ref rect, CombineMode.Union));
}
public void Union(Rectangle rect)
{
Gdip.CheckStatus(Gdip.GdipCombineRegionRectI(new HandleRef(this, NativeRegion), ref rect, CombineMode.Union));
}
public void Union(GraphicsPath path)
{
ArgumentNullException.ThrowIfNull(path);
Gdip.CheckStatus(Gdip.GdipCombineRegionPath(new HandleRef(this, NativeRegion), new HandleRef(path, path._nativePath), CombineMode.Union));
}
public void Union(Region region)
{
ArgumentNullException.ThrowIfNull(region);
Gdip.CheckStatus(Gdip.GdipCombineRegionRegion(new HandleRef(this, NativeRegion), new HandleRef(region, region.NativeRegion), CombineMode.Union));
}
public void Xor(RectangleF rect)
{
Gdip.CheckStatus(Gdip.GdipCombineRegionRect(new HandleRef(this, NativeRegion), ref rect, CombineMode.Xor));
}
public void Xor(Rectangle rect)
{
Gdip.CheckStatus(Gdip.GdipCombineRegionRectI(new HandleRef(this, NativeRegion), ref rect, CombineMode.Xor));
}
public void Xor(GraphicsPath path)
{
ArgumentNullException.ThrowIfNull(path);
Gdip.CheckStatus(Gdip.GdipCombineRegionPath(new HandleRef(this, NativeRegion), new HandleRef(path, path._nativePath), CombineMode.Xor));
}
public void Xor(Region region)
{
ArgumentNullException.ThrowIfNull(region);
Gdip.CheckStatus(Gdip.GdipCombineRegionRegion(new HandleRef(this, NativeRegion), new HandleRef(region, region.NativeRegion), CombineMode.Xor));
}
public void Exclude(RectangleF rect)
{
Gdip.CheckStatus(Gdip.GdipCombineRegionRect(new HandleRef(this, NativeRegion), ref rect, CombineMode.Exclude));
}
public void Exclude(Rectangle rect)
{
Gdip.CheckStatus(Gdip.GdipCombineRegionRectI(new HandleRef(this, NativeRegion), ref rect, CombineMode.Exclude));
}
public void Exclude(GraphicsPath path)
{
ArgumentNullException.ThrowIfNull(path);
Gdip.CheckStatus(Gdip.GdipCombineRegionPath(
new HandleRef(this, NativeRegion),
new HandleRef(path, path._nativePath),
CombineMode.Exclude));
}
public void Exclude(Region region)
{
ArgumentNullException.ThrowIfNull(region);
Gdip.CheckStatus(Gdip.GdipCombineRegionRegion(
new HandleRef(this, NativeRegion),
new HandleRef(region, region.NativeRegion),
CombineMode.Exclude));
}
public void Complement(RectangleF rect)
{
Gdip.CheckStatus(Gdip.GdipCombineRegionRect(new HandleRef(this, NativeRegion), ref rect, CombineMode.Complement));
}
public void Complement(Rectangle rect)
{
Gdip.CheckStatus(Gdip.GdipCombineRegionRectI(new HandleRef(this, NativeRegion), ref rect, CombineMode.Complement));
}
public void Complement(GraphicsPath path)
{
ArgumentNullException.ThrowIfNull(path);
Gdip.CheckStatus(Gdip.GdipCombineRegionPath(new HandleRef(this, NativeRegion), new HandleRef(path, path._nativePath), CombineMode.Complement));
}
public void Complement(Region region)
{
ArgumentNullException.ThrowIfNull(region);
Gdip.CheckStatus(Gdip.GdipCombineRegionRegion(new HandleRef(this, NativeRegion), new HandleRef(region, region.NativeRegion), CombineMode.Complement));
}
public void Translate(float dx, float dy)
{
Gdip.CheckStatus(Gdip.GdipTranslateRegion(new HandleRef(this, NativeRegion), dx, dy));
}
public void Translate(int dx, int dy)
{
Gdip.CheckStatus(Gdip.GdipTranslateRegionI(new HandleRef(this, NativeRegion), dx, dy));
}
public void Transform(Matrix matrix)
{
ArgumentNullException.ThrowIfNull(matrix);
Gdip.CheckStatus(Gdip.GdipTransformRegion(
new HandleRef(this, NativeRegion),
new HandleRef(matrix, matrix.NativeMatrix)));
}
public RectangleF GetBounds(Graphics g)
{
ArgumentNullException.ThrowIfNull(g);
Gdip.CheckStatus(Gdip.GdipGetRegionBounds(new HandleRef(this, NativeRegion), new HandleRef(g, g.NativeGraphics), out RectangleF bounds));
return bounds;
}
public IntPtr GetHrgn(Graphics g)
{
ArgumentNullException.ThrowIfNull(g);
Gdip.CheckStatus(Gdip.GdipGetRegionHRgn(new HandleRef(this, NativeRegion), new HandleRef(g, g.NativeGraphics), out IntPtr hrgn));
return hrgn;
}
public bool IsEmpty(Graphics g)
{
ArgumentNullException.ThrowIfNull(g);
Gdip.CheckStatus(Gdip.GdipIsEmptyRegion(new HandleRef(this, NativeRegion), new HandleRef(g, g.NativeGraphics), out int isEmpty));
return isEmpty != 0;
}
public bool IsInfinite(Graphics g)
{
ArgumentNullException.ThrowIfNull(g);
Gdip.CheckStatus(Gdip.GdipIsInfiniteRegion(new HandleRef(this, NativeRegion), new HandleRef(g, g.NativeGraphics), out int isInfinite));
return isInfinite != 0;
}
public bool Equals(Region region, Graphics g)
{
ArgumentNullException.ThrowIfNull(region);
ArgumentNullException.ThrowIfNull(g);
Gdip.CheckStatus(Gdip.GdipIsEqualRegion(new HandleRef(this, NativeRegion), new HandleRef(region, region.NativeRegion), new HandleRef(g, g.NativeGraphics), out int isEqual));
return isEqual != 0;
}
public RegionData? GetRegionData()
{
Gdip.CheckStatus(Gdip.GdipGetRegionDataSize(new HandleRef(this, NativeRegion), out int regionSize));
if (regionSize == 0)
return null;
byte[] regionData = new byte[regionSize];
Gdip.CheckStatus(Gdip.GdipGetRegionData(new HandleRef(this, NativeRegion), regionData, regionSize, out _));
return new RegionData(regionData);
}
public bool IsVisible(float x, float y) => IsVisible(new PointF(x, y), null);
public bool IsVisible(PointF point) => IsVisible(point, null);
public bool IsVisible(float x, float y, Graphics? g) => IsVisible(new PointF(x, y), g);
public bool IsVisible(PointF point, Graphics? g)
{
Gdip.CheckStatus(Gdip.GdipIsVisibleRegionPoint(
new HandleRef(this, NativeRegion),
point.X, point.Y,
new HandleRef(g, g?.NativeGraphics ?? IntPtr.Zero),
out int isVisible));
return isVisible != 0;
}
public bool IsVisible(float x, float y, float width, float height) => IsVisible(new RectangleF(x, y, width, height), null);
public bool IsVisible(RectangleF rect) => IsVisible(rect, null);
public bool IsVisible(float x, float y, float width, float height, Graphics? g) => IsVisible(new RectangleF(x, y, width, height), g);
public bool IsVisible(RectangleF rect, Graphics? g)
{
Gdip.CheckStatus(Gdip.GdipIsVisibleRegionRect(
new HandleRef(this, NativeRegion),
rect.X, rect.Y, rect.Width, rect.Height,
new HandleRef(g, g?.NativeGraphics ?? IntPtr.Zero),
out int isVisible));
return isVisible != 0;
}
public bool IsVisible(int x, int y, Graphics? g) => IsVisible(new Point(x, y), g);
public bool IsVisible(Point point) => IsVisible(point, null);
public bool IsVisible(Point point, Graphics? g)
{
Gdip.CheckStatus(Gdip.GdipIsVisibleRegionPointI(
new HandleRef(this, NativeRegion),
point.X, point.Y,
new HandleRef(g, g?.NativeGraphics ?? IntPtr.Zero),
out int isVisible));
return isVisible != 0;
}
public bool IsVisible(int x, int y, int width, int height) => IsVisible(new Rectangle(x, y, width, height), null);
public bool IsVisible(Rectangle rect) => IsVisible(rect, null);
public bool IsVisible(int x, int y, int width, int height, Graphics? g) => IsVisible(new Rectangle(x, y, width, height), g);
public bool IsVisible(Rectangle rect, Graphics? g)
{
Gdip.CheckStatus(Gdip.GdipIsVisibleRegionRectI(
new HandleRef(this, NativeRegion),
rect.X, rect.Y, rect.Width, rect.Height,
new HandleRef(g, g?.NativeGraphics ?? IntPtr.Zero),
out int isVisible));
return isVisible != 0;
}
public unsafe RectangleF[] GetRegionScans(Matrix matrix)
{
ArgumentNullException.ThrowIfNull(matrix);
Gdip.CheckStatus(Gdip.GdipGetRegionScansCount(
new HandleRef(this, NativeRegion),
out int count,
new HandleRef(matrix, matrix.NativeMatrix)));
RectangleF[] rectangles = new RectangleF[count];
// Pinning an empty array gives null, libgdiplus doesn't like this.
// As invoking isn't necessary, just return the empty array.
if (count == 0)
return rectangles;
fixed (RectangleF* r = rectangles)
{
Gdip.CheckStatus(Gdip.GdipGetRegionScans
(new HandleRef(this, NativeRegion),
r,
out count,
new HandleRef(matrix, matrix.NativeMatrix)));
}
return rectangles;
}
}
}