-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathGraphics.cs
More file actions
3653 lines (3118 loc) · 134 KB
/
Graphics.cs
File metadata and controls
3653 lines (3118 loc) · 134 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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
#if NET9_0_OR_GREATER
using System.Drawing.Imaging.Effects;
#endif
using System.Numerics;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
namespace System.Drawing;
/// <summary>
/// Encapsulates a GDI+ drawing surface.
/// </summary>
public sealed unsafe partial class Graphics : MarshalByRefObject, IDisposable, IDeviceContext, IGraphics
{
/// <summary>
/// The context state previous to the current Graphics context (the head of the stack).
/// We don't keep a GraphicsContext for the current context since it is available at any time from GDI+ and
/// we don't want to keep track of changes in it.
/// </summary>
private GraphicsContext? _previousContext;
private static readonly object s_syncObject = new();
// Object reference used for printing; it could point to a PrintPreviewGraphics to obtain the VisibleClipBounds, or
// a DeviceContext holding a printer DC.
private object? _printingHelper;
// GDI+'s preferred HPALETTE.
private static HPALETTE s_halftonePalette;
// pointer back to the Image backing a specific graphic object
private Image? _backingImage;
/// <summary>
/// Handle to native DC - obtained from the GDI+ graphics object. We need to cache it to implement
/// IDeviceContext interface.
/// </summary>
private HDC _nativeHdc;
public delegate bool DrawImageAbort(IntPtr callbackdata);
/// <summary>
/// Callback for EnumerateMetafile methods.
/// This method can then call Metafile.PlayRecord to play the record that was just enumerated.
/// </summary>
/// <param name="recordType">if >= MinRecordType, it's an EMF+ record</param>
/// <param name="flags">always 0 for EMF records</param>
/// <param name="dataSize">size of the data, or 0 if no data</param>
/// <param name="data">pointer to the data, or NULL if no data (UINT32 aligned)</param>
/// <param name="callbackData">pointer to callbackData, if any</param>
/// <returns>False to abort enumerating, true to continue.</returns>
public delegate bool EnumerateMetafileProc(
EmfPlusRecordType recordType,
int flags,
int dataSize,
IntPtr data,
PlayRecordCallback? callbackData);
/// <summary>
/// Constructor to initialize this object from a native GDI+ Graphics pointer.
/// </summary>
private Graphics(GpGraphics* gdipNativeGraphics)
{
if (gdipNativeGraphics is null)
throw new ArgumentNullException(nameof(gdipNativeGraphics));
NativeGraphics = gdipNativeGraphics;
}
/// <summary>
/// Creates a new instance of the <see cref='Graphics'/> class from the specified handle to a device context.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public static Graphics FromHdc(IntPtr hdc)
{
if (hdc == IntPtr.Zero)
throw new ArgumentNullException(nameof(hdc));
return FromHdcInternal(hdc);
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
public static Graphics FromHdcInternal(IntPtr hdc)
{
GpGraphics* nativeGraphics;
Gdip.CheckStatus(PInvoke.GdipCreateFromHDC((HDC)hdc, &nativeGraphics));
return new Graphics(nativeGraphics);
}
/// <summary>
/// Creates a new instance of the Graphics class from the specified handle to a device context and handle to a device.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public static Graphics FromHdc(IntPtr hdc, IntPtr hdevice)
{
GpGraphics* nativeGraphics;
Gdip.CheckStatus(PInvoke.GdipCreateFromHDC2((HDC)hdc, (HANDLE)hdevice, &nativeGraphics));
return new Graphics(nativeGraphics);
}
/// <summary>
/// Creates a new instance of the <see cref='Graphics'/> class from a window handle.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Advanced)]
public static Graphics FromHwnd(IntPtr hwnd) => FromHwndInternal(hwnd);
[EditorBrowsable(EditorBrowsableState.Advanced)]
public static Graphics FromHwndInternal(IntPtr hwnd)
{
GpGraphics* nativeGraphics;
// This is one of the few places we need to manually ensure GDI+ is initialized. Other calls to PInvoke will do
// this automatically, PInvokeCore cannot and as such needs to be manually initialized if we've never called
// another PInvoke method.
GdiPlusInitialization.EnsureInitialized();
Gdip.CheckStatus(PInvokeCore.GdipCreateFromHWND((HWND)hwnd, &nativeGraphics));
return new Graphics(nativeGraphics);
}
/// <summary>
/// Creates an instance of the <see cref='Graphics'/> class from an existing <see cref='Image'/>.
/// </summary>
public static Graphics FromImage(Image image)
{
ArgumentNullException.ThrowIfNull(image);
if ((image.PixelFormat & PixelFormat.Indexed) != 0)
throw new ArgumentException(SR.GdiplusCannotCreateGraphicsFromIndexedPixelFormat, nameof(image));
GpGraphics* nativeGraphics;
Gdip.CheckStatus(PInvoke.GdipGetImageGraphicsContext(image.Pointer(), &nativeGraphics));
GC.KeepAlive(image);
return new Graphics(nativeGraphics) { _backingImage = image };
}
[EditorBrowsable(EditorBrowsableState.Never)]
public void ReleaseHdcInternal(IntPtr hdc)
{
CheckStatus(!Gdip.Initialized ? Status.Ok : PInvoke.GdipReleaseDC(NativeGraphics, (HDC)hdc));
_nativeHdc = HDC.Null;
}
/// <summary>
/// Deletes this <see cref='Graphics'/>, and frees the memory allocated for it.
/// </summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (disposing)
{
while (_previousContext is not null)
{
// Dispose entire stack.
GraphicsContext? context = _previousContext.Previous;
_previousContext.Dispose();
_previousContext = context;
}
if (PrintingHelper is HdcHandle printerDC)
{
printerDC.Dispose();
_printingHelper = null;
}
}
if (!_nativeHdc.IsNull)
{
ReleaseHdc();
}
if (NativeGraphics is not null)
{
Status status = !Gdip.Initialized ? Status.Ok : PInvokeCore.GdipDeleteGraphics(NativeGraphics);
NativeGraphics = null;
Debug.Assert(status == Status.Ok, $"GDI+ returned an error status: {status}");
}
}
~Graphics() => Dispose(disposing: false);
/// <summary>
/// Handle to native GDI+ graphics object. This object is created on demand.
/// </summary>
internal GpGraphics* NativeGraphics { get; private set; }
nint IPointer<GpGraphics>.Pointer => (nint)NativeGraphics;
public Region Clip
{
get
{
Region region = new();
CheckStatus(PInvoke.GdipGetClip(NativeGraphics, region.NativeRegion));
return region;
}
set => SetClip(value, Drawing2D.CombineMode.Replace);
}
public RectangleF ClipBounds
{
get
{
RectF rect;
CheckStatus(PInvoke.GdipGetClipBounds(NativeGraphics, &rect));
return rect;
}
}
/// <summary>
/// Gets or sets the <see cref='Drawing2D.CompositingMode'/> associated with this <see cref='Graphics'/>.
/// </summary>
public Drawing2D.CompositingMode CompositingMode
{
get
{
GdiPlus.CompositingMode mode;
CheckStatus(PInvoke.GdipGetCompositingMode(NativeGraphics, &mode));
return (Drawing2D.CompositingMode)mode;
}
set
{
if (value is < Drawing2D.CompositingMode.SourceOver or > Drawing2D.CompositingMode.SourceCopy)
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(Drawing2D.CompositingMode));
CheckStatus(PInvoke.GdipSetCompositingMode(NativeGraphics, (GdiPlus.CompositingMode)value));
}
}
public Drawing2D.CompositingQuality CompositingQuality
{
get
{
GdiPlus.CompositingQuality quality;
CheckStatus(PInvoke.GdipGetCompositingQuality(NativeGraphics, &quality));
return (Drawing2D.CompositingQuality)quality;
}
set
{
if (value is < Drawing2D.CompositingQuality.Invalid or > Drawing2D.CompositingQuality.AssumeLinear)
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(Drawing2D.CompositingQuality));
CheckStatus(PInvoke.GdipSetCompositingQuality(NativeGraphics, (GdiPlus.CompositingQuality)value));
}
}
public float DpiX
{
get
{
float dpi;
CheckStatus(PInvoke.GdipGetDpiX(NativeGraphics, &dpi));
return dpi;
}
}
public float DpiY
{
get
{
float dpi;
CheckStatus(PInvoke.GdipGetDpiY(NativeGraphics, &dpi));
return dpi;
}
}
/// <summary>
/// Gets or sets the interpolation mode associated with this Graphics.
/// </summary>
public Drawing2D.InterpolationMode InterpolationMode
{
get
{
GdiPlus.InterpolationMode mode;
CheckStatus(PInvoke.GdipGetInterpolationMode(NativeGraphics, &mode));
return (Drawing2D.InterpolationMode)mode;
}
set
{
if (value is < Drawing2D.InterpolationMode.Invalid or > Drawing2D.InterpolationMode.HighQualityBicubic)
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(Drawing2D.InterpolationMode));
CheckStatus(PInvoke.GdipSetInterpolationMode(NativeGraphics, (GdiPlus.InterpolationMode)value));
}
}
public bool IsClipEmpty
{
get
{
BOOL isEmpty;
CheckStatus(PInvoke.GdipIsClipEmpty(NativeGraphics, &isEmpty));
return isEmpty;
}
}
public bool IsVisibleClipEmpty
{
get
{
BOOL isEmpty;
CheckStatus(PInvoke.GdipIsVisibleClipEmpty(NativeGraphics, &isEmpty));
return isEmpty;
}
}
public float PageScale
{
get
{
float scale;
CheckStatus(PInvoke.GdipGetPageScale(NativeGraphics, &scale));
return scale;
}
set => CheckStatus(PInvoke.GdipSetPageScale(NativeGraphics, value));
}
public GraphicsUnit PageUnit
{
get
{
Unit unit;
CheckStatus(PInvoke.GdipGetPageUnit(NativeGraphics, &unit));
return (GraphicsUnit)unit;
}
set
{
if (value is < GraphicsUnit.World or > GraphicsUnit.Millimeter)
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(GraphicsUnit));
CheckStatus(PInvoke.GdipSetPageUnit(NativeGraphics, (Unit)value));
}
}
public PixelOffsetMode PixelOffsetMode
{
get
{
GdiPlus.PixelOffsetMode mode;
CheckStatus(PInvoke.GdipGetPixelOffsetMode(NativeGraphics, &mode));
return (PixelOffsetMode)mode;
}
set
{
if (value is < PixelOffsetMode.Invalid or > PixelOffsetMode.Half)
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(PixelOffsetMode));
CheckStatus(PInvoke.GdipSetPixelOffsetMode(NativeGraphics, (GdiPlus.PixelOffsetMode)value));
}
}
public Point RenderingOrigin
{
get
{
int x, y;
CheckStatus(PInvoke.GdipGetRenderingOrigin(NativeGraphics, &x, &y));
return new Point(x, y);
}
set => CheckStatus(PInvoke.GdipSetRenderingOrigin(NativeGraphics, value.X, value.Y));
}
public Drawing2D.SmoothingMode SmoothingMode
{
get
{
GdiPlus.SmoothingMode mode;
CheckStatus(PInvoke.GdipGetSmoothingMode(NativeGraphics, &mode));
return (Drawing2D.SmoothingMode)mode;
}
set
{
if (value is < Drawing2D.SmoothingMode.Invalid or > Drawing2D.SmoothingMode.AntiAlias)
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(Drawing2D.SmoothingMode));
CheckStatus(PInvoke.GdipSetSmoothingMode(NativeGraphics, (GdiPlus.SmoothingMode)value));
}
}
public int TextContrast
{
get
{
uint textContrast;
CheckStatus(PInvoke.GdipGetTextContrast(NativeGraphics, &textContrast));
return (int)textContrast;
}
set => CheckStatus(PInvoke.GdipSetTextContrast(NativeGraphics, (uint)value));
}
/// <summary>
/// Gets or sets the rendering mode for text associated with this <see cref='Graphics'/>.
/// </summary>
public TextRenderingHint TextRenderingHint
{
get
{
GdiPlus.TextRenderingHint hint;
CheckStatus(PInvoke.GdipGetTextRenderingHint(NativeGraphics, &hint));
return (TextRenderingHint)hint;
}
set
{
if (value is < TextRenderingHint.SystemDefault or > TextRenderingHint.ClearTypeGridFit)
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(TextRenderingHint));
CheckStatus(PInvoke.GdipSetTextRenderingHint(NativeGraphics, (GdiPlus.TextRenderingHint)value));
}
}
/// <summary>
/// Gets or sets the world transform for this <see cref='Graphics'/>.
/// </summary>
public Matrix Transform
{
get
{
Matrix matrix = new();
CheckStatus(PInvoke.GdipGetWorldTransform(NativeGraphics, matrix.NativeMatrix));
return matrix;
}
set
{
CheckStatus(PInvoke.GdipSetWorldTransform(NativeGraphics, value.NativeMatrix));
GC.KeepAlive(value);
}
}
/// <summary>
/// Gets or sets the world transform elements for this <see cref="Graphics"/>.
/// </summary>
/// <remarks>
/// <para>
/// This is a more performant alternative to <see cref="Transform"/> that does not need disposal.
/// </para>
/// </remarks>
public Matrix3x2 TransformElements
{
get
{
GdiPlus.Matrix* nativeMatrix;
CheckStatus(PInvoke.GdipCreateMatrix(&nativeMatrix));
try
{
CheckStatus(PInvoke.GdipGetWorldTransform(NativeGraphics, nativeMatrix));
Matrix3x2 matrix = default;
CheckStatus(PInvoke.GdipGetMatrixElements(nativeMatrix, (float*)&matrix));
return matrix;
}
finally
{
if (nativeMatrix is not null)
{
PInvoke.GdipDeleteMatrix(nativeMatrix);
}
}
}
set
{
GdiPlus.Matrix* nativeMatrix = Matrix.CreateNativeHandle(value);
try
{
CheckStatus(PInvoke.GdipSetWorldTransform(NativeGraphics, nativeMatrix));
}
finally
{
if (nativeMatrix is not null)
{
PInvoke.GdipDeleteMatrix(nativeMatrix);
}
}
}
}
HDC IHdcContext.GetHdc() => (HDC)GetHdc();
public IntPtr GetHdc()
{
HDC hdc;
CheckStatus(PInvoke.GdipGetDC(NativeGraphics, &hdc));
// Need to cache the hdc to be able to release with a call to IDeviceContext.ReleaseHdc().
_nativeHdc = hdc;
return _nativeHdc;
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
public void ReleaseHdc(IntPtr hdc) => ReleaseHdcInternal(hdc);
public void ReleaseHdc() => ReleaseHdcInternal(_nativeHdc);
/// <summary>
/// Forces immediate execution of all operations currently on the stack.
/// </summary>
public void Flush() => Flush(Drawing2D.FlushIntention.Flush);
/// <summary>
/// Forces execution of all operations currently on the stack.
/// </summary>
public void Flush(Drawing2D.FlushIntention intention) =>
CheckStatus(PInvoke.GdipFlush(NativeGraphics, (GdiPlus.FlushIntention)intention));
public void SetClip(Graphics g) => SetClip(g, Drawing2D.CombineMode.Replace);
public void SetClip(Graphics g, Drawing2D.CombineMode combineMode)
{
ArgumentNullException.ThrowIfNull(g);
CheckStatus(PInvoke.GdipSetClipGraphics(NativeGraphics, g.NativeGraphics, (GdiPlus.CombineMode)combineMode));
GC.KeepAlive(g);
}
public void SetClip(Rectangle rect) => SetClip(rect, Drawing2D.CombineMode.Replace);
public void SetClip(Rectangle rect, Drawing2D.CombineMode combineMode) => SetClip((RectangleF)rect, combineMode);
public void SetClip(RectangleF rect) => SetClip(rect, Drawing2D.CombineMode.Replace);
public void SetClip(RectangleF rect, Drawing2D.CombineMode combineMode) =>
CheckStatus(PInvoke.GdipSetClipRect(NativeGraphics, rect.X, rect.Y, rect.Width, rect.Height, (GdiPlus.CombineMode)combineMode));
public void SetClip(GraphicsPath path) => SetClip(path, Drawing2D.CombineMode.Replace);
public void SetClip(GraphicsPath path, Drawing2D.CombineMode combineMode)
{
ArgumentNullException.ThrowIfNull(path);
CheckStatus(PInvoke.GdipSetClipPath(NativeGraphics, path._nativePath, (GdiPlus.CombineMode)combineMode));
GC.KeepAlive(path);
}
public void SetClip(Region region, Drawing2D.CombineMode combineMode)
{
ArgumentNullException.ThrowIfNull(region);
CheckStatus(PInvoke.GdipSetClipRegion(NativeGraphics, region.NativeRegion, (GdiPlus.CombineMode)combineMode));
GC.KeepAlive(region);
}
public void IntersectClip(Rectangle rect) => IntersectClip((RectangleF)rect);
public void IntersectClip(RectangleF rect) =>
CheckStatus(PInvoke.GdipSetClipRect(
NativeGraphics,
rect.X, rect.Y, rect.Width, rect.Height,
GdiPlus.CombineMode.CombineModeIntersect));
public void IntersectClip(Region region)
{
ArgumentNullException.ThrowIfNull(region);
CheckStatus(PInvoke.GdipSetClipRegion(NativeGraphics, region.NativeRegion, GdiPlus.CombineMode.CombineModeIntersect));
GC.KeepAlive(region);
}
public void ExcludeClip(Rectangle rect) =>
CheckStatus(PInvoke.GdipSetClipRect(
NativeGraphics,
rect.X, rect.Y, rect.Width, rect.Height,
GdiPlus.CombineMode.CombineModeExclude));
public void ExcludeClip(Region region)
{
ArgumentNullException.ThrowIfNull(region);
CheckStatus(PInvoke.GdipSetClipRegion(NativeGraphics, region.NativeRegion, GdiPlus.CombineMode.CombineModeExclude));
GC.KeepAlive(region);
}
public void ResetClip() => CheckStatus(PInvoke.GdipResetClip(NativeGraphics));
public void TranslateClip(float dx, float dy) => CheckStatus(PInvoke.GdipTranslateClip(NativeGraphics, dx, dy));
public void TranslateClip(int dx, int dy) => CheckStatus(PInvoke.GdipTranslateClip(NativeGraphics, dx, dy));
public bool IsVisible(int x, int y) => IsVisible(new Point(x, y));
public bool IsVisible(Point point) => IsVisible(point.X, point.Y);
public bool IsVisible(float x, float y)
{
BOOL isVisible;
CheckStatus(PInvoke.GdipIsVisiblePoint(NativeGraphics, x, y, &isVisible));
return isVisible;
}
public bool IsVisible(PointF point) => IsVisible(point.X, point.Y);
public bool IsVisible(int x, int y, int width, int height) => IsVisible((float)x, y, width, height);
public bool IsVisible(Rectangle rect) => IsVisible((float)rect.X, rect.Y, rect.Width, rect.Height);
public bool IsVisible(float x, float y, float width, float height)
{
BOOL isVisible;
CheckStatus(PInvoke.GdipIsVisibleRect(NativeGraphics, x, y, width, height, &isVisible));
return isVisible;
}
public bool IsVisible(RectangleF rect) => IsVisible(rect.X, rect.Y, rect.Width, rect.Height);
/// <summary>
/// Resets the world transform to identity.
/// </summary>
public void ResetTransform() => CheckStatus(PInvoke.GdipResetWorldTransform(NativeGraphics));
/// <summary>
/// Multiplies the <see cref='Matrix'/> that represents the world transform and <paramref name="matrix"/>.
/// </summary>
public void MultiplyTransform(Matrix matrix) => MultiplyTransform(matrix, MatrixOrder.Prepend);
/// <summary>
/// Multiplies the <see cref='Matrix'/> that represents the world transform and <paramref name="matrix"/>.
/// </summary>
public void MultiplyTransform(Matrix matrix, MatrixOrder order)
{
ArgumentNullException.ThrowIfNull(matrix);
CheckStatus(PInvoke.GdipMultiplyWorldTransform(NativeGraphics, matrix.NativeMatrix, (GdiPlus.MatrixOrder)order));
GC.KeepAlive(matrix);
}
public void TranslateTransform(float dx, float dy) => TranslateTransform(dx, dy, MatrixOrder.Prepend);
public void TranslateTransform(float dx, float dy, MatrixOrder order) =>
CheckStatus(PInvoke.GdipTranslateWorldTransform(NativeGraphics, dx, dy, (GdiPlus.MatrixOrder)order));
public void ScaleTransform(float sx, float sy) => ScaleTransform(sx, sy, MatrixOrder.Prepend);
public void ScaleTransform(float sx, float sy, MatrixOrder order) =>
CheckStatus(PInvoke.GdipScaleWorldTransform(NativeGraphics, sx, sy, (GdiPlus.MatrixOrder)order));
public void RotateTransform(float angle) => RotateTransform(angle, MatrixOrder.Prepend);
public void RotateTransform(float angle, MatrixOrder order) =>
CheckStatus(PInvoke.GdipRotateWorldTransform(NativeGraphics, angle, (GdiPlus.MatrixOrder)order));
/// <summary>
/// Draws an arc from the specified ellipse.
/// </summary>
public void DrawArc(Pen pen, float x, float y, float width, float height, float startAngle, float sweepAngle)
{
ArgumentNullException.ThrowIfNull(pen);
CheckErrorStatus(PInvoke.GdipDrawArc(
NativeGraphics,
pen.NativePen,
x, y, width, height,
startAngle,
sweepAngle));
GC.KeepAlive(pen);
}
/// <summary>
/// Draws an arc from the specified ellipse.
/// </summary>
public void DrawArc(Pen pen, RectangleF rect, float startAngle, float sweepAngle) =>
DrawArc(pen, rect.X, rect.Y, rect.Width, rect.Height, startAngle, sweepAngle);
/// <summary>
/// Draws an arc from the specified ellipse.
/// </summary>
public void DrawArc(Pen pen, int x, int y, int width, int height, int startAngle, int sweepAngle)
=> DrawArc(pen, (float)x, y, width, height, startAngle, sweepAngle);
/// <summary>
/// Draws an arc from the specified ellipse.
/// </summary>
public void DrawArc(Pen pen, Rectangle rect, float startAngle, float sweepAngle) =>
DrawArc(pen, rect.X, rect.Y, rect.Width, rect.Height, startAngle, sweepAngle);
/// <summary>
/// Draws a cubic Bezier curve defined by four ordered pairs that represent points.
/// </summary>
public void DrawBezier(Pen pen, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)
{
ArgumentNullException.ThrowIfNull(pen);
CheckErrorStatus(PInvoke.GdipDrawBezier(
NativeGraphics,
pen.NativePen,
x1, y1, x2, y2, x3, y3, x4, y4));
GC.KeepAlive(pen);
}
/// <summary>
/// Draws a cubic Bezier curve defined by four points.
/// </summary>
public void DrawBezier(Pen pen, PointF pt1, PointF pt2, PointF pt3, PointF pt4) =>
DrawBezier(pen, pt1.X, pt1.Y, pt2.X, pt2.Y, pt3.X, pt3.Y, pt4.X, pt4.Y);
/// <summary>
/// Draws a cubic Bezier curve defined by four points.
/// </summary>
public void DrawBezier(Pen pen, Point pt1, Point pt2, Point pt3, Point pt4) =>
DrawBezier(pen, pt1.X, pt1.Y, pt2.X, pt2.Y, pt3.X, pt3.Y, pt4.X, pt4.Y);
/// <summary>
/// Draws the outline of a rectangle specified by <paramref name="rect"/>.
/// </summary>
/// <param name="pen">A Pen that determines the color, width, and style of the rectangle.</param>
/// <param name="rect">A Rectangle structure that represents the rectangle to draw.</param>
public void DrawRectangle(Pen pen, RectangleF rect) => DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height);
/// <summary>
/// Draws the outline of a rectangle specified by <paramref name="rect"/>.
/// </summary>
public void DrawRectangle(Pen pen, Rectangle rect) => DrawRectangle(pen, rect.X, rect.Y, rect.Width, rect.Height);
#if NET9_0_OR_GREATER
/// <inheritdoc cref="DrawRoundedRectangle(Pen, RectangleF, SizeF)"/>
public void DrawRoundedRectangle(Pen pen, Rectangle rect, Size radius) =>
DrawRoundedRectangle(pen, (RectangleF)rect, radius);
/// <summary>
/// Draws the outline of the specified rounded rectangle.
/// </summary>
/// <param name="pen">The <see cref="Pen"/> to draw the outline with.</param>
/// <param name="rect">The bounds of the rounded rectangle.</param>
/// <param name="radius">The radius width and height used to round the corners of the rectangle.</param>
public void DrawRoundedRectangle(Pen pen, RectangleF rect, SizeF radius)
{
using GraphicsPath path = new();
path.AddRoundedRectangle(rect, radius);
DrawPath(pen, path);
}
#endif
/// <summary>
/// Draws the outline of the specified rectangle.
/// </summary>
public void DrawRectangle(Pen pen, float x, float y, float width, float height)
{
ArgumentNullException.ThrowIfNull(pen);
CheckErrorStatus(PInvoke.GdipDrawRectangle(NativeGraphics, pen.NativePen, x, y, width, height));
GC.KeepAlive(pen);
}
/// <summary>
/// Draws the outline of the specified rectangle.
/// </summary>
public void DrawRectangle(Pen pen, int x, int y, int width, int height)
=> DrawRectangle(pen, (float)x, y, width, height);
/// <inheritdoc cref="DrawRectangles(Pen, Rectangle[])"/>
public void DrawRectangles(Pen pen, params RectangleF[] rects) => DrawRectangles(pen, rects.OrThrowIfNull().AsSpan());
/// <inheritdoc cref="DrawRectangles(Pen, Rectangle[])"/>
#if NET9_0_OR_GREATER
public
#else
private
#endif
void DrawRectangles(Pen pen, params ReadOnlySpan<RectangleF> rects)
{
ArgumentNullException.ThrowIfNull(pen);
fixed (RectangleF* r = rects)
{
CheckErrorStatus(PInvoke.GdipDrawRectangles(NativeGraphics, pen.NativePen, (RectF*)r, rects.Length));
}
GC.KeepAlive(pen);
}
/// <summary>
/// Draws the outlines of a series of rectangles.
/// </summary>
/// <param name="pen"><see cref="Pen"/> that determines the color, width, and style of the outlines of the rectangles.</param>
/// <param name="rects">An array of <see cref="Rectangle"/> structures that represents the rectangles to draw.</param>
public void DrawRectangles(Pen pen, params Rectangle[] rects) => DrawRectangles(pen, rects.OrThrowIfNull().AsSpan());
/// <inheritdoc cref="DrawRectangles(Pen, Rectangle[])"/>
#if NET9_0_OR_GREATER
public
#else
private
#endif
void DrawRectangles(Pen pen, params ReadOnlySpan<Rectangle> rects)
{
ArgumentNullException.ThrowIfNull(pen);
fixed (Rectangle* r = rects)
{
CheckErrorStatus(PInvoke.GdipDrawRectanglesI(NativeGraphics, pen.NativePen, (Rect*)r, rects.Length));
}
GC.KeepAlive(pen);
}
/// <summary>
/// Draws the outline of an ellipse defined by a bounding rectangle.
/// </summary>
public void DrawEllipse(Pen pen, RectangleF rect) => DrawEllipse(pen, rect.X, rect.Y, rect.Width, rect.Height);
/// <summary>
/// Draws the outline of an ellipse defined by a bounding rectangle.
/// </summary>
public void DrawEllipse(Pen pen, float x, float y, float width, float height)
{
ArgumentNullException.ThrowIfNull(pen);
CheckErrorStatus(PInvoke.GdipDrawEllipse(NativeGraphics, pen.NativePen, x, y, width, height));
GC.KeepAlive(pen);
}
/// <summary>
/// Draws the outline of an ellipse specified by a bounding rectangle.
/// </summary>
public void DrawEllipse(Pen pen, Rectangle rect) => DrawEllipse(pen, (float)rect.X, rect.Y, rect.Width, rect.Height);
/// <summary>
/// Draws the outline of an ellipse defined by a bounding rectangle.
/// </summary>
public void DrawEllipse(Pen pen, int x, int y, int width, int height) => DrawEllipse(pen, (float)x, y, width, height);
/// <summary>
/// Draws the outline of a pie section defined by an ellipse and two radial lines.
/// </summary>
public void DrawPie(Pen pen, RectangleF rect, float startAngle, float sweepAngle) =>
DrawPie(pen, rect.X, rect.Y, rect.Width, rect.Height, startAngle, sweepAngle);
/// <summary>
/// Draws the outline of a pie section defined by an ellipse and two radial lines.
/// </summary>
public void DrawPie(Pen pen, float x, float y, float width, float height, float startAngle, float sweepAngle)
{
ArgumentNullException.ThrowIfNull(pen);
CheckErrorStatus(PInvoke.GdipDrawPie(NativeGraphics, pen.NativePen, x, y, width, height, startAngle, sweepAngle));
GC.KeepAlive(pen);
}
/// <summary>
/// Draws the outline of a pie section defined by an ellipse and two radial lines.
/// </summary>
public void DrawPie(Pen pen, Rectangle rect, float startAngle, float sweepAngle) =>
DrawPie(pen, rect.X, rect.Y, rect.Width, rect.Height, startAngle, sweepAngle);
/// <summary>
/// Draws the outline of a pie section defined by an ellipse and two radial lines.
/// </summary>
public void DrawPie(Pen pen, int x, int y, int width, int height, int startAngle, int sweepAngle) =>
DrawPie(pen, (float)x, y, width, height, startAngle, sweepAngle);
/// <inheritdoc cref="DrawPolygon(Pen, Point[])"/>
public void DrawPolygon(Pen pen, params PointF[] points) => DrawPolygon(pen, points.OrThrowIfNull().AsSpan());
/// <inheritdoc cref="DrawPolygon(Pen, Point[])"/>
#if NET9_0_OR_GREATER
public
#else
private
#endif
void DrawPolygon(Pen pen, params ReadOnlySpan<PointF> points)
{
ArgumentNullException.ThrowIfNull(pen);
fixed (PointF* p = points)
{
CheckErrorStatus(PInvoke.GdipDrawPolygon(NativeGraphics, pen.NativePen, (GdiPlus.PointF*)p, points.Length));
}
GC.KeepAlive(pen);
}
/// <summary>
/// Draws the outline of a polygon defined by an array of points.
/// </summary>
/// <param name="pen">The <see cref="Pen"/> to draw the outline with.</param>
/// <param name="points">An array of <see cref="Point"/> structures that represent the vertices of the polygon.</param>
public void DrawPolygon(Pen pen, params Point[] points) => DrawPolygon(pen, points.OrThrowIfNull().AsSpan());
/// <inheritdoc cref="DrawPolygon(Pen, Point[])"/>
#if NET9_0_OR_GREATER
public
#else
private
#endif
void DrawPolygon(Pen pen, params ReadOnlySpan<Point> points)
{
ArgumentNullException.ThrowIfNull(pen);
fixed (Point* p = points)
{
CheckErrorStatus(PInvoke.GdipDrawPolygonI(NativeGraphics, pen.NativePen, (GdiPlus.Point*)p, points.Length));
}
GC.KeepAlive(pen);
}
/// <summary>
/// Draws the lines and curves defined by a <see cref='GraphicsPath'/>.
/// </summary>
public void DrawPath(Pen pen, GraphicsPath path)
{
ArgumentNullException.ThrowIfNull(pen);
ArgumentNullException.ThrowIfNull(path);
CheckErrorStatus(PInvoke.GdipDrawPath(NativeGraphics, pen.NativePen, path._nativePath));
GC.KeepAlive(pen);
GC.KeepAlive(path);
}
/// <inheritdoc cref="DrawCurve(Pen, Point[], int, int, float)"/>
public void DrawCurve(Pen pen, params PointF[] points) => DrawCurve(pen, points.OrThrowIfNull().AsSpan());
/// <inheritdoc cref="DrawCurve(Pen, Point[], int, int, float)"/>
#if NET9_0_OR_GREATER
public
#else
private
#endif
void DrawCurve(Pen pen, params ReadOnlySpan<PointF> points)
{
ArgumentNullException.ThrowIfNull(pen);
fixed (PointF* p = points)
{
CheckErrorStatus(PInvoke.GdipDrawCurve(NativeGraphics, pen.NativePen, (GdiPlus.PointF*)p, points.Length));
}
GC.KeepAlive(pen);
}
/// <inheritdoc cref="DrawCurve(Pen, Point[], int, int, float)"/>
public void DrawCurve(Pen pen, PointF[] points, float tension) =>
DrawCurve(pen, points.OrThrowIfNull().AsSpan(), tension);
/// <inheritdoc cref="DrawCurve(Pen, Point[], int, int, float)"/>
#if NET9_0_OR_GREATER
public
#else
private
#endif
void DrawCurve(Pen pen, ReadOnlySpan<PointF> points, float tension)
{
ArgumentNullException.ThrowIfNull(pen);
fixed (PointF* p = points)
{
CheckErrorStatus(PInvoke.GdipDrawCurve2(
NativeGraphics,
pen.NativePen,
(GdiPlus.PointF*)p, points.Length,
tension));
}
GC.KeepAlive(pen);
}
/// <inheritdoc cref="DrawCurve(Pen, Point[], int, int, float)"/>
public void DrawCurve(Pen pen, PointF[] points, int offset, int numberOfSegments) =>
DrawCurve(pen, points, offset, numberOfSegments, 0.5f);
#if NET9_0_OR_GREATER
/// <inheritdoc cref="DrawCurve(Pen, Point[], int, int, float)"/>
public void DrawCurve(Pen pen, ReadOnlySpan<PointF> points, int offset, int numberOfSegments) =>
DrawCurve(pen, points, offset, numberOfSegments, 0.5f);
#endif
/// <inheritdoc cref="DrawCurve(Pen, Point[], int, int, float)"/>
public void DrawCurve(Pen pen, PointF[] points, int offset, int numberOfSegments, float tension) =>
DrawCurve(pen, points.OrThrowIfNull().AsSpan(), offset, numberOfSegments, tension);
/// <inheritdoc cref="DrawCurve(Pen, Point[], int, int, float)"/>
#if NET9_0_OR_GREATER
public
#else
private
#endif
void DrawCurve(Pen pen, ReadOnlySpan<PointF> points, int offset, int numberOfSegments, float tension)
{
ArgumentNullException.ThrowIfNull(pen);
fixed (PointF* p = points)
{
CheckErrorStatus(PInvoke.GdipDrawCurve3(
NativeGraphics,
pen.NativePen,
(GdiPlus.PointF*)p, points.Length,
offset,
numberOfSegments,
tension));
}
GC.KeepAlive(pen);
}
/// <inheritdoc cref="DrawCurve(Pen, Point[], int, int, float)"/>