From 7eeb1dec3ca8cceec8bdd90b13dfd33210c27968 Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Tue, 19 Apr 2022 15:30:20 +0300 Subject: [PATCH] Remove `!!` from System.Drawing.Common. --- .../src/System/Drawing/Bitmap.cs | 17 +- .../System/Drawing/Drawing2D/GraphicsPath.cs | 112 ++++-- .../Drawing/Drawing2D/LinearGradientBrush.cs | 4 +- .../src/System/Drawing/Drawing2D/Matrix.cs | 28 +- .../Drawing/Drawing2D/PathGradientBrush.cs | 15 +- .../src/System/Drawing/Font.cs | 20 +- .../src/System/Drawing/FontConverter.cs | 4 +- .../src/System/Drawing/FontFamily.cs | 4 +- .../src/System/Drawing/Graphics.cs | 363 ++++++++++++++---- .../src/System/Drawing/Icon.cs | 15 +- .../src/System/Drawing/Image.cs | 25 +- .../src/System/Drawing/Imaging/Metafile.cs | 4 +- .../src/System/Drawing/Internal/GPStream.cs | 7 +- .../src/System/Drawing/Pen.cs | 4 +- .../Drawing/Printing/MarginsConverter.cs | 8 +- .../src/System/Drawing/Region.cs | 77 +++- .../src/System/Drawing/StringFormat.cs | 4 +- .../src/System/Drawing/TextureBrush.cs | 24 +- 18 files changed, 558 insertions(+), 177 deletions(-) diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Bitmap.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Bitmap.cs index 5f66ef769de0d..85a5a8012197c 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Bitmap.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Bitmap.cs @@ -54,8 +54,10 @@ public Bitmap(Stream stream) : this(stream, false) { } - public unsafe Bitmap(Stream stream!!, bool useIcm) + public unsafe Bitmap(Stream stream, bool useIcm) { + ArgumentNullException.ThrowIfNull(stream); + using DrawingCom.IStreamWrapper streamWrapper = DrawingCom.GetComWrapper(new GPStream(stream)); IntPtr bitmap = IntPtr.Zero; @@ -78,8 +80,11 @@ public Bitmap(Type type, string resource) : this(GetResourceStream(type, resourc { } - private static Stream GetResourceStream(Type type!!, string resource!!) + private static Stream GetResourceStream(Type type, string resource) { + ArgumentNullException.ThrowIfNull(type); + ArgumentNullException.ThrowIfNull(resource); + Stream? stream = type.Module.Assembly.GetManifestResourceStream(type, resource); if (stream == null) { @@ -93,8 +98,10 @@ public Bitmap(int width, int height) : this(width, height, PixelFormat.Format32b { } - public Bitmap(int width, int height, Graphics g!!) + public Bitmap(int width, int height, Graphics g) { + ArgumentNullException.ThrowIfNull(g); + IntPtr bitmap; int status = Gdip.GdipCreateBitmapFromGraphics(width, height, new HandleRef(g, g.NativeGraphics), out bitmap); Gdip.CheckStatus(status); @@ -128,8 +135,10 @@ public Bitmap(Image original, Size newSize) : this(original, newSize.Width, newS { } - public Bitmap(Image original!!, int width, int height) : this(width, height, PixelFormat.Format32bppArgb) + public Bitmap(Image original, int width, int height) : this(width, height, PixelFormat.Format32bppArgb) { + ArgumentNullException.ThrowIfNull(original); + using (Graphics g = Graphics.FromImage(this)) { g.Clear(Color.Transparent); diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Drawing2D/GraphicsPath.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Drawing2D/GraphicsPath.cs index aeafa81d13e6b..c591fe883880d 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Drawing2D/GraphicsPath.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Drawing2D/GraphicsPath.cs @@ -26,8 +26,10 @@ public GraphicsPath(FillMode fillMode) public GraphicsPath(PointF[] pts, byte[] types) : this(pts, types, FillMode.Alternate) { } - public unsafe GraphicsPath(PointF[] pts!!, byte[] types, FillMode fillMode) + public unsafe GraphicsPath(PointF[] pts, byte[] types, FillMode fillMode) { + ArgumentNullException.ThrowIfNull(pts); + if (pts.Length != types.Length) throw Gdip.StatusException(Gdip.InvalidParameter); @@ -43,8 +45,10 @@ public unsafe GraphicsPath(PointF[] pts!!, byte[] types, FillMode fillMode) public GraphicsPath(Point[] pts, byte[] types) : this(pts, types, FillMode.Alternate) { } - public unsafe GraphicsPath(Point[] pts!!, byte[] types, FillMode fillMode) + public unsafe GraphicsPath(Point[] pts, byte[] types, FillMode fillMode) { + ArgumentNullException.ThrowIfNull(pts); + if (pts.Length != types.Length) throw Gdip.StatusException(Gdip.InvalidParameter); @@ -242,8 +246,10 @@ public bool IsOutlineVisible(float x, float y, Pen pen, Graphics? graphics) return IsOutlineVisible(new PointF(x, y), pen, graphics); } - public bool IsOutlineVisible(PointF pt, Pen pen!!, Graphics? graphics) + public bool IsOutlineVisible(PointF pt, Pen pen, Graphics? graphics) { + ArgumentNullException.ThrowIfNull(pen); + Gdip.CheckStatus(Gdip.GdipIsOutlineVisiblePathPoint( new HandleRef(this, _nativePath), pt.X, pt.Y, @@ -260,8 +266,10 @@ public bool IsOutlineVisible(PointF pt, Pen pen!!, Graphics? graphics) public bool IsOutlineVisible(int x, int y, Pen pen, Graphics? graphics) => IsOutlineVisible(new Point(x, y), pen, graphics); - public bool IsOutlineVisible(Point pt, Pen pen!!, Graphics? graphics) + public bool IsOutlineVisible(Point pt, Pen pen, Graphics? graphics) { + ArgumentNullException.ThrowIfNull(pen); + Gdip.CheckStatus(Gdip.GdipIsOutlineVisiblePathPointI( new HandleRef(this, _nativePath), pt.X, pt.Y, @@ -279,8 +287,10 @@ public void AddLine(float x1, float y1, float x2, float y2) Gdip.CheckStatus(Gdip.GdipAddPathLine(new HandleRef(this, _nativePath), x1, y1, x2, y2)); } - public unsafe void AddLines(PointF[] points!!) + public unsafe void AddLines(PointF[] points) { + ArgumentNullException.ThrowIfNull(points); + if (points.Length == 0) throw new ArgumentException(null, nameof(points)); @@ -297,8 +307,10 @@ public void AddLine(int x1, int y1, int x2, int y2) Gdip.CheckStatus(Gdip.GdipAddPathLineI(new HandleRef(this, _nativePath), x1, y1, x2, y2)); } - public unsafe void AddLines(Point[] points!!) + public unsafe void AddLines(Point[] points) { + ArgumentNullException.ThrowIfNull(points); + if (points.Length == 0) throw new ArgumentException(null, nameof(points)); @@ -348,8 +360,10 @@ public void AddBezier(float x1, float y1, float x2, float y2, float x3, float y3 x1, y1, x2, y2, x3, y3, x4, y4)); } - public unsafe void AddBeziers(PointF[] points!!) + public unsafe void AddBeziers(PointF[] points) { + ArgumentNullException.ThrowIfNull(points); + fixed (PointF* p = points) { Gdip.CheckStatus(Gdip.GdipAddPathBeziers(new HandleRef(this, _nativePath), p, points.Length)); @@ -368,8 +382,10 @@ public void AddBezier(int x1, int y1, int x2, int y2, int x3, int y3, int x4, in x1, y1, x2, y2, x3, y3, x4, y4)); } - public unsafe void AddBeziers(params Point[] points!!) + public unsafe void AddBeziers(params Point[] points) { + ArgumentNullException.ThrowIfNull(points); + if (points.Length == 0) return; @@ -382,16 +398,20 @@ public unsafe void AddBeziers(params Point[] points!!) /// /// Add cardinal splines to the path object /// - public unsafe void AddCurve(PointF[] points!!) + public unsafe void AddCurve(PointF[] points) { + ArgumentNullException.ThrowIfNull(points); + fixed (PointF* p = points) { Gdip.CheckStatus(Gdip.GdipAddPathCurve(new HandleRef(this, _nativePath), p, points.Length)); } } - public unsafe void AddCurve(PointF[] points!!, float tension) + public unsafe void AddCurve(PointF[] points, float tension) { + ArgumentNullException.ThrowIfNull(points); + if (points.Length == 0) return; @@ -401,8 +421,10 @@ public unsafe void AddCurve(PointF[] points!!, float tension) } } - public unsafe void AddCurve(PointF[] points!!, int offset, int numberOfSegments, float tension) + public unsafe void AddCurve(PointF[] points, int offset, int numberOfSegments, float tension) { + ArgumentNullException.ThrowIfNull(points); + fixed (PointF* p = points) { Gdip.CheckStatus(Gdip.GdipAddPathCurve3( @@ -410,16 +432,20 @@ public unsafe void AddCurve(PointF[] points!!, int offset, int numberOfSegments, } } - public unsafe void AddCurve(Point[] points!!) + public unsafe void AddCurve(Point[] points) { + ArgumentNullException.ThrowIfNull(points); + fixed (Point* p = points) { Gdip.CheckStatus(Gdip.GdipAddPathCurveI(new HandleRef(this, _nativePath), p, points.Length)); } } - public unsafe void AddCurve(Point[] points!!, float tension) + public unsafe void AddCurve(Point[] points, float tension) { + ArgumentNullException.ThrowIfNull(points); + fixed (Point* p = points) { Gdip.CheckStatus(Gdip.GdipAddPathCurve2I( @@ -427,8 +453,10 @@ public unsafe void AddCurve(Point[] points!!, float tension) } } - public unsafe void AddCurve(Point[] points!!, int offset, int numberOfSegments, float tension) + public unsafe void AddCurve(Point[] points, int offset, int numberOfSegments, float tension) { + ArgumentNullException.ThrowIfNull(points); + fixed (Point* p = points) { Gdip.CheckStatus(Gdip.GdipAddPathCurve3I( @@ -436,8 +464,10 @@ public unsafe void AddCurve(Point[] points!!, int offset, int numberOfSegments, } } - public unsafe void AddClosedCurve(PointF[] points!!) + public unsafe void AddClosedCurve(PointF[] points) { + ArgumentNullException.ThrowIfNull(points); + fixed (PointF* p = points) { Gdip.CheckStatus(Gdip.GdipAddPathClosedCurve( @@ -445,24 +475,30 @@ public unsafe void AddClosedCurve(PointF[] points!!) } } - public unsafe void AddClosedCurve(PointF[] points!!, float tension) + public unsafe void AddClosedCurve(PointF[] points, float tension) { + ArgumentNullException.ThrowIfNull(points); + fixed (PointF* p = points) { Gdip.CheckStatus(Gdip.GdipAddPathClosedCurve2(new HandleRef(this, _nativePath), p, points.Length, tension)); } } - public unsafe void AddClosedCurve(Point[] points!!) + public unsafe void AddClosedCurve(Point[] points) { + ArgumentNullException.ThrowIfNull(points); + fixed (Point* p = points) { Gdip.CheckStatus(Gdip.GdipAddPathClosedCurveI(new HandleRef(this, _nativePath), p, points.Length)); } } - public unsafe void AddClosedCurve(Point[] points!!, float tension) + public unsafe void AddClosedCurve(Point[] points, float tension) { + ArgumentNullException.ThrowIfNull(points); + fixed (Point* p = points) { Gdip.CheckStatus(Gdip.GdipAddPathClosedCurve2I(new HandleRef(this, _nativePath), p, points.Length, tension)); @@ -476,8 +512,10 @@ public void AddRectangle(RectangleF rect) rect.X, rect.Y, rect.Width, rect.Height)); } - public unsafe void AddRectangles(RectangleF[] rects!!) + public unsafe void AddRectangles(RectangleF[] rects) { + ArgumentNullException.ThrowIfNull(rects); + if (rects.Length == 0) throw new ArgumentException(null, nameof(rects)); @@ -495,8 +533,10 @@ public void AddRectangle(Rectangle rect) rect.X, rect.Y, rect.Width, rect.Height)); } - public unsafe void AddRectangles(Rectangle[] rects!!) + public unsafe void AddRectangles(Rectangle[] rects) { + ArgumentNullException.ThrowIfNull(rects); + if (rects.Length == 0) throw new ArgumentException(null, nameof(rects)); @@ -547,8 +587,10 @@ public void AddPie(int x, int y, int width, int height, float startAngle, float sweepAngle)); } - public unsafe void AddPolygon(PointF[] points!!) + public unsafe void AddPolygon(PointF[] points) { + ArgumentNullException.ThrowIfNull(points); + fixed (PointF* p = points) { Gdip.CheckStatus(Gdip.GdipAddPathPolygon(new HandleRef(this, _nativePath), p, points.Length)); @@ -558,16 +600,20 @@ public unsafe void AddPolygon(PointF[] points!!) /// /// Adds a polygon to the current figure. /// - public unsafe void AddPolygon(Point[] points!!) + public unsafe void AddPolygon(Point[] points) { + ArgumentNullException.ThrowIfNull(points); + fixed (Point* p = points) { Gdip.CheckStatus(Gdip.GdipAddPathPolygonI(new HandleRef(this, _nativePath), p, points.Length)); } } - public void AddPath(GraphicsPath addingPath!!, bool connect) + public void AddPath(GraphicsPath addingPath, bool connect) { + ArgumentNullException.ThrowIfNull(addingPath); + Gdip.CheckStatus(Gdip.GdipAddPathPath( new HandleRef(this, _nativePath), new HandleRef(addingPath, addingPath._nativePath), connect)); } @@ -582,8 +628,10 @@ public void AddString(string s, FontFamily family, int style, float emSize, Poin AddString(s, family, style, emSize, new Rectangle(origin.X, origin.Y, 0, 0), format); } - public void AddString(string s, FontFamily family!!, int style, float emSize, RectangleF layoutRect, StringFormat? format) + public void AddString(string s, FontFamily family, int style, float emSize, RectangleF layoutRect, StringFormat? format) { + ArgumentNullException.ThrowIfNull(family); + Gdip.CheckStatus(Gdip.GdipAddPathString( new HandleRef(this, _nativePath), s, @@ -595,8 +643,10 @@ public void AddString(string s, FontFamily family!!, int style, float emSize, Re new HandleRef(format, format?.nativeFormat ?? IntPtr.Zero))); } - public void AddString(string s, FontFamily family!!, int style, float emSize, Rectangle layoutRect, StringFormat? format) + public void AddString(string s, FontFamily family, int style, float emSize, Rectangle layoutRect, StringFormat? format) { + ArgumentNullException.ThrowIfNull(family); + Gdip.CheckStatus(Gdip.GdipAddPathStringI( new HandleRef(this, _nativePath), s, @@ -608,8 +658,10 @@ public void AddString(string s, FontFamily family!!, int style, float emSize, Re new HandleRef(format, format?.nativeFormat ?? IntPtr.Zero))); } - public void Transform(Matrix matrix!!) + public void Transform(Matrix matrix) { + ArgumentNullException.ThrowIfNull(matrix); + if (matrix.NativeMatrix == IntPtr.Zero) return; @@ -649,8 +701,10 @@ public void Flatten(Matrix? matrix, float flatness) public void Widen(Pen pen, Matrix? matrix) => Widen(pen, matrix, Flatness); - public void Widen(Pen pen!!, Matrix? matrix, float flatness) + public void Widen(Pen pen, Matrix? matrix, float flatness) { + ArgumentNullException.ThrowIfNull(pen); + // GDI+ wrongly returns an out of memory status when there is nothing in the path, so we have to check // before calling the widen method and do nothing if we dont have anything in the path. if (PointCount == 0) @@ -672,8 +726,10 @@ public void Warp(PointF[] destPoints, RectangleF srcRect, Matrix? matrix, WarpMo Warp(destPoints, srcRect, matrix, warpMode, 0.25f); } - public unsafe void Warp(PointF[] destPoints!!, RectangleF srcRect, Matrix? matrix, WarpMode warpMode, float flatness) + public unsafe void Warp(PointF[] destPoints, RectangleF srcRect, Matrix? matrix, WarpMode warpMode, float flatness) { + ArgumentNullException.ThrowIfNull(destPoints); + fixed (PointF* p = destPoints) { Gdip.CheckStatus(Gdip.GdipWarpPath( diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Drawing2D/LinearGradientBrush.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Drawing2D/LinearGradientBrush.cs index 56afa27cee56d..bf9aa25a3d7ec 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Drawing2D/LinearGradientBrush.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Drawing2D/LinearGradientBrush.cs @@ -476,8 +476,10 @@ public void ResetTransform() public void MultiplyTransform(Matrix matrix) => MultiplyTransform(matrix, MatrixOrder.Prepend); - public void MultiplyTransform(Matrix matrix!!, MatrixOrder order) + 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 compatability with GDI+. if (matrix.NativeMatrix == IntPtr.Zero) diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Drawing2D/Matrix.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Drawing2D/Matrix.cs index d418b7a7d3055..ffaa33fb55252 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Drawing2D/Matrix.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Drawing2D/Matrix.cs @@ -52,8 +52,10 @@ internal static IntPtr CreateNativeHandle(Matrix3x2 matrix) return nativeMatrix; } - public unsafe Matrix(RectangleF rect, PointF[] plgpts!!) + public unsafe Matrix(RectangleF rect, PointF[] plgpts) { + ArgumentNullException.ThrowIfNull(plgpts); + if (plgpts.Length != 3) throw Gdip.StatusException(Gdip.InvalidParameter); @@ -64,8 +66,10 @@ public unsafe Matrix(RectangleF rect, PointF[] plgpts!!) } } - public unsafe Matrix(Rectangle rect, Point[] plgpts!!) + public unsafe Matrix(Rectangle rect, Point[] plgpts) { + ArgumentNullException.ThrowIfNull(plgpts); + if (plgpts.Length != 3) throw Gdip.StatusException(Gdip.InvalidParameter); @@ -170,8 +174,10 @@ public void Reset() public void Multiply(Matrix matrix) => Multiply(matrix, MatrixOrder.Prepend); - public void Multiply(Matrix matrix!!, MatrixOrder order) + public void Multiply(Matrix matrix, MatrixOrder order) { + ArgumentNullException.ThrowIfNull(matrix); + if (matrix.NativeMatrix == NativeMatrix) throw new InvalidOperationException(SR.GdiplusObjectBusy); @@ -240,8 +246,10 @@ public void Invert() Gdip.CheckStatus(Gdip.GdipInvertMatrix(new HandleRef(this, NativeMatrix))); } - public unsafe void TransformPoints(PointF[] pts!!) + public unsafe void TransformPoints(PointF[] pts) { + ArgumentNullException.ThrowIfNull(pts); + fixed (PointF* p = pts) { Gdip.CheckStatus(Gdip.GdipTransformMatrixPoints( @@ -251,8 +259,10 @@ public unsafe void TransformPoints(PointF[] pts!!) } } - public unsafe void TransformPoints(Point[] pts!!) + public unsafe void TransformPoints(Point[] pts) { + ArgumentNullException.ThrowIfNull(pts); + fixed (Point* p = pts) { Gdip.CheckStatus(Gdip.GdipTransformMatrixPointsI( @@ -262,8 +272,10 @@ public unsafe void TransformPoints(Point[] pts!!) } } - public unsafe void TransformVectors(PointF[] pts!!) + public unsafe void TransformVectors(PointF[] pts) { + ArgumentNullException.ThrowIfNull(pts); + fixed (PointF* p = pts) { Gdip.CheckStatus(Gdip.GdipVectorTransformMatrixPoints( @@ -275,8 +287,10 @@ public unsafe void TransformVectors(PointF[] pts!!) public void VectorTransformPoints(Point[] pts) => TransformVectors(pts); - public unsafe void TransformVectors(Point[] pts!!) + public unsafe void TransformVectors(Point[] pts) { + ArgumentNullException.ThrowIfNull(pts); + fixed (Point* p = pts) { Gdip.CheckStatus(Gdip.GdipVectorTransformMatrixPointsI( diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Drawing2D/PathGradientBrush.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Drawing2D/PathGradientBrush.cs index d05f2585e08f7..9eed53798f8b0 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Drawing2D/PathGradientBrush.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Drawing2D/PathGradientBrush.cs @@ -13,8 +13,10 @@ public sealed class PathGradientBrush : Brush { public PathGradientBrush(PointF[] points) : this(points, WrapMode.Clamp) { } - public unsafe PathGradientBrush(PointF[] points!!, WrapMode wrapMode) + public unsafe PathGradientBrush(PointF[] points, WrapMode wrapMode) { + ArgumentNullException.ThrowIfNull(points); + if (wrapMode < WrapMode.Tile || wrapMode > WrapMode.Clamp) throw new InvalidEnumArgumentException(nameof(wrapMode), unchecked((int)wrapMode), typeof(WrapMode)); @@ -35,8 +37,10 @@ public unsafe PathGradientBrush(PointF[] points!!, WrapMode wrapMode) public PathGradientBrush(Point[] points) : this(points, WrapMode.Clamp) { } - public unsafe PathGradientBrush(Point[] points!!, WrapMode wrapMode) + public unsafe PathGradientBrush(Point[] points, WrapMode wrapMode) { + ArgumentNullException.ThrowIfNull(points); + if (wrapMode < WrapMode.Tile || wrapMode > WrapMode.Clamp) throw new InvalidEnumArgumentException(nameof(wrapMode), unchecked((int)wrapMode), typeof(WrapMode)); @@ -55,8 +59,10 @@ public unsafe PathGradientBrush(Point[] points!!, WrapMode wrapMode) } } - public PathGradientBrush(GraphicsPath path!!) + public PathGradientBrush(GraphicsPath path) { + ArgumentNullException.ThrowIfNull(path); + Gdip.CheckStatus(Gdip.GdipCreatePathGradientFromPath(new HandleRef(path, path._nativePath), out IntPtr nativeBrush)); SetNativeBrushInternal(nativeBrush); } @@ -331,8 +337,9 @@ public void ResetTransform() public void MultiplyTransform(Matrix matrix) => MultiplyTransform(matrix, MatrixOrder.Prepend); - public void MultiplyTransform(Matrix matrix!!, MatrixOrder order) + 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 compatability with GDI+. diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Font.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Font.cs index 82bbcd57c652d..5fa7227b53721 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Font.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Font.cs @@ -199,8 +199,10 @@ private void Dispose(bool disposing) /// /// Returns the height of this Font in the specified graphics context. /// - public float GetHeight(Graphics graphics!!) + public float GetHeight(Graphics graphics) { + ArgumentNullException.ThrowIfNull(graphics); + float height; int status = Gdip.GdipGetFontHeight(new HandleRef(this, NativeFont), new HandleRef(graphics, graphics.NativeGraphics), out height); Gdip.CheckStatus(status); @@ -261,8 +263,10 @@ public override string ToString() => // This is used by SystemFonts when constructing a system Font objects. internal void SetSystemFontName(string systemFontName) => _systemFontName = systemFontName; - public unsafe void ToLogFont(object logFont!!, Graphics graphics) + public unsafe void ToLogFont(object logFont, Graphics graphics) { + ArgumentNullException.ThrowIfNull(logFont); + Type type = logFont.GetType(); int nativeSize = sizeof(Interop.User32.LOGFONT); if (Marshal.SizeOf(type) != nativeSize) @@ -287,8 +291,10 @@ public unsafe void ToLogFont(object logFont!!, Graphics graphics) } } - private unsafe Interop.User32.LOGFONT ToLogFontInternal(Graphics graphics!!) + private unsafe Interop.User32.LOGFONT ToLogFontInternal(Graphics graphics) { + ArgumentNullException.ThrowIfNull(graphics); + Interop.User32.LOGFONT logFont = default; Gdip.CheckStatus(Gdip.GdipGetLogFontW( new HandleRef(this, NativeFont), new HandleRef(graphics, graphics.NativeGraphics), ref logFont)); @@ -485,8 +491,10 @@ private void Initialize(string familyName, float emSize, FontStyle style, Graphi /// /// Initializes this object's fields. /// - private void Initialize(FontFamily family!!, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet, bool gdiVerticalFont) + private void Initialize(FontFamily family, float emSize, FontStyle style, GraphicsUnit unit, byte gdiCharSet, bool gdiVerticalFont) { + ArgumentNullException.ThrowIfNull(family); + if (float.IsNaN(emSize) || float.IsInfinity(emSize) || emSize <= 0) { throw new ArgumentException(SR.Format(SR.InvalidBoundArgument, nameof(emSize), emSize, 0, "System.Single.MaxValue"), nameof(emSize)); @@ -581,8 +589,10 @@ internal static Font FromLogFontInternal(ref Interop.User32.LOGFONT logFont, Int /// A boxed LOGFONT. /// Handle to a device context (HDC). /// The newly created . - public static unsafe Font FromLogFont(object lf!!, IntPtr hdc) + public static unsafe Font FromLogFont(object lf, IntPtr hdc) { + ArgumentNullException.ThrowIfNull(lf); + if (lf is Interop.User32.LOGFONT logFont) { // A boxed LOGFONT, just use it to create the font diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/FontConverter.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/FontConverter.cs index 42fe50531576d..d818eb096d9e3 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/FontConverter.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/FontConverter.cs @@ -270,8 +270,10 @@ private static GraphicsUnit ParseGraphicsUnits(string units) => _ => throw new ArgumentException(SR.Format(SR.InvalidArgumentValueFontConverter, units), nameof(units)), }; - public override object CreateInstance(ITypeDescriptorContext? context, IDictionary propertyValues!!) + public override object CreateInstance(ITypeDescriptorContext? context, IDictionary propertyValues) { + ArgumentNullException.ThrowIfNull(propertyValues); + object? value; byte charSet = 1; float size = 8; diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/FontFamily.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/FontFamily.cs index 6cc17d4909a39..7991b1ad6b645 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/FontFamily.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/FontFamily.cs @@ -244,8 +244,10 @@ private static IntPtr GetGdipGenericSansSerif() /// graphics context. /// [Obsolete("FontFamily.GetFamilies has been deprecated. Use Families instead.")] - public static FontFamily[] GetFamilies(Graphics graphics!!) + public static FontFamily[] GetFamilies(Graphics graphics) { + ArgumentNullException.ThrowIfNull(graphics); + return new InstalledFontCollection().Families; } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.cs index a5167e6b86e47..906672485951b 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.cs @@ -133,8 +133,10 @@ public static Graphics FromHwndInternal(IntPtr hwnd) /// /// Creates an instance of the class from an existing . /// - public static Graphics FromImage(Image image!!) + public static Graphics FromImage(Image image) { + ArgumentNullException.ThrowIfNull(image); + if ((image.PixelFormat & PixelFormat.Indexed) != 0) throw new ArgumentException(SR.GdiplusCannotCreateGraphicsFromIndexedPixelFormat, nameof(image)); @@ -583,8 +585,10 @@ public void Flush(FlushIntention intention) public void SetClip(Graphics g) => SetClip(g, CombineMode.Replace); - public void SetClip(Graphics g!!, CombineMode combineMode) + public void SetClip(Graphics g, CombineMode combineMode) { + ArgumentNullException.ThrowIfNull(g); + Gdip.CheckStatus(Gdip.GdipSetClipGraphics( new HandleRef(this, NativeGraphics), new HandleRef(g, g.NativeGraphics), @@ -613,16 +617,20 @@ public void SetClip(RectangleF rect, CombineMode combineMode) public void SetClip(GraphicsPath path) => SetClip(path, CombineMode.Replace); - public void SetClip(GraphicsPath path!!, CombineMode combineMode) + 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) + public void SetClip(Region region, CombineMode combineMode) { + ArgumentNullException.ThrowIfNull(region); + Gdip.CheckStatus(Gdip.GdipSetClipRegion( new HandleRef(this, NativeGraphics), new HandleRef(region, region.NativeRegion), @@ -645,8 +653,10 @@ public void IntersectClip(RectangleF rect) CombineMode.Intersect)); } - public void IntersectClip(Region region!!) + public void IntersectClip(Region region) { + ArgumentNullException.ThrowIfNull(region); + Gdip.CheckStatus(Gdip.GdipSetClipRegion( new HandleRef(this, NativeGraphics), new HandleRef(region, region.NativeRegion), @@ -661,8 +671,10 @@ public void ExcludeClip(Rectangle rect) CombineMode.Exclude)); } - public void ExcludeClip(Region region!!) + public void ExcludeClip(Region region) { + ArgumentNullException.ThrowIfNull(region); + Gdip.CheckStatus(Gdip.GdipSetClipRegion( new HandleRef(this, NativeGraphics), new HandleRef(region, region.NativeRegion), @@ -754,8 +766,10 @@ public void ResetTransform() /// /// Multiplies the that represents the world transform and . /// - public void MultiplyTransform(Matrix matrix!!, MatrixOrder order) + 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 compatability with GDI+. if (matrix.NativeMatrix == IntPtr.Zero) @@ -789,8 +803,10 @@ public void RotateTransform(float angle, MatrixOrder order) /// /// Draws an arc from the specified ellipse. /// - public void DrawArc(Pen pen!!, float x, float y, float width, float height, float startAngle, float sweepAngle) + 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), @@ -810,8 +826,10 @@ public void DrawArc(Pen pen, RectangleF rect, float startAngle, float sweepAngle /// /// Draws an arc from the specified ellipse. /// - public void DrawArc(Pen pen!!, int x, int y, int width, int height, int startAngle, int sweepAngle) + 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), @@ -831,8 +849,10 @@ public void DrawArc(Pen pen, Rectangle rect, float startAngle, float sweepAngle) /// /// Draws a cubic bezier curve defined by four ordered pairs that represent points. /// - public void DrawBezier(Pen pen!!, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) + 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)); @@ -875,8 +895,10 @@ public void DrawRectangle(Pen pen, Rectangle rect) /// /// Draws the outline of the specified rectangle. /// - public void DrawRectangle(Pen pen!!, float x, float y, float width, float height) + 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)); @@ -885,8 +907,10 @@ public void DrawRectangle(Pen pen!!, float x, float y, float width, float height /// /// Draws the outline of the specified rectangle. /// - public void DrawRectangle(Pen pen!!, int x, int y, int width, int height) + 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)); @@ -895,8 +919,11 @@ public void DrawRectangle(Pen pen!!, int x, int y, int width, int height) /// /// Draws the outlines of a series of rectangles. /// - public unsafe void DrawRectangles(Pen pen!!, RectangleF[] rects!!) + public unsafe void DrawRectangles(Pen pen, RectangleF[] rects) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(rects); + fixed (RectangleF* r = rects) { CheckErrorStatus(Gdip.GdipDrawRectangles( @@ -908,8 +935,11 @@ public unsafe void DrawRectangles(Pen pen!!, RectangleF[] rects!!) /// /// Draws the outlines of a series of rectangles. /// - public unsafe void DrawRectangles(Pen pen!!, Rectangle[] rects!!) + public unsafe void DrawRectangles(Pen pen, Rectangle[] rects) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(rects); + fixed (Rectangle* r = rects) { CheckErrorStatus(Gdip.GdipDrawRectanglesI( @@ -929,8 +959,10 @@ public void DrawEllipse(Pen pen, RectangleF rect) /// /// Draws the outline of an ellipse defined by a bounding rectangle. /// - public void DrawEllipse(Pen pen!!, float x, float y, float width, float height) + public void DrawEllipse(Pen pen, float x, float y, float width, float height) { + ArgumentNullException.ThrowIfNull(pen); + CheckErrorStatus(Gdip.GdipDrawEllipse( new HandleRef(this, NativeGraphics), new HandleRef(pen, pen.NativePen), @@ -948,8 +980,10 @@ public void DrawEllipse(Pen pen, Rectangle rect) /// /// Draws the outline of an ellipse defined by a bounding rectangle. /// - public void DrawEllipse(Pen pen!!, int x, int y, int width, int height) + public void DrawEllipse(Pen pen, int x, int y, int width, int height) { + ArgumentNullException.ThrowIfNull(pen); + CheckErrorStatus(Gdip.GdipDrawEllipseI( new HandleRef(this, NativeGraphics), new HandleRef(pen, pen.NativePen), @@ -967,8 +1001,10 @@ public void DrawPie(Pen pen, RectangleF rect, float startAngle, float sweepAngle /// /// Draws the outline of a pie section defined by an ellipse and two radial lines. /// - public void DrawPie(Pen pen!!, float x, float y, float width, float height, float startAngle, float sweepAngle) + public void DrawPie(Pen pen, float x, float y, float width, float height, float startAngle, float sweepAngle) { + ArgumentNullException.ThrowIfNull(pen); + CheckErrorStatus(Gdip.GdipDrawPie( new HandleRef(this, NativeGraphics), new HandleRef(pen, pen.NativePen), @@ -988,8 +1024,10 @@ public void DrawPie(Pen pen, Rectangle rect, float startAngle, float sweepAngle) /// /// Draws the outline of a pie section defined by an ellipse and two radial lines. /// - public void DrawPie(Pen pen!!, int x, int y, int width, int height, int startAngle, int sweepAngle) + public void DrawPie(Pen pen, int x, int y, int width, int height, int startAngle, int sweepAngle) { + ArgumentNullException.ThrowIfNull(pen); + CheckErrorStatus(Gdip.GdipDrawPieI( new HandleRef(this, NativeGraphics), new HandleRef(pen, pen.NativePen), @@ -1001,8 +1039,11 @@ public void DrawPie(Pen pen!!, int x, int y, int width, int height, int startAng /// /// Draws the outline of a polygon defined by an array of points. /// - public unsafe void DrawPolygon(Pen pen!!, PointF[] points!!) + public unsafe void DrawPolygon(Pen pen, PointF[] points) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(points); + fixed (PointF* p = points) { CheckErrorStatus(Gdip.GdipDrawPolygon( @@ -1014,8 +1055,11 @@ public unsafe void DrawPolygon(Pen pen!!, PointF[] points!!) /// /// Draws the outline of a polygon defined by an array of points. /// - public unsafe void DrawPolygon(Pen pen!!, Point[] points!!) + public unsafe void DrawPolygon(Pen pen, Point[] points) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(points); + fixed (Point* p = points) { CheckErrorStatus(Gdip.GdipDrawPolygonI( @@ -1027,8 +1071,11 @@ public unsafe void DrawPolygon(Pen pen!!, Point[] points!!) /// /// Draws the lines and curves defined by a . /// - public void DrawPath(Pen pen!!, GraphicsPath path!!) + public void DrawPath(Pen pen, GraphicsPath path) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(path); + CheckErrorStatus(Gdip.GdipDrawPath( new HandleRef(this, NativeGraphics), new HandleRef(pen, pen.NativePen), @@ -1038,8 +1085,11 @@ public void DrawPath(Pen pen!!, GraphicsPath path!!) /// /// Draws a curve defined by an array of points. /// - public unsafe void DrawCurve(Pen pen!!, PointF[] points!!) + public unsafe void DrawCurve(Pen pen, PointF[] points) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(points); + fixed (PointF* p = points) { CheckErrorStatus(Gdip.GdipDrawCurve( @@ -1052,8 +1102,11 @@ public unsafe void DrawCurve(Pen pen!!, PointF[] points!!) /// /// Draws a curve defined by an array of points. /// - public unsafe void DrawCurve(Pen pen!!, PointF[] points!!, float tension) + public unsafe void DrawCurve(Pen pen, PointF[] points, float tension) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(points); + fixed (PointF* p = points) { CheckErrorStatus(Gdip.GdipDrawCurve2( @@ -1072,8 +1125,11 @@ public void DrawCurve(Pen pen, PointF[] points, int offset, int numberOfSegments /// /// Draws a curve defined by an array of points. /// - public unsafe void DrawCurve(Pen pen!!, PointF[] points!!, int offset, int numberOfSegments, float tension) + public unsafe void DrawCurve(Pen pen, PointF[] points, int offset, int numberOfSegments, float tension) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(points); + fixed (PointF* p = points) { CheckErrorStatus(Gdip.GdipDrawCurve3( @@ -1089,8 +1145,11 @@ public unsafe void DrawCurve(Pen pen!!, PointF[] points!!, int offset, int numbe /// /// Draws a curve defined by an array of points. /// - public unsafe void DrawCurve(Pen pen!!, Point[] points!!) + public unsafe void DrawCurve(Pen pen, Point[] points) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(points); + fixed (Point* p = points) { CheckErrorStatus(Gdip.GdipDrawCurveI( @@ -1103,8 +1162,11 @@ public unsafe void DrawCurve(Pen pen!!, Point[] points!!) /// /// Draws a curve defined by an array of points. /// - public unsafe void DrawCurve(Pen pen!!, Point[] points!!, float tension) + public unsafe void DrawCurve(Pen pen, Point[] points, float tension) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(points); + fixed (Point* p = points) { CheckErrorStatus(Gdip.GdipDrawCurve2I( @@ -1118,8 +1180,11 @@ public unsafe void DrawCurve(Pen pen!!, Point[] points!!, float tension) /// /// Draws a curve defined by an array of points. /// - public unsafe void DrawCurve(Pen pen!!, Point[] points!!, int offset, int numberOfSegments, float tension) + public unsafe void DrawCurve(Pen pen, Point[] points, int offset, int numberOfSegments, float tension) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(points); + fixed (Point* p = points) { CheckErrorStatus(Gdip.GdipDrawCurve3I( @@ -1135,8 +1200,11 @@ public unsafe void DrawCurve(Pen pen!!, Point[] points!!, int offset, int number /// /// Draws a closed curve defined by an array of points. /// - public unsafe void DrawClosedCurve(Pen pen!!, PointF[] points!!) + public unsafe void DrawClosedCurve(Pen pen, PointF[] points) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(points); + fixed (PointF* p = points) { CheckErrorStatus(Gdip.GdipDrawClosedCurve( @@ -1149,8 +1217,11 @@ public unsafe void DrawClosedCurve(Pen pen!!, PointF[] points!!) /// /// Draws a closed curve defined by an array of points. /// - public unsafe void DrawClosedCurve(Pen pen!!, PointF[] points!!, float tension, FillMode fillmode) + public unsafe void DrawClosedCurve(Pen pen, PointF[] points, float tension, FillMode fillmode) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(points); + fixed (PointF* p = points) { CheckErrorStatus(Gdip.GdipDrawClosedCurve2( @@ -1164,8 +1235,11 @@ public unsafe void DrawClosedCurve(Pen pen!!, PointF[] points!!, float tension, /// /// Draws a closed curve defined by an array of points. /// - public unsafe void DrawClosedCurve(Pen pen!!, Point[] points!!) + public unsafe void DrawClosedCurve(Pen pen, Point[] points) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(points); + fixed (Point* p = points) { CheckErrorStatus(Gdip.GdipDrawClosedCurveI( @@ -1178,8 +1252,11 @@ public unsafe void DrawClosedCurve(Pen pen!!, Point[] points!!) /// /// Draws a closed curve defined by an array of points. /// - public unsafe void DrawClosedCurve(Pen pen!!, Point[] points!!, float tension, FillMode fillmode) + public unsafe void DrawClosedCurve(Pen pen, Point[] points, float tension, FillMode fillmode) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(points); + fixed (Point* p = points) { CheckErrorStatus(Gdip.GdipDrawClosedCurve2I( @@ -1209,8 +1286,10 @@ public void FillRectangle(Brush brush, RectangleF rect) /// /// Fills the interior of a rectangle with a . /// - public void FillRectangle(Brush brush!!, float x, float y, float width, float height) + public void FillRectangle(Brush brush, float x, float y, float width, float height) { + ArgumentNullException.ThrowIfNull(brush); + CheckErrorStatus(Gdip.GdipFillRectangle( new HandleRef(this, NativeGraphics), new HandleRef(brush, brush.NativeBrush), @@ -1228,8 +1307,10 @@ public void FillRectangle(Brush brush, Rectangle rect) /// /// Fills the interior of a rectangle with a . /// - public void FillRectangle(Brush brush!!, int x, int y, int width, int height) + public void FillRectangle(Brush brush, int x, int y, int width, int height) { + ArgumentNullException.ThrowIfNull(brush); + CheckErrorStatus(Gdip.GdipFillRectangleI( new HandleRef(this, NativeGraphics), new HandleRef(brush, brush.NativeBrush), @@ -1239,8 +1320,11 @@ public void FillRectangle(Brush brush!!, int x, int y, int width, int height) /// /// Fills the interiors of a series of rectangles with a . /// - public unsafe void FillRectangles(Brush brush!!, RectangleF[] rects!!) + public unsafe void FillRectangles(Brush brush, RectangleF[] rects) { + ArgumentNullException.ThrowIfNull(brush); + ArgumentNullException.ThrowIfNull(rects); + fixed (RectangleF* r = rects) { CheckErrorStatus(Gdip.GdipFillRectangles( @@ -1253,8 +1337,11 @@ public unsafe void FillRectangles(Brush brush!!, RectangleF[] rects!!) /// /// Fills the interiors of a series of rectangles with a . /// - public unsafe void FillRectangles(Brush brush!!, Rectangle[] rects!!) + public unsafe void FillRectangles(Brush brush, Rectangle[] rects) { + ArgumentNullException.ThrowIfNull(brush); + ArgumentNullException.ThrowIfNull(rects); + fixed (Rectangle* r = rects) { CheckErrorStatus(Gdip.GdipFillRectanglesI( @@ -1275,8 +1362,11 @@ public void FillPolygon(Brush brush, PointF[] points) /// /// Fills the interior of a polygon defined by an array of points. /// - public unsafe void FillPolygon(Brush brush!!, PointF[] points!!, FillMode fillMode) + public unsafe void FillPolygon(Brush brush, PointF[] points, FillMode fillMode) { + ArgumentNullException.ThrowIfNull(brush); + ArgumentNullException.ThrowIfNull(points); + fixed (PointF* p = points) { CheckErrorStatus(Gdip.GdipFillPolygon( @@ -1298,8 +1388,11 @@ public void FillPolygon(Brush brush, Point[] points) /// /// Fills the interior of a polygon defined by an array of points. /// - public unsafe void FillPolygon(Brush brush!!, Point[] points!!, FillMode fillMode) + public unsafe void FillPolygon(Brush brush, Point[] points, FillMode fillMode) { + ArgumentNullException.ThrowIfNull(brush); + ArgumentNullException.ThrowIfNull(points); + fixed (Point* p = points) { CheckErrorStatus(Gdip.GdipFillPolygonI( @@ -1321,8 +1414,10 @@ public void FillEllipse(Brush brush, RectangleF rect) /// /// Fills the interior of an ellipse defined by a bounding rectangle. /// - public void FillEllipse(Brush brush!!, float x, float y, float width, float height) + public void FillEllipse(Brush brush, float x, float y, float width, float height) { + ArgumentNullException.ThrowIfNull(brush); + CheckErrorStatus(Gdip.GdipFillEllipse( new HandleRef(this, NativeGraphics), new HandleRef(brush, brush.NativeBrush), @@ -1340,8 +1435,10 @@ public void FillEllipse(Brush brush, Rectangle rect) /// /// Fills the interior of an ellipse defined by a bounding rectangle. /// - public void FillEllipse(Brush brush!!, int x, int y, int width, int height) + public void FillEllipse(Brush brush, int x, int y, int width, int height) { + ArgumentNullException.ThrowIfNull(brush); + CheckErrorStatus(Gdip.GdipFillEllipseI( new HandleRef(this, NativeGraphics), new HandleRef(brush, brush.NativeBrush), @@ -1371,8 +1468,10 @@ public void FillPie(Brush brush, RectangleF rect, float startAngle, float sweepA /// /// Fills the interior of a pie section defined by an ellipse and two radial lines. /// - public void FillPie(Brush brush!!, float x, float y, float width, float height, float startAngle, float sweepAngle) + public void FillPie(Brush brush, float x, float y, float width, float height, float startAngle, float sweepAngle) { + ArgumentNullException.ThrowIfNull(brush); + CheckErrorStatus(Gdip.GdipFillPie( new HandleRef(this, NativeGraphics), new HandleRef(brush, brush.NativeBrush), @@ -1384,8 +1483,10 @@ public void FillPie(Brush brush!!, float x, float y, float width, float height, /// /// Fills the interior of a pie section defined by an ellipse and two radial lines. /// - public void FillPie(Brush brush!!, int x, int y, int width, int height, int startAngle, int sweepAngle) + public void FillPie(Brush brush, int x, int y, int width, int height, int startAngle, int sweepAngle) { + ArgumentNullException.ThrowIfNull(brush); + CheckErrorStatus(Gdip.GdipFillPieI( new HandleRef(this, NativeGraphics), new HandleRef(brush, brush.NativeBrush), @@ -1397,8 +1498,11 @@ public void FillPie(Brush brush!!, int x, int y, int width, int height, int star /// /// Fills the interior a closed curve defined by an array of points. /// - public unsafe void FillClosedCurve(Brush brush!!, PointF[] points!!) + public unsafe void FillClosedCurve(Brush brush, PointF[] points) { + ArgumentNullException.ThrowIfNull(brush); + ArgumentNullException.ThrowIfNull(points); + fixed (PointF* p = points) { CheckErrorStatus(Gdip.GdipFillClosedCurve( @@ -1416,8 +1520,10 @@ public void FillClosedCurve(Brush brush, PointF[] points, FillMode fillmode) FillClosedCurve(brush, points, fillmode, 0.5f); } - public unsafe void FillClosedCurve(Brush brush!!, PointF[] points!!, FillMode fillmode, float tension) + public unsafe void FillClosedCurve(Brush brush, PointF[] points, FillMode fillmode, float tension) { + ArgumentNullException.ThrowIfNull(points); + fixed (PointF* p = points) { CheckErrorStatus(Gdip.GdipFillClosedCurve2( @@ -1432,8 +1538,11 @@ public unsafe void FillClosedCurve(Brush brush!!, PointF[] points!!, FillMode fi /// /// Fills the interior a closed curve defined by an array of points. /// - public unsafe void FillClosedCurve(Brush brush!!, Point[] points!!) + public unsafe void FillClosedCurve(Brush brush, Point[] points) { + ArgumentNullException.ThrowIfNull(brush); + ArgumentNullException.ThrowIfNull(points); + fixed (Point* p = points) { CheckErrorStatus(Gdip.GdipFillClosedCurveI( @@ -1448,8 +1557,11 @@ public void FillClosedCurve(Brush brush, Point[] points, FillMode fillmode) FillClosedCurve(brush, points, fillmode, 0.5f); } - public unsafe void FillClosedCurve(Brush brush!!, Point[] points!!, FillMode fillmode, float tension) + public unsafe void FillClosedCurve(Brush brush, Point[] points, FillMode fillmode, float tension) { + ArgumentNullException.ThrowIfNull(brush); + ArgumentNullException.ThrowIfNull(points); + fixed (Point* p = points) { CheckErrorStatus(Gdip.GdipFillClosedCurve2I( @@ -1489,12 +1601,12 @@ public void DrawString(string? s, Font font, Brush brush, RectangleF layoutRecta DrawString(s, font, brush, layoutRectangle, null); } - public void DrawString(string? s, Font font, Brush brush!!, RectangleF layoutRectangle, StringFormat? format) + public void DrawString(string? s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat? format) { + ArgumentNullException.ThrowIfNull(brush); if (string.IsNullOrEmpty(s)) return; - if (font == null) - throw new ArgumentNullException(nameof(font)); + ArgumentNullException.ThrowIfNull(font); CheckErrorStatus(Gdip.GdipDrawString( new HandleRef(this, NativeGraphics), @@ -1647,8 +1759,10 @@ public void DrawImage(Image image, PointF point) DrawImage(image, point.X, point.Y); } - public void DrawImage(Image image!!, float x, float y) + public void DrawImage(Image image, float x, float y) { + ArgumentNullException.ThrowIfNull(image); + int status = Gdip.GdipDrawImage( new HandleRef(this, NativeGraphics), new HandleRef(image, image.nativeImage), x, y); @@ -1662,8 +1776,10 @@ public void DrawImage(Image image, RectangleF rect) DrawImage(image, rect.X, rect.Y, rect.Width, rect.Height); } - public void DrawImage(Image image!!, float x, float y, float width, float height) + public void DrawImage(Image image, float x, float y, float width, float height) { + ArgumentNullException.ThrowIfNull(image); + int status = Gdip.GdipDrawImageRect( new HandleRef(this, NativeGraphics), new HandleRef(image, image.nativeImage), @@ -1679,8 +1795,10 @@ public void DrawImage(Image image, Point point) DrawImage(image, point.X, point.Y); } - public void DrawImage(Image image!!, int x, int y) + public void DrawImage(Image image, int x, int y) { + ArgumentNullException.ThrowIfNull(image); + int status = Gdip.GdipDrawImageI( new HandleRef(this, NativeGraphics), new HandleRef(image, image.nativeImage), @@ -1695,8 +1813,10 @@ public void DrawImage(Image image, Rectangle rect) DrawImage(image, rect.X, rect.Y, rect.Width, rect.Height); } - public void DrawImage(Image image!!, int x, int y, int width, int height) + public void DrawImage(Image image, int x, int y, int width, int height) { + ArgumentNullException.ThrowIfNull(image); + int status = Gdip.GdipDrawImageRectI( new HandleRef(this, NativeGraphics), new HandleRef(image, image.nativeImage), @@ -1727,8 +1847,10 @@ public void DrawImageUnscaled(Image image, int x, int y, int width, int height) DrawImage(image, x, y); } - public void DrawImageUnscaledAndClipped(Image image!!, Rectangle rect) + public void DrawImageUnscaledAndClipped(Image image, Rectangle rect) { + ArgumentNullException.ThrowIfNull(image); + int width = Math.Min(rect.Width, image.Width); int height = Math.Min(rect.Height, image.Height); @@ -1747,8 +1869,11 @@ public void DrawImageUnscaledAndClipped(Image image!!, Rectangle rect) // // @notes Perspective blt only works for bitmap images. - public unsafe void DrawImage(Image image!!, PointF[] destPoints!!) + public unsafe void DrawImage(Image image, PointF[] destPoints) { + ArgumentNullException.ThrowIfNull(image); + ArgumentNullException.ThrowIfNull(destPoints); + int count = destPoints.Length; if (count != 3 && count != 4) throw new ArgumentException(SR.GdiplusDestPointsInvalidLength); @@ -1765,8 +1890,11 @@ public unsafe void DrawImage(Image image!!, PointF[] destPoints!!) } } - public unsafe void DrawImage(Image image!!, Point[] destPoints!!) + public unsafe void DrawImage(Image image, Point[] destPoints) { + ArgumentNullException.ThrowIfNull(image); + ArgumentNullException.ThrowIfNull(destPoints); + int count = destPoints.Length; if (count != 3 && count != 4) throw new ArgumentException(SR.GdiplusDestPointsInvalidLength); @@ -1783,8 +1911,10 @@ public unsafe void DrawImage(Image image!!, Point[] destPoints!!) } } - public void DrawImage(Image image!!, float x, float y, RectangleF srcRect, GraphicsUnit srcUnit) + public void DrawImage(Image image, float x, float y, RectangleF srcRect, GraphicsUnit srcUnit) { + ArgumentNullException.ThrowIfNull(image); + int status = Gdip.GdipDrawImagePointRect( new HandleRef(this, NativeGraphics), new HandleRef(image, image.nativeImage), @@ -1796,8 +1926,10 @@ public void DrawImage(Image image!!, float x, float y, RectangleF srcRect, Graph CheckErrorStatus(status); } - public void DrawImage(Image image!!, int x, int y, Rectangle srcRect, GraphicsUnit srcUnit) + public void DrawImage(Image image, int x, int y, Rectangle srcRect, GraphicsUnit srcUnit) { + ArgumentNullException.ThrowIfNull(image); + int status = Gdip.GdipDrawImagePointRectI( new HandleRef(this, NativeGraphics), new HandleRef(image, image.nativeImage), @@ -1809,8 +1941,10 @@ public void DrawImage(Image image!!, int x, int y, Rectangle srcRect, GraphicsUn CheckErrorStatus(status); } - public void DrawImage(Image image!!, RectangleF destRect, RectangleF srcRect, GraphicsUnit srcUnit) + public void DrawImage(Image image, RectangleF destRect, RectangleF srcRect, GraphicsUnit srcUnit) { + ArgumentNullException.ThrowIfNull(image); + int status = Gdip.GdipDrawImageRectRect( new HandleRef(this, NativeGraphics), new HandleRef(image, image.nativeImage), @@ -1825,8 +1959,10 @@ public void DrawImage(Image image!!, RectangleF destRect, RectangleF srcRect, Gr CheckErrorStatus(status); } - public void DrawImage(Image image!!, Rectangle destRect, Rectangle srcRect, GraphicsUnit srcUnit) + public void DrawImage(Image image, Rectangle destRect, Rectangle srcRect, GraphicsUnit srcUnit) { + ArgumentNullException.ThrowIfNull(image); + int status = Gdip.GdipDrawImageRectRectI( new HandleRef(this, NativeGraphics), new HandleRef(image, image.nativeImage), @@ -1841,8 +1977,11 @@ public void DrawImage(Image image!!, Rectangle destRect, Rectangle srcRect, Grap CheckErrorStatus(status); } - public unsafe void DrawImage(Image image!!, PointF[] destPoints!!, RectangleF srcRect, GraphicsUnit srcUnit) + public unsafe void DrawImage(Image image, PointF[] destPoints, RectangleF srcRect, GraphicsUnit srcUnit) { + ArgumentNullException.ThrowIfNull(image); + ArgumentNullException.ThrowIfNull(destPoints); + int count = destPoints.Length; if (count != 3 && count != 4) throw new ArgumentException(SR.GdiplusDestPointsInvalidLength); @@ -1881,14 +2020,17 @@ public void DrawImage( } public unsafe void DrawImage( - Image image!!, - PointF[] destPoints!!, + Image image, + PointF[] destPoints, RectangleF srcRect, GraphicsUnit srcUnit, ImageAttributes? imageAttr, DrawImageAbort? callback, int callbackData) { + ArgumentNullException.ThrowIfNull(image); + ArgumentNullException.ThrowIfNull(destPoints); + int count = destPoints.Length; if (count != 3 && count != 4) throw new ArgumentException(SR.GdiplusDestPointsInvalidLength); @@ -1937,14 +2079,17 @@ public void DrawImage( } public unsafe void DrawImage( - Image image!!, - Point[] destPoints!!, + Image image, + Point[] destPoints, Rectangle srcRect, GraphicsUnit srcUnit, ImageAttributes? imageAttr, DrawImageAbort? callback, int callbackData) { + ArgumentNullException.ThrowIfNull(image); + ArgumentNullException.ThrowIfNull(destPoints); + int count = destPoints.Length; if (count != 3 && count != 4) throw new ArgumentException(SR.GdiplusDestPointsInvalidLength); @@ -2006,7 +2151,7 @@ public void DrawImage( } public void DrawImage( - Image image!!, + Image image, Rectangle destRect, float srcX, float srcY, @@ -2017,6 +2162,8 @@ public void DrawImage( DrawImageAbort? callback, IntPtr callbackData) { + ArgumentNullException.ThrowIfNull(image); + int status = Gdip.GdipDrawImageRectRect( new HandleRef(this, NativeGraphics), new HandleRef(image, image.nativeImage), @@ -2071,7 +2218,7 @@ public void DrawImage( } public void DrawImage( - Image image!!, + Image image, Rectangle destRect, int srcX, int srcY, @@ -2082,6 +2229,8 @@ public void DrawImage( DrawImageAbort? callback, IntPtr callbackData) { + ArgumentNullException.ThrowIfNull(image); + int status = Gdip.GdipDrawImageRectRectI( new HandleRef(this, NativeGraphics), new HandleRef(image, image.nativeImage), @@ -2107,8 +2256,11 @@ public void DrawLine(Pen pen, PointF pt1, PointF pt2) /// /// Draws a series of line segments that connect an array of points. /// - public unsafe void DrawLines(Pen pen!!, PointF[] points!!) + public unsafe void DrawLines(Pen pen, PointF[] points) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(points); + fixed (PointF* p = points) { CheckErrorStatus(Gdip.GdipDrawLines( @@ -2122,8 +2274,10 @@ public unsafe void DrawLines(Pen pen!!, PointF[] points!!) /// /// Draws a line connecting the two specified points. /// - public void DrawLine(Pen pen!!, int x1, int y1, int x2, int y2) + public void DrawLine(Pen pen, int x1, int y1, int x2, int y2) { + ArgumentNullException.ThrowIfNull(pen); + CheckErrorStatus(Gdip.GdipDrawLineI(new HandleRef(this, NativeGraphics), new HandleRef(pen, pen.NativePen), x1, y1, x2, y2)); } @@ -2138,8 +2292,11 @@ public void DrawLine(Pen pen, Point pt1, Point pt2) /// /// Draws a series of line segments that connect an array of points. /// - public unsafe void DrawLines(Pen pen!!, Point[] points!!) + public unsafe void DrawLines(Pen pen, Point[] points) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(points); + fixed (Point* p = points) { CheckErrorStatus(Gdip.GdipDrawLinesI( @@ -2365,8 +2522,10 @@ public void EnumerateMetafile( EnumerateMetafile(metafile, destPoints, srcRect, srcUnit, callback, callbackData, null); } - public unsafe void TransformPoints(CoordinateSpace destSpace, CoordinateSpace srcSpace, PointF[] pts!!) + public unsafe void TransformPoints(CoordinateSpace destSpace, CoordinateSpace srcSpace, PointF[] pts) { + ArgumentNullException.ThrowIfNull(pts); + fixed (PointF* p = pts) { Gdip.CheckStatus(Gdip.GdipTransformPoints( @@ -2378,8 +2537,10 @@ public unsafe void TransformPoints(CoordinateSpace destSpace, CoordinateSpace sr } } - public unsafe void TransformPoints(CoordinateSpace destSpace, CoordinateSpace srcSpace, Point[] pts!!) + public unsafe void TransformPoints(CoordinateSpace destSpace, CoordinateSpace srcSpace, Point[] pts) { + ArgumentNullException.ThrowIfNull(pts); + fixed (Point* p = pts) { Gdip.CheckStatus(Gdip.GdipTransformPointsI( @@ -2522,16 +2683,21 @@ public Color GetNearestColor(Color color) /// /// Draws a line connecting the two specified points. /// - public void DrawLine(Pen pen!!, float x1, float y1, float x2, float y2) + public void DrawLine(Pen pen, float x1, float y1, float x2, float y2) { + ArgumentNullException.ThrowIfNull(pen); + CheckErrorStatus(Gdip.GdipDrawLine(new HandleRef(this, NativeGraphics), new HandleRef(pen, pen.NativePen), x1, y1, x2, y2)); } /// /// Draws a series of cubic Bezier curves from an array of points. /// - public unsafe void DrawBeziers(Pen pen!!, PointF[] points!!) + public unsafe void DrawBeziers(Pen pen, PointF[] points) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(points); + fixed (PointF* p = points) { CheckErrorStatus(Gdip.GdipDrawBeziers( @@ -2544,8 +2710,11 @@ public unsafe void DrawBeziers(Pen pen!!, PointF[] points!!) /// /// Draws a series of cubic Bezier curves from an array of points. /// - public unsafe void DrawBeziers(Pen pen!!, Point[] points!!) + public unsafe void DrawBeziers(Pen pen, Point[] points) { + ArgumentNullException.ThrowIfNull(pen); + ArgumentNullException.ThrowIfNull(points); + fixed (Point* p = points) { CheckErrorStatus(Gdip.GdipDrawBeziersI( @@ -2559,8 +2728,11 @@ public unsafe void DrawBeziers(Pen pen!!, Point[] points!!) /// /// Fills the interior of a path. /// - public void FillPath(Brush brush!!, GraphicsPath path!!) + public void FillPath(Brush brush, GraphicsPath path) { + ArgumentNullException.ThrowIfNull(brush); + ArgumentNullException.ThrowIfNull(path); + CheckErrorStatus(Gdip.GdipFillPath( new HandleRef(this, NativeGraphics), new HandleRef(brush, brush.NativeBrush), @@ -2570,16 +2742,21 @@ public void FillPath(Brush brush!!, GraphicsPath path!!) /// /// Fills the interior of a . /// - public void FillRegion(Brush brush!!, Region region!!) + public void FillRegion(Brush brush, Region region) { + ArgumentNullException.ThrowIfNull(brush); + ArgumentNullException.ThrowIfNull(region); + CheckErrorStatus(Gdip.GdipFillRegion( new HandleRef(this, NativeGraphics), new HandleRef(brush, brush.NativeBrush), new HandleRef(region, region.NativeRegion))); } - public void DrawIcon(Icon icon!!, int x, int y) + public void DrawIcon(Icon icon, int x, int y) { + ArgumentNullException.ThrowIfNull(icon); + if (_backingImage != null) { // We don't call the icon directly because we want to stay in GDI+ all the time @@ -2599,8 +2776,10 @@ public void DrawIcon(Icon icon!!, int x, int y) /// it passes the call to the actual image. This version crops the image to the given /// dimensions and allows the user to specify a rectangle within the image to draw. /// - public void DrawIcon(Icon icon!!, Rectangle targetRect) + public void DrawIcon(Icon icon, Rectangle targetRect) { + ArgumentNullException.ThrowIfNull(icon); + if (_backingImage != null) { // We don't call the icon directly because we want to stay in GDI+ all the time @@ -2620,8 +2799,10 @@ public void DrawIcon(Icon icon!!, Rectangle targetRect) /// it passes the call to the actual image. This version stretches the image to the given /// dimensions and allows the user to specify a rectangle within the image to draw. /// - public void DrawIconUnstretched(Icon icon!!, Rectangle targetRect) + public void DrawIconUnstretched(Icon icon, Rectangle targetRect) { + ArgumentNullException.ThrowIfNull(icon); + if (_backingImage != null) { DrawImageUnscaled(icon.ToBitmap(), targetRect); @@ -2697,11 +2878,13 @@ public void EnumerateMetafile( public unsafe void EnumerateMetafile( Metafile metafile, - PointF[] destPoints!!, + PointF[] destPoints, EnumerateMetafileProc callback, IntPtr callbackData, ImageAttributes? imageAttr) { + ArgumentNullException.ThrowIfNull(destPoints); + if (destPoints.Length != 3) throw new ArgumentException(SR.GdiplusDestPointsInvalidParallelogram); @@ -2719,11 +2902,13 @@ public unsafe void EnumerateMetafile( public unsafe void EnumerateMetafile( Metafile metafile, - Point[] destPoints!!, + Point[] destPoints, EnumerateMetafileProc callback, IntPtr callbackData, ImageAttributes? imageAttr) { + ArgumentNullException.ThrowIfNull(destPoints); + if (destPoints.Length != 3) throw new ArgumentException(SR.GdiplusDestPointsInvalidParallelogram); @@ -2821,13 +3006,15 @@ public void EnumerateMetafile( public unsafe void EnumerateMetafile( Metafile metafile, - PointF[] destPoints!!, + PointF[] destPoints, RectangleF srcRect, GraphicsUnit unit, EnumerateMetafileProc callback, IntPtr callbackData, ImageAttributes? imageAttr) { + ArgumentNullException.ThrowIfNull(destPoints); + if (destPoints.Length != 3) throw new ArgumentException(SR.GdiplusDestPointsInvalidParallelogram); @@ -2847,13 +3034,15 @@ public unsafe void EnumerateMetafile( public unsafe void EnumerateMetafile( Metafile metafile, - Point[] destPoints!!, + Point[] destPoints, Rectangle srcRect, GraphicsUnit unit, EnumerateMetafileProc callback, IntPtr callbackData, ImageAttributes? imageAttr) { + ArgumentNullException.ThrowIfNull(destPoints); + if (destPoints.Length != 3) throw new ArgumentException(SR.GdiplusDestPointsInvalidParallelogram); @@ -3106,8 +3295,10 @@ public GraphicsContainer BeginContainer() return new GraphicsContainer(state); } - public void EndContainer(GraphicsContainer container!!) + public void EndContainer(GraphicsContainer container) { + ArgumentNullException.ThrowIfNull(container); + Gdip.CheckStatus(Gdip.GdipEndContainer(new HandleRef(this, NativeGraphics), container.nativeGraphicsContainer)); PopContext(container.nativeGraphicsContainer); } @@ -3131,8 +3322,10 @@ public GraphicsContainer BeginContainer(Rectangle dstrect, Rectangle srcrect, Gr return new GraphicsContainer(state); } - public void AddMetafileComment(byte[] data!!) + public void AddMetafileComment(byte[] data) { + ArgumentNullException.ThrowIfNull(data); + Gdip.CheckStatus(Gdip.GdipComment(new HandleRef(this, NativeGraphics), data.Length, data)); } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.cs index 885e2668c40f0..cb4c250cec555 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.cs @@ -82,8 +82,10 @@ public Icon(Icon original, Size size) : this(original, size.Width, size.Height) { } - public Icon(Icon original!!, int width, int height) : this() + public Icon(Icon original, int width, int height) : this() { + ArgumentNullException.ThrowIfNull(original); + _iconData = original._iconData; if (_iconData == null) @@ -97,8 +99,10 @@ public Icon(Icon original!!, int width, int height) : this() } } - public Icon(Type type, string resource!!) : this() + public Icon(Type type, string resource) : this() { + ArgumentNullException.ThrowIfNull(resource); + Stream? stream = type.Module.Assembly.GetManifestResourceStream(type, resource); if (stream == null) { @@ -118,8 +122,10 @@ public Icon(Stream stream, Size size) : this(stream, size.Width, size.Height) { } - public Icon(Stream stream!!, int width, int height) : this() + public Icon(Stream stream, int width, int height) : this() { + ArgumentNullException.ThrowIfNull(stream); + _iconData = new byte[(int)stream.Length]; stream.Read(_iconData, 0, _iconData.Length); Initialize(width, height); @@ -150,8 +156,9 @@ void ISerializable.GetObjectData(SerializationInfo si, StreamingContext context) public static Icon? ExtractAssociatedIcon(string filePath) => ExtractAssociatedIcon(filePath, 0); - private static unsafe Icon? ExtractAssociatedIcon(string filePath!!, int index) + private static unsafe Icon? ExtractAssociatedIcon(string filePath, int index) { + ArgumentNullException.ThrowIfNull(filePath); if (string.IsNullOrEmpty(filePath)) throw new ArgumentException(SR.NullOrEmptyPath, nameof(filePath)); diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Image.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Image.cs index ca782a8649f5f..cc4c37999206f 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Image.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Image.cs @@ -140,8 +140,10 @@ public static Image FromFile(string filename, bool useEmbeddedColorManagement) public static Image FromStream(Stream stream, bool useEmbeddedColorManagement) => FromStream(stream, useEmbeddedColorManagement, true); - public static Image FromStream(Stream stream!!, bool useEmbeddedColorManagement, bool validateImageData) + public static Image FromStream(Stream stream, bool useEmbeddedColorManagement, bool validateImageData) { + ArgumentNullException.ThrowIfNull(stream); + IntPtr image = LoadGdipImageFromStream(new GPStream(stream), useEmbeddedColorManagement); if (validateImageData) @@ -252,8 +254,10 @@ protected virtual void Dispose(bool disposing) /// /// Saves this to the specified file in the specified format. /// - public void Save(string filename, ImageFormat format!!) + public void Save(string filename, ImageFormat format) { + ArgumentNullException.ThrowIfNull(format); + ImageCodecInfo? codec = format.FindEncoder(); if (codec == null) @@ -265,8 +269,10 @@ public void Save(string filename, ImageFormat format!!) /// /// Saves this to the specified file in the specified format and with the specified encoder parameters. /// - public void Save(string filename!!, ImageCodecInfo encoder!!, EncoderParameters? encoderParams) + public void Save(string filename, ImageCodecInfo encoder, EncoderParameters? encoderParams) { + ArgumentNullException.ThrowIfNull(filename); + ArgumentNullException.ThrowIfNull(encoder); ThrowIfDirectoryDoesntExist(filename); IntPtr encoderParamsMemory = IntPtr.Zero; @@ -329,8 +335,10 @@ private void Save(MemoryStream stream) /// /// Saves this to the specified stream in the specified format. /// - public void Save(Stream stream, ImageFormat format!!) + public void Save(Stream stream, ImageFormat format) { + ArgumentNullException.ThrowIfNull(format); + ImageCodecInfo codec = format.FindEncoder()!; Save(stream, codec, null); } @@ -338,8 +346,11 @@ public void Save(Stream stream, ImageFormat format!!) /// /// Saves this to the specified stream in the specified format. /// - public void Save(Stream stream!!, ImageCodecInfo encoder!!, EncoderParameters? encoderParams) + public void Save(Stream stream, ImageCodecInfo encoder, EncoderParameters? encoderParams) { + ArgumentNullException.ThrowIfNull(stream); + ArgumentNullException.ThrowIfNull(encoder); + IntPtr encoderParamsMemory = IntPtr.Zero; if (encoderParams != null) @@ -412,8 +423,10 @@ public void SaveAdd(EncoderParameters? encoderParams) /// /// Adds an to the specified . /// - public void SaveAdd(Image image!!, EncoderParameters? encoderParams) + public void SaveAdd(Image image, EncoderParameters? encoderParams) { + ArgumentNullException.ThrowIfNull(image); + IntPtr encoder = IntPtr.Zero; if (encoderParams != null) diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/Metafile.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/Metafile.cs index 245127d606417..577761e242fd0 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/Metafile.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Imaging/Metafile.cs @@ -314,8 +314,10 @@ public Metafile(IntPtr hmetafile, WmfPlaceableFileHeader wmfHeader) : /// /// Initializes a new instance of the class from the specified stream. /// - public unsafe Metafile(Stream stream!!) + public unsafe Metafile(Stream stream) { + ArgumentNullException.ThrowIfNull(stream); + using DrawingCom.IStreamWrapper streamWrapper = DrawingCom.GetComWrapper(new GPStream(stream)); IntPtr metafile = IntPtr.Zero; diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Internal/GPStream.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Internal/GPStream.cs index 4758abd0ec59a..524be173b52cf 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Internal/GPStream.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Internal/GPStream.cs @@ -129,8 +129,13 @@ public void SetSize(ulong value) _dataStream.SetLength(checked((long)value)); } - public unsafe void Stat(Interop.Ole32.STATSTG* pstatstg!!, Interop.Ole32.STATFLAG grfStatFlag) + public unsafe void Stat(Interop.Ole32.STATSTG* pstatstg, Interop.Ole32.STATFLAG grfStatFlag) { + if (pstatstg == null) + { + throw new ArgumentNullException(nameof(pstatstg)); + } + *pstatstg = new Interop.Ole32.STATSTG { cbSize = (ulong)_dataStream.Length, diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Pen.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Pen.cs index 445a8ca92f750..dceda4189f6e1 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Pen.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Pen.cs @@ -77,8 +77,10 @@ public Pen(Brush brush) : this(brush, (float)1.0) /// /// Initializes a new instance of the class with the specified and width. /// - public Pen(Brush brush!!, float width) + public Pen(Brush brush, float width) { + ArgumentNullException.ThrowIfNull(brush); + IntPtr pen; int status = Gdip.GdipCreatePen2(new HandleRef(brush, brush.NativeBrush), width, diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/MarginsConverter.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/MarginsConverter.cs index feb3eabbadc54..42266a7e178ef 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/MarginsConverter.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/MarginsConverter.cs @@ -91,8 +91,10 @@ public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen( /// type is string. If this cannot convert to the desitnation type, this will /// throw a NotSupportedException. /// - public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType!!) + public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType) { + ArgumentNullException.ThrowIfNull(destinationType); + if (value is Margins margins) { if (destinationType == typeof(string)) @@ -140,8 +142,10 @@ public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen( /// for the object. This is useful for objects that are immutable, but still /// want to provide changable properties. /// - public override object CreateInstance(ITypeDescriptorContext? context, IDictionary propertyValues!!) + public override object CreateInstance(ITypeDescriptorContext? context, IDictionary propertyValues) { + ArgumentNullException.ThrowIfNull(propertyValues); + object? left = propertyValues["Left"]; object? right = propertyValues["Right"]; object? top = propertyValues["Top"]; diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Region.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Region.cs index 88275abb723b0..f7ff48efb3dd1 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Region.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Region.cs @@ -36,14 +36,18 @@ public Region(Rectangle rect) SetNativeRegion(region); } - public Region(GraphicsPath path!!) + 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!!) + public Region(RegionData rgnData) { + ArgumentNullException.ThrowIfNull(rgnData); + Gdip.CheckStatus(Gdip.GdipCreateRegionRgnData( rgnData.Data, rgnData.Data.Length, @@ -139,13 +143,17 @@ public void Intersect(Rectangle rect) Gdip.CheckStatus(Gdip.GdipCombineRegionRectI(new HandleRef(this, NativeRegion), ref rect, CombineMode.Intersect)); } - public void Intersect(GraphicsPath path!!) + 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!!) + public void Intersect(Region region) { + ArgumentNullException.ThrowIfNull(region); + Gdip.CheckStatus(Gdip.GdipCombineRegionRegion(new HandleRef(this, NativeRegion), new HandleRef(region, region.NativeRegion), CombineMode.Intersect)); } @@ -159,13 +167,17 @@ public void Union(Rectangle rect) Gdip.CheckStatus(Gdip.GdipCombineRegionRectI(new HandleRef(this, NativeRegion), ref rect, CombineMode.Union)); } - public void Union(GraphicsPath path!!) + 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!!) + public void Union(Region region) { + ArgumentNullException.ThrowIfNull(region); + Gdip.CheckStatus(Gdip.GdipCombineRegionRegion(new HandleRef(this, NativeRegion), new HandleRef(region, region.NativeRegion), CombineMode.Union)); } @@ -179,13 +191,17 @@ public void Xor(Rectangle rect) Gdip.CheckStatus(Gdip.GdipCombineRegionRectI(new HandleRef(this, NativeRegion), ref rect, CombineMode.Xor)); } - public void Xor(GraphicsPath path!!) + 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!!) + public void Xor(Region region) { + ArgumentNullException.ThrowIfNull(region); + Gdip.CheckStatus(Gdip.GdipCombineRegionRegion(new HandleRef(this, NativeRegion), new HandleRef(region, region.NativeRegion), CombineMode.Xor)); } @@ -199,16 +215,20 @@ public void Exclude(Rectangle rect) Gdip.CheckStatus(Gdip.GdipCombineRegionRectI(new HandleRef(this, NativeRegion), ref rect, CombineMode.Exclude)); } - public void Exclude(GraphicsPath path!!) + 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!!) + public void Exclude(Region region) { + ArgumentNullException.ThrowIfNull(region); + Gdip.CheckStatus(Gdip.GdipCombineRegionRegion( new HandleRef(this, NativeRegion), new HandleRef(region, region.NativeRegion), @@ -225,13 +245,17 @@ public void Complement(Rectangle rect) Gdip.CheckStatus(Gdip.GdipCombineRegionRectI(new HandleRef(this, NativeRegion), ref rect, CombineMode.Complement)); } - public void Complement(GraphicsPath path!!) + 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!!) + public void Complement(Region region) { + ArgumentNullException.ThrowIfNull(region); + Gdip.CheckStatus(Gdip.GdipCombineRegionRegion(new HandleRef(this, NativeRegion), new HandleRef(region, region.NativeRegion), CombineMode.Complement)); } @@ -245,39 +269,52 @@ public void Translate(int dx, int dy) Gdip.CheckStatus(Gdip.GdipTranslateRegionI(new HandleRef(this, NativeRegion), dx, dy)); } - public void Transform(Matrix matrix!!) + 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!!) + 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!!) + 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!!) + 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!!) + 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!!) + 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; } @@ -360,8 +397,10 @@ public bool IsVisible(Rectangle rect, Graphics? g) return isVisible != 0; } - public unsafe RectangleF[] GetRegionScans(Matrix matrix!!) + public unsafe RectangleF[] GetRegionScans(Matrix matrix) { + ArgumentNullException.ThrowIfNull(matrix); + Gdip.CheckStatus(Gdip.GdipGetRegionScansCount( new HandleRef(this, NativeRegion), out int count, diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/StringFormat.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/StringFormat.cs index fc910b3ce9589..be9245ae46ea1 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/StringFormat.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/StringFormat.cs @@ -53,8 +53,10 @@ public StringFormat(StringFormatFlags options, int language) /// Initializes a new instance of the class from the specified /// existing . /// - public StringFormat(StringFormat format!!) + public StringFormat(StringFormat format) { + ArgumentNullException.ThrowIfNull(format); + int status = Gdip.GdipCloneStringFormat(new HandleRef(format, format.nativeFormat), out nativeFormat); if (status != Gdip.Ok) diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/TextureBrush.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/TextureBrush.cs index 9835d63d78ca1..14ac17ed03d0a 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/TextureBrush.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/TextureBrush.cs @@ -22,8 +22,10 @@ public TextureBrush(Image bitmap) : this(bitmap, WrapMode.Tile) { } - public TextureBrush(Image image!!, WrapMode wrapMode) + public TextureBrush(Image image, WrapMode wrapMode) { + ArgumentNullException.ThrowIfNull(image); + if (wrapMode < WrapMode.Tile || wrapMode > WrapMode.Clamp) { throw new InvalidEnumArgumentException(nameof(wrapMode), unchecked((int)wrapMode), typeof(WrapMode)); @@ -38,8 +40,10 @@ public TextureBrush(Image image!!, WrapMode wrapMode) SetNativeBrushInternal(brush); } - public TextureBrush(Image image!!, WrapMode wrapMode, RectangleF dstRect) + public TextureBrush(Image image, WrapMode wrapMode, RectangleF dstRect) { + ArgumentNullException.ThrowIfNull(image); + if (wrapMode < WrapMode.Tile || wrapMode > WrapMode.Clamp) { throw new InvalidEnumArgumentException(nameof(wrapMode), unchecked((int)wrapMode), typeof(WrapMode)); @@ -58,8 +62,10 @@ public TextureBrush(Image image!!, WrapMode wrapMode, RectangleF dstRect) SetNativeBrushInternal(brush); } - public TextureBrush(Image image!!, WrapMode wrapMode, Rectangle dstRect) + public TextureBrush(Image image, WrapMode wrapMode, Rectangle dstRect) { + ArgumentNullException.ThrowIfNull(image); + if (wrapMode < WrapMode.Tile || wrapMode > WrapMode.Clamp) { throw new InvalidEnumArgumentException(nameof(wrapMode), unchecked((int)wrapMode), typeof(WrapMode)); @@ -80,8 +86,10 @@ public TextureBrush(Image image!!, WrapMode wrapMode, Rectangle dstRect) public TextureBrush(Image image, RectangleF dstRect) : this(image, dstRect, null) { } - public TextureBrush(Image image!!, RectangleF dstRect, ImageAttributes? imageAttr) + public TextureBrush(Image image, RectangleF dstRect, ImageAttributes? imageAttr) { + ArgumentNullException.ThrowIfNull(image); + IntPtr brush; int status = Gdip.GdipCreateTextureIA(new HandleRef(image, image.nativeImage), new HandleRef(imageAttr, (imageAttr == null) ? @@ -98,8 +106,10 @@ public TextureBrush(Image image!!, RectangleF dstRect, ImageAttributes? imageAtt public TextureBrush(Image image, Rectangle dstRect) : this(image, dstRect, null) { } - public TextureBrush(Image image!!, Rectangle dstRect, ImageAttributes? imageAttr) + public TextureBrush(Image image, Rectangle dstRect, ImageAttributes? imageAttr) { + ArgumentNullException.ThrowIfNull(image); + IntPtr brush; int status = Gdip.GdipCreateTextureIAI(new HandleRef(image, image.nativeImage), new HandleRef(imageAttr, (imageAttr == null) ? @@ -193,8 +203,10 @@ public void ResetTransform() public void MultiplyTransform(Matrix matrix) => MultiplyTransform(matrix, MatrixOrder.Prepend); - public void MultiplyTransform(Matrix matrix!!, MatrixOrder order) + 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 compatability with GDI+. if (matrix.NativeMatrix == IntPtr.Zero)