-
Notifications
You must be signed in to change notification settings - Fork 957
/
Graphics.cs
3885 lines (3383 loc) · 134 KB
/
Graphics.cs
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;
using System.Drawing.Internal;
using System.Drawing.Text;
using System.Globalization;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
#if NET7_0_OR_GREATER
using System.Runtime.InteropServices.Marshalling;
#endif
using Gdip = System.Drawing.SafeNativeMethods.Gdip;
using static Interop;
namespace System.Drawing;
/// <summary>
/// Encapsulates a GDI+ drawing surface.
/// </summary>
public sealed class Graphics : MarshalByRefObject, IDisposable, IDeviceContext
{
#if FINALIZATION_WATCH
static readonly TraceSwitch GraphicsFinalization = new TraceSwitch("GraphicsFinalization", "Tracks the creation and destruction of finalization");
internal static string GetAllocationStack() {
if (GraphicsFinalization.TraceVerbose) {
return Environment.StackTrace;
}
else {
return "Enabled 'GraphicsFinalization' switch to see stack of allocation";
}
}
private string allocationSite = Graphics.GetAllocationStack();
#endif
/// <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 IntPtr 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 IntPtr _nativeHdc;
public delegate bool DrawImageAbort(IntPtr callbackdata);
#if NET7_0_OR_GREATER
[CustomMarshaller(typeof(DrawImageAbort), MarshalMode.ManagedToUnmanagedIn, typeof(KeepAliveMarshaller))]
internal static class DrawImageAbortMarshaller
{
internal unsafe struct KeepAliveMarshaller
{
private delegate BOOL DrawImageAbortNative(IntPtr callbackdata);
private DrawImageAbortNative? _managed;
private delegate* unmanaged<IntPtr, BOOL> _nativeFunction;
public void FromManaged(DrawImageAbort? managed)
{
_managed = managed is null ? null : data => managed(data) ? BOOL.TRUE : BOOL.FALSE;
_nativeFunction = _managed is null ? null : (delegate* unmanaged<IntPtr, BOOL>)Marshal.GetFunctionPointerForDelegate(_managed);
}
public delegate* unmanaged<IntPtr, BOOL> ToUnmanaged()
{
return _nativeFunction;
}
public void OnInvoked()
{
GC.KeepAlive(_managed);
}
public void Free() { }
}
}
#endif
/// <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);
#if NET7_0_OR_GREATER
[CustomMarshaller(typeof(EnumerateMetafileProc), MarshalMode.ManagedToUnmanagedIn, typeof(KeepAliveMarshaller))]
internal static class EnumerateMetafileProcMarshaller
{
internal unsafe struct KeepAliveMarshaller
{
private delegate BOOL EnumerateMetafileProcNative(
EmfPlusRecordType recordType,
int flags,
int dataSize,
IntPtr data,
IntPtr callbackData);
private EnumerateMetafileProcNative? _managed;
private delegate* unmanaged<IntPtr, BOOL> _nativeFunction;
public void FromManaged(EnumerateMetafileProc? managed)
{
_managed = managed is null ? null : (recordType, flags, dataSize, data, callbackData) =>
managed(recordType, flags, dataSize, data, callbackData == IntPtr.Zero ? null : Marshal.GetDelegateForFunctionPointer<PlayRecordCallback>(callbackData)) ? BOOL.TRUE : BOOL.FALSE;
_nativeFunction = _managed is null ? null : (delegate* unmanaged<IntPtr, BOOL>)Marshal.GetFunctionPointerForDelegate(_managed);
}
public delegate* unmanaged<IntPtr, BOOL> ToUnmanaged()
{
return _nativeFunction;
}
public void OnInvoked()
{
GC.KeepAlive(_managed);
}
public void Free() {}
}
}
#endif
/// <summary>
/// Constructor to initialize this object from a native GDI+ Graphics pointer.
/// </summary>
private Graphics(IntPtr gdipNativeGraphics)
{
if (gdipNativeGraphics == IntPtr.Zero)
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)
{
Gdip.CheckStatus(Gdip.GdipCreateFromHDC(hdc, out IntPtr 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)
{
Gdip.CheckStatus(Gdip.GdipCreateFromHDC2(hdc, hdevice, out IntPtr 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)
{
Gdip.CheckStatus(Gdip.GdipCreateFromHWND(hwnd, out IntPtr 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));
Gdip.CheckStatus(Gdip.GdipGetImageGraphicsContext(
new HandleRef(image, image._nativeImage),
out IntPtr nativeGraphics));
return new Graphics(nativeGraphics) { _backingImage = image };
}
[EditorBrowsable(EditorBrowsableState.Never)]
public void ReleaseHdcInternal(IntPtr hdc)
{
Gdip.CheckStatus(!Gdip.Initialized ? Gdip.Ok :
Gdip.GdipReleaseDC(new HandleRef(this, NativeGraphics), hdc));
_nativeHdc = IntPtr.Zero;
}
/// <summary>
/// Deletes this <see cref='Graphics'/>, and frees the memory allocated for it.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
#if FINALIZATION_WATCH
Debug.WriteLineIf(!disposing && NativeGraphics != IntPtr.Zero, $"""
System.Drawing.Graphics: ***************************************************
System.Drawing.Graphics: Object Disposed through finalization:
{allocationSite}
""");
#endif
while (_previousContext != null)
{
// Dispose entire stack.
GraphicsContext? context = _previousContext.Previous;
_previousContext.Dispose();
_previousContext = context;
}
if (NativeGraphics != IntPtr.Zero)
{
try
{
if (_nativeHdc != IntPtr.Zero) // avoid a handle leak.
{
ReleaseHdc();
}
if (PrintingHelper is DeviceContext printerDC)
{
printerDC.Dispose();
_printingHelper = null;
}
#if DEBUG
int status = !Gdip.Initialized ? Gdip.Ok :
#endif
Gdip.GdipDeleteGraphics(new HandleRef(this, NativeGraphics));
#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
{
NativeGraphics = IntPtr.Zero;
}
}
}
~Graphics() => Dispose(false);
/// <summary>
/// Handle to native GDI+ graphics object. This object is created on demand.
/// </summary>
internal IntPtr NativeGraphics { get; private set; }
public Region Clip
{
get
{
var region = new Region();
int status = Gdip.GdipGetClip(new HandleRef(this, NativeGraphics), new HandleRef(region, region.NativeRegion));
Gdip.CheckStatus(status);
return region;
}
set => SetClip(value, CombineMode.Replace);
}
public RectangleF ClipBounds
{
get
{
Gdip.CheckStatus(Gdip.GdipGetClipBounds(new HandleRef(this, NativeGraphics), out RectangleF rect));
return rect;
}
}
/// <summary>
/// Gets or sets the <see cref='Drawing2D.CompositingMode'/> associated with this <see cref='Graphics'/>.
/// </summary>
public CompositingMode CompositingMode
{
get
{
Gdip.CheckStatus(Gdip.GdipGetCompositingMode(new HandleRef(this, NativeGraphics), out CompositingMode mode));
return mode;
}
set
{
if (value < CompositingMode.SourceOver || value > CompositingMode.SourceCopy)
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(CompositingMode));
Gdip.CheckStatus(Gdip.GdipSetCompositingMode(new HandleRef(this, NativeGraphics), value));
}
}
public CompositingQuality CompositingQuality
{
get
{
Gdip.CheckStatus(Gdip.GdipGetCompositingQuality(new HandleRef(this, NativeGraphics), out CompositingQuality cq));
return cq;
}
set
{
if (value < CompositingQuality.Invalid || value > CompositingQuality.AssumeLinear)
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(CompositingQuality));
Gdip.CheckStatus(Gdip.GdipSetCompositingQuality(new HandleRef(this, NativeGraphics), value));
}
}
public float DpiX
{
get
{
Gdip.CheckStatus(Gdip.GdipGetDpiX(new HandleRef(this, NativeGraphics), out float dpi));
return dpi;
}
}
public float DpiY
{
get
{
Gdip.CheckStatus(Gdip.GdipGetDpiY(new HandleRef(this, NativeGraphics), out float dpi));
return dpi;
}
}
/// <summary>
/// Gets or sets the interpolation mode associated with this Graphics.
/// </summary>
public InterpolationMode InterpolationMode
{
get
{
Gdip.CheckStatus(Gdip.GdipGetInterpolationMode(new HandleRef(this, NativeGraphics), out InterpolationMode mode));
return mode;
}
set
{
if (value < InterpolationMode.Invalid || value > InterpolationMode.HighQualityBicubic)
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(InterpolationMode));
// GDI+ interprets the value of InterpolationMode and sets a value accordingly.
// Libgdiplus does not, so do this manually here.
switch (value)
{
case InterpolationMode.Default:
case InterpolationMode.Low:
value = InterpolationMode.Bilinear;
break;
case InterpolationMode.High:
value = InterpolationMode.HighQualityBicubic;
break;
case InterpolationMode.Invalid:
throw new ArgumentException(SR.GdiplusInvalidParameter);
default:
break;
}
Gdip.CheckStatus(Gdip.GdipSetInterpolationMode(new HandleRef(this, NativeGraphics), value));
}
}
public bool IsClipEmpty
{
get
{
Gdip.CheckStatus(Gdip.GdipIsClipEmpty(new HandleRef(this, NativeGraphics), out bool isEmpty));
return isEmpty;
}
}
public bool IsVisibleClipEmpty
{
get
{
Gdip.CheckStatus(Gdip.GdipIsVisibleClipEmpty(new HandleRef(this, NativeGraphics), out bool isEmpty));
return isEmpty;
}
}
public float PageScale
{
get
{
Gdip.CheckStatus(Gdip.GdipGetPageScale(new HandleRef(this, NativeGraphics), out float scale));
return scale;
}
set
{
// Libgdiplus doesn't perform argument validation, so do this here for compatibility.
if (value <= 0 || value > 1000000032)
throw new ArgumentException(SR.GdiplusInvalidParameter);
Gdip.CheckStatus(Gdip.GdipSetPageScale(new HandleRef(this, NativeGraphics), value));
}
}
public GraphicsUnit PageUnit
{
get
{
Gdip.CheckStatus(Gdip.GdipGetPageUnit(new HandleRef(this, NativeGraphics), out GraphicsUnit unit));
return unit;
}
set
{
if (value < GraphicsUnit.World || value > GraphicsUnit.Millimeter)
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(GraphicsUnit));
// GDI+ doesn't allow GraphicsUnit.World as a valid value for PageUnit.
// Libgdiplus doesn't perform argument validation, so do this here.
if (value == GraphicsUnit.World)
throw new ArgumentException(SR.GdiplusInvalidParameter);
Gdip.CheckStatus(Gdip.GdipSetPageUnit(new HandleRef(this, NativeGraphics), value));
}
}
public PixelOffsetMode PixelOffsetMode
{
get
{
Gdip.CheckStatus(Gdip.GdipGetPixelOffsetMode(new HandleRef(this, NativeGraphics), out PixelOffsetMode mode));
return mode;
}
set
{
if (value < PixelOffsetMode.Invalid || value > PixelOffsetMode.Half)
throw new InvalidEnumArgumentException(nameof(value), unchecked((int)value), typeof(PixelOffsetMode));
// GDI+ doesn't allow PixelOffsetMode.Invalid as a valid value for PixelOffsetMode.
// Libgdiplus doesn't perform argument validation, so do this here.
if (value == PixelOffsetMode.Invalid)
throw new ArgumentException(SR.GdiplusInvalidParameter);
Gdip.CheckStatus(Gdip.GdipSetPixelOffsetMode(new HandleRef(this, NativeGraphics), value));
}
}
public Point RenderingOrigin
{
get
{
Gdip.CheckStatus(Gdip.GdipGetRenderingOrigin(new HandleRef(this, NativeGraphics), out int x, out int y));
return new Point(x, y);
}
set
{
Gdip.CheckStatus(Gdip.GdipSetRenderingOrigin(new HandleRef(this, NativeGraphics), value.X, value.Y));
}
}
public SmoothingMode SmoothingMode
{
get
{
Gdip.CheckStatus(Gdip.GdipGetSmoothingMode(new HandleRef(this, NativeGraphics), out SmoothingMode mode));
return mode;
}
set
{
if (value < SmoothingMode.Invalid || value > SmoothingMode.AntiAlias)
throw new InvalidEnumArgumentException(nameof(value), unchecked((int)value), typeof(SmoothingMode));
// GDI+ interprets the value of SmoothingMode and sets a value accordingly.
// Libgdiplus does not, so do this manually here.
switch (value)
{
case SmoothingMode.Default:
case SmoothingMode.HighSpeed:
value = SmoothingMode.None;
break;
case SmoothingMode.HighQuality:
value = SmoothingMode.AntiAlias;
break;
case SmoothingMode.Invalid:
throw new ArgumentException(SR.GdiplusInvalidParameter);
default:
break;
}
Gdip.CheckStatus(Gdip.GdipSetSmoothingMode(new HandleRef(this, NativeGraphics), value));
}
}
public int TextContrast
{
get
{
Gdip.CheckStatus(Gdip.GdipGetTextContrast(new HandleRef(this, NativeGraphics), out int textContrast));
return textContrast;
}
set
{
Gdip.CheckStatus(Gdip.GdipSetTextContrast(new HandleRef(this, NativeGraphics), value));
}
}
/// <summary>
/// Gets or sets the rendering mode for text associated with this <see cref='Graphics'/>.
/// </summary>
public TextRenderingHint TextRenderingHint
{
get
{
Gdip.CheckStatus(Gdip.GdipGetTextRenderingHint(new HandleRef(this, NativeGraphics), out TextRenderingHint hint));
return hint;
}
set
{
if (value < TextRenderingHint.SystemDefault || value > TextRenderingHint.ClearTypeGridFit)
throw new InvalidEnumArgumentException(nameof(value), unchecked((int)value), typeof(TextRenderingHint));
Gdip.CheckStatus(Gdip.GdipSetTextRenderingHint(new HandleRef(this, NativeGraphics), value));
}
}
/// <summary>
/// Gets or sets the world transform for this <see cref='Graphics'/>.
/// </summary>
public Matrix Transform
{
get
{
var matrix = new Matrix();
Gdip.CheckStatus(Gdip.GdipGetWorldTransform(
new HandleRef(this, NativeGraphics), new HandleRef(matrix, matrix.NativeMatrix)));
return matrix;
}
set
{
Gdip.CheckStatus(Gdip.GdipSetWorldTransform(
new HandleRef(this, NativeGraphics), new HandleRef(value, value.NativeMatrix)));
}
}
/// <summary>
/// Gets or sets the world transform elements for this <see cref="Graphics"/>.
/// </summary>
/// <remarks>
/// This is a more performant alternative to <see cref="Transform"/> that does not need disposal.
/// </remarks>
public unsafe Matrix3x2 TransformElements
{
get
{
Gdip.CheckStatus(Gdip.GdipCreateMatrix(out IntPtr nativeMatrix));
try
{
Gdip.CheckStatus(Gdip.GdipGetWorldTransform(
new HandleRef(this, NativeGraphics), new HandleRef(null, nativeMatrix)));
Matrix3x2 matrix = default;
Gdip.CheckStatus(Gdip.GdipGetMatrixElements(new HandleRef(null, nativeMatrix), (float*)&matrix));
return matrix;
}
finally
{
if (nativeMatrix != IntPtr.Zero)
{
Gdip.GdipDeleteMatrix(new HandleRef(null, nativeMatrix));
}
}
}
set
{
IntPtr nativeMatrix = Matrix.CreateNativeHandle(value);
try
{
Gdip.CheckStatus(Gdip.GdipSetWorldTransform(
new HandleRef(this, NativeGraphics), new HandleRef(null, nativeMatrix)));
}
finally
{
if (nativeMatrix != IntPtr.Zero)
{
Gdip.GdipDeleteMatrix(new HandleRef(null, nativeMatrix));
}
}
}
}
public IntPtr GetHdc()
{
IntPtr hdc;
Gdip.CheckStatus(Gdip.GdipGetDC(new HandleRef(this, NativeGraphics), out hdc));
_nativeHdc = hdc; // need to cache the hdc to be able to release with a call to IDeviceContext.ReleaseHdc().
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(FlushIntention.Flush);
/// <summary>
/// Forces execution of all operations currently on the stack.
/// </summary>
public void Flush(FlushIntention intention)
{
Gdip.CheckStatus(Gdip.GdipFlush(new HandleRef(this, NativeGraphics), intention));
}
public void SetClip(Graphics g) => SetClip(g, CombineMode.Replace);
public void SetClip(Graphics g, CombineMode combineMode)
{
ArgumentNullException.ThrowIfNull(g);
Gdip.CheckStatus(Gdip.GdipSetClipGraphics(
new HandleRef(this, NativeGraphics),
new HandleRef(g, g.NativeGraphics),
combineMode));
}
public void SetClip(Rectangle rect) => SetClip(rect, CombineMode.Replace);
public void SetClip(Rectangle rect, CombineMode combineMode)
{
Gdip.CheckStatus(Gdip.GdipSetClipRectI(
new HandleRef(this, NativeGraphics),
rect.X, rect.Y, rect.Width, rect.Height,
combineMode));
}
public void SetClip(RectangleF rect) => SetClip(rect, CombineMode.Replace);
public void SetClip(RectangleF rect, CombineMode combineMode)
{
Gdip.CheckStatus(Gdip.GdipSetClipRect(
new HandleRef(this, NativeGraphics),
rect.X, rect.Y, rect.Width, rect.Height,
combineMode));
}
public void SetClip(GraphicsPath path) => SetClip(path, CombineMode.Replace);
public void SetClip(GraphicsPath path, CombineMode combineMode)
{
ArgumentNullException.ThrowIfNull(path);
Gdip.CheckStatus(Gdip.GdipSetClipPath(
new HandleRef(this, NativeGraphics),
new HandleRef(path, path._nativePath),
combineMode));
}
public void SetClip(Region region, CombineMode combineMode)
{
ArgumentNullException.ThrowIfNull(region);
Gdip.CheckStatus(Gdip.GdipSetClipRegion(
new HandleRef(this, NativeGraphics),
new HandleRef(region, region.NativeRegion),
combineMode));
}
public void IntersectClip(Rectangle rect)
{
Gdip.CheckStatus(Gdip.GdipSetClipRectI(
new HandleRef(this, NativeGraphics),
rect.X, rect.Y, rect.Width, rect.Height,
CombineMode.Intersect));
}
public void IntersectClip(RectangleF rect)
{
Gdip.CheckStatus(Gdip.GdipSetClipRect(
new HandleRef(this, NativeGraphics),
rect.X, rect.Y, rect.Width, rect.Height,
CombineMode.Intersect));
}
public void IntersectClip(Region region)
{
ArgumentNullException.ThrowIfNull(region);
Gdip.CheckStatus(Gdip.GdipSetClipRegion(
new HandleRef(this, NativeGraphics),
new HandleRef(region, region.NativeRegion),
CombineMode.Intersect));
}
public void ExcludeClip(Rectangle rect)
{
Gdip.CheckStatus(Gdip.GdipSetClipRectI(
new HandleRef(this, NativeGraphics),
rect.X, rect.Y, rect.Width, rect.Height,
CombineMode.Exclude));
}
public void ExcludeClip(Region region)
{
ArgumentNullException.ThrowIfNull(region);
Gdip.CheckStatus(Gdip.GdipSetClipRegion(
new HandleRef(this, NativeGraphics),
new HandleRef(region, region.NativeRegion),
CombineMode.Exclude));
}
public void ResetClip()
{
Gdip.CheckStatus(Gdip.GdipResetClip(new HandleRef(this, NativeGraphics)));
}
public void TranslateClip(float dx, float dy)
{
Gdip.CheckStatus(Gdip.GdipTranslateClip(new HandleRef(this, NativeGraphics), dx, dy));
}
public void TranslateClip(int dx, int dy)
{
Gdip.CheckStatus(Gdip.GdipTranslateClip(new HandleRef(this, NativeGraphics), dx, dy));
}
public bool IsVisible(int x, int y) => IsVisible(new Point(x, y));
public bool IsVisible(Point point)
{
Gdip.CheckStatus(Gdip.GdipIsVisiblePointI(
new HandleRef(this, NativeGraphics),
point.X, point.Y,
out bool isVisible));
return isVisible;
}
public bool IsVisible(float x, float y) => IsVisible(new PointF(x, y));
public bool IsVisible(PointF point)
{
Gdip.CheckStatus(Gdip.GdipIsVisiblePoint(
new HandleRef(this, NativeGraphics),
point.X, point.Y,
out bool isVisible));
return isVisible;
}
public bool IsVisible(int x, int y, int width, int height)
{
return IsVisible(new Rectangle(x, y, width, height));
}
public bool IsVisible(Rectangle rect)
{
Gdip.CheckStatus(Gdip.GdipIsVisibleRectI(
new HandleRef(this, NativeGraphics),
rect.X, rect.Y, rect.Width, rect.Height,
out bool isVisible));
return isVisible;
}
public bool IsVisible(float x, float y, float width, float height)
{
return IsVisible(new RectangleF(x, y, width, height));
}
public bool IsVisible(RectangleF rect)
{
Gdip.CheckStatus(Gdip.GdipIsVisibleRect(
new HandleRef(this, NativeGraphics),
rect.X, rect.Y, rect.Width, rect.Height,
out bool isVisible));
return isVisible;
}
/// <summary>
/// Resets the world transform to identity.
/// </summary>
public void ResetTransform()
{
Gdip.CheckStatus(Gdip.GdipResetWorldTransform(new HandleRef(this, 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);
// Multiplying the transform by a disposed matrix is a nop in GDI+, but throws
// with the libgdiplus backend. Simulate a nop for compatibility with GDI+.
if (matrix.NativeMatrix == IntPtr.Zero)
return;
Gdip.CheckStatus(Gdip.GdipMultiplyWorldTransform(
new HandleRef(this, NativeGraphics), new HandleRef(matrix, matrix.NativeMatrix), order));
}
public void TranslateTransform(float dx, float dy) => TranslateTransform(dx, dy, MatrixOrder.Prepend);
public void TranslateTransform(float dx, float dy, MatrixOrder order)
{
Gdip.CheckStatus(Gdip.GdipTranslateWorldTransform(new HandleRef(this, NativeGraphics), dx, dy, order));
}
public void ScaleTransform(float sx, float sy) => ScaleTransform(sx, sy, MatrixOrder.Prepend);
public void ScaleTransform(float sx, float sy, MatrixOrder order)
{
Gdip.CheckStatus(Gdip.GdipScaleWorldTransform(new HandleRef(this, NativeGraphics), sx, sy, order));
}
public void RotateTransform(float angle) => RotateTransform(angle, MatrixOrder.Prepend);
public void RotateTransform(float angle, MatrixOrder order)
{
Gdip.CheckStatus(Gdip.GdipRotateWorldTransform(new HandleRef(this, NativeGraphics), angle, 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(Gdip.GdipDrawArc(
new HandleRef(this, NativeGraphics),
new HandleRef(pen, pen.NativePen),
x, y, width, height,
startAngle,
sweepAngle));
}
/// <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)
{
ArgumentNullException.ThrowIfNull(pen);
CheckErrorStatus(Gdip.GdipDrawArcI(
new HandleRef(this, NativeGraphics),
new HandleRef(pen, pen.NativePen),
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(Gdip.GdipDrawBezier(
new HandleRef(this, NativeGraphics), new HandleRef(pen, pen.NativePen),
x1, y1, x2, y2, x3, y3, x4, y4));
}
/// <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);
}
/// <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(Gdip.GdipDrawRectangle(
new HandleRef(this, NativeGraphics), new HandleRef(pen, pen.NativePen),
x, y, width, height));
}
/// <summary>
/// Draws the outline of the specified rectangle.
/// </summary>
public void DrawRectangle(Pen pen, int x, int y, int width, int height)
{
ArgumentNullException.ThrowIfNull(pen);
CheckErrorStatus(Gdip.GdipDrawRectangleI(
new HandleRef(this, NativeGraphics), new HandleRef(pen, pen.NativePen),
x, y, width, height));
}
/// <summary>
/// Draws the outlines of a series of rectangles.
/// </summary>
public unsafe void DrawRectangles(Pen pen, RectangleF[] rects)
{
ArgumentNullException.ThrowIfNull(pen);
ArgumentNullException.ThrowIfNull(rects);
fixed (RectangleF* r = rects)
{
CheckErrorStatus(Gdip.GdipDrawRectangles(
new HandleRef(this, NativeGraphics), new HandleRef(pen, pen.NativePen),