Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added DrawRectangle overload accepting RectangleF #62385

Merged
merged 9 commits into from
Jan 11, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace System.Drawing
{
public sealed partial class Graphics
{
public void DrawRectangle(System.Drawing.Pen pen, System.Drawing.RectangleF rect) { }
public void FillPie(System.Drawing.Brush brush, System.Drawing.RectangleF rect, float startAngle, float sweepAngle) { }
public System.Numerics.Matrix3x2 TransformElements { get { throw null; } set { } }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
Expand Down
22 changes: 22 additions & 0 deletions src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,16 @@ 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>
medo64 marked this conversation as resolved.
Show resolved Hide resolved
/// <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>
Expand Down Expand Up @@ -1325,6 +1335,18 @@ public void FillPie(Brush brush, Rectangle rect, float startAngle, float sweepAn
FillPie(brush, rect.X, rect.Y, rect.Width, rect.Height, startAngle, sweepAngle);
}

/// <summary>
/// Fills the interior of a pie section defined by an ellipse and two radial lines.
/// </summary>
medo64 marked this conversation as resolved.
Show resolved Hide resolved
/// <param name="brush">A Brush that determines the characteristics of the fill.</param>
/// <param name="rect">A Rectangle structure that represents the bounding rectangle that defines the ellipse from which the pie section comes.</param>
/// <param name="startAngle">Angle in degrees measured clockwise from the x-axis to the first side of the pie section.</param>
/// <param name="sweepAngle">Angle in degrees measured clockwise from the <paramref name="startAngle"/> parameter to the second side of the pie section.</param>
public void FillPie(Brush brush, RectangleF rect, float startAngle, float sweepAngle)
{
FillPie(brush, rect.X, rect.Y, rect.Width, rect.Height, startAngle, sweepAngle);
}

/// <summary>
/// Fills the interior of a pie section defined by an ellipse and two radial lines.
/// </summary>
Expand Down
149 changes: 149 additions & 0 deletions src/libraries/System.Drawing.Common/tests/GraphicsTests.Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,154 @@ public void TransformElements_RoundTrip()
}
}
}

[ConditionalFact(Helpers.IsDrawingSupported)]
public void DrawRectangle_NullPen_ThrowsArgumentNullException_Core()
{
using (var image = new Bitmap(10, 10))
using (Graphics graphics = Graphics.FromImage(image))
{
AssertExtensions.Throws<ArgumentNullException>("pen", () => graphics.DrawRectangle(null, new RectangleF(0f, 0f, 1f, 1f)));
// other DrawRectangle overloads tested in DrawRectangle_NullPen_ThrowsArgumentNullException()
}
}

[ConditionalFact(Helpers.IsDrawingSupported)]
public void DrawRectangle_DisposedPen_ThrowsArgumentException_Core()
{
using (var image = new Bitmap(10, 10))
using (Graphics graphics = Graphics.FromImage(image))
{
var pen = new Pen(Color.Red);
pen.Dispose();

AssertExtensions.Throws<ArgumentException>(null, () => graphics.DrawRectangle(pen, new RectangleF(0f, 0f, 1f, 1f)));
// other DrawRectangle overloads tested in DrawRectangle_DisposedPen_ThrowsArgumentException()
}
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/22221", TestPlatforms.AnyUnix)]
[ConditionalFact(Helpers.IsDrawingSupported)]
public void DrawRectangle_Busy_ThrowsInvalidOperationException_Core()
{
using (var image = new Bitmap(10, 10))
using (Graphics graphics = Graphics.FromImage(image))
using (var pen = new Pen(Color.Red))
{
graphics.GetHdc();
try
{
Assert.Throws<InvalidOperationException>(() => graphics.DrawRectangle(pen, new RectangleF(0f, 0f, 1f, 1f)));
// other DrawRectangle overloads tested in DrawRectangle_Busy_ThrowsInvalidOperationException()
}
finally
{
graphics.ReleaseHdc();
}
}
}

[ConditionalFact(Helpers.IsDrawingSupported)]
public void DrawRectangle_Disposed_ThrowsArgumentException_Core()
{
using (var image = new Bitmap(10, 10))
using (var pen = new Pen(Color.Red))
{
Graphics graphics = Graphics.FromImage(image);
graphics.Dispose();

AssertExtensions.Throws<ArgumentException>(null, () => graphics.DrawRectangle(pen, new RectangleF(0f, 0f, 1f, 1f)));
// other DrawRectangle overloads tested in DrawRectangle_Disposed_ThrowsArgumentException()
}
}

[ConditionalFact(Helpers.IsDrawingSupported)]
public void FillPie_NullPen_ThrowsArgumentNullException_Core()
{
using (var image = new Bitmap(10, 10))
using (Graphics graphics = Graphics.FromImage(image))
{
AssertExtensions.Throws<ArgumentNullException>("brush", () => graphics.FillPie(null, new RectangleF(0, 0, 1, 1), 0, 90));
// other FillPie overloads tested in FillPie_NullPen_ThrowsArgumentNullException()
}
}

[ConditionalFact(Helpers.IsDrawingSupported)]
public void FillPie_DisposedPen_ThrowsArgumentException_Core()
{
using (var image = new Bitmap(10, 10))
using (Graphics graphics = Graphics.FromImage(image))
{
var brush = new SolidBrush(Color.Red);
brush.Dispose();

AssertExtensions.Throws<ArgumentException>(null, () => graphics.FillPie(brush, new RectangleF(0, 0, 1, 1), 0, 90));
// other FillPie overloads tested in FillPie_DisposedPen_ThrowsArgumentException()
}
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/22221", TestPlatforms.AnyUnix)]
[ConditionalFact(Helpers.IsDrawingSupported)]
public void FillPie_ZeroWidth_ThrowsArgumentException_Core()
{
using (var image = new Bitmap(10, 10))
using (Graphics graphics = Graphics.FromImage(image))
using (var brush = new SolidBrush(Color.Red))
{
AssertExtensions.Throws<ArgumentException>(null, () => graphics.FillPie(brush, new RectangleF(0, 0, 0, 1), 0, 90));
// other FillPie overloads tested in FillPie_ZeroWidth_ThrowsArgumentException()
}
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/22221", TestPlatforms.AnyUnix)]
[ConditionalFact(Helpers.IsDrawingSupported)]
public void FillPie_ZeroHeight_ThrowsArgumentException_Core()
{
using (var image = new Bitmap(10, 10))
using (Graphics graphics = Graphics.FromImage(image))
using (var brush = new SolidBrush(Color.Red))
{
AssertExtensions.Throws<ArgumentException>(null, () => graphics.FillPie(brush, new RectangleF(0, 0, 1, 0), 0, 90));
// other FillPie overloads tested in FillPie_ZeroHeight_ThrowsArgumentException()
}
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/22221", TestPlatforms.AnyUnix)]
[ConditionalFact(Helpers.IsDrawingSupported)]
public void FillPie_Busy_ThrowsInvalidOperationException_Core()
{
using (var image = new Bitmap(10, 10))
using (Graphics graphics = Graphics.FromImage(image))
using (var brush = new SolidBrush(Color.Red))
{
graphics.GetHdc();
try
{
Assert.Throws<InvalidOperationException>(() => graphics.FillPie(brush, new RectangleF(0, 0, 1, 1), 0, 90));
// other FillPie overloads tested in FillPie_Busy_ThrowsInvalidOperationException()
}
finally
{
graphics.ReleaseHdc();
}
}
}

[ConditionalFact(Helpers.IsDrawingSupported)]
public void FillPie_Disposed_ThrowsArgumentException_Core()
{
using (var image = new Bitmap(10, 10))
using (var brush = new SolidBrush(Color.Red))
{
Graphics graphics = Graphics.FromImage(image);
graphics.Dispose();

AssertExtensions.Throws<ArgumentException>(null, () => graphics.FillPie(brush, new RectangleF(0, 0, 1, 1), 0, 90));
// other FillPie overloads tested in FillPie_Disposed_ThrowsArgumentException()
}
}



}
}
93 changes: 92 additions & 1 deletion src/libraries/System.Drawing.Common/tests/GraphicsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2399,7 +2399,6 @@ public void DrawRectangle_DisposedPen_ThrowsArgumentException()
var pen = new Pen(Color.Red);
pen.Dispose();


AssertExtensions.Throws<ArgumentException>(null, () => graphics.DrawRectangle(pen, new Rectangle(0, 0, 1, 1)));
AssertExtensions.Throws<ArgumentException>(null, () => graphics.DrawRectangle(pen, 0, 0, 1, 1));
AssertExtensions.Throws<ArgumentException>(null, () => graphics.DrawRectangle(pen, 0f, 0f, 1f, 1f));
Expand Down Expand Up @@ -3099,6 +3098,98 @@ public void DrawClosedCurve_Disposed_ThrowsArgumentException()
}
}

[ConditionalFact(Helpers.IsDrawingSupported)]
public void FillPie_NullPen_ThrowsArgumentNullException()
{
using (var image = new Bitmap(10, 10))
using (Graphics graphics = Graphics.FromImage(image))
{
AssertExtensions.Throws<ArgumentNullException>("brush", () => graphics.FillPie(null, new Rectangle(0, 0, 1, 1), 0, 90));
AssertExtensions.Throws<ArgumentNullException>("brush", () => graphics.FillPie(null, 0, 0, 1, 1, 0, 90));
AssertExtensions.Throws<ArgumentNullException>("brush", () => graphics.FillPie(null, 0f, 0f, 1f, 1f, 0, 90));
}
}

[ConditionalFact(Helpers.IsDrawingSupported)]
public void FillPie_DisposedPen_ThrowsArgumentException()
{
using (var image = new Bitmap(10, 10))
using (Graphics graphics = Graphics.FromImage(image))
{
var brush = new SolidBrush(Color.Red);
brush.Dispose();

AssertExtensions.Throws<ArgumentException>(null, () => graphics.FillPie(brush, new Rectangle(0, 0, 1, 1), 0, 90));
AssertExtensions.Throws<ArgumentException>(null, () => graphics.FillPie(brush, 0, 0, 1, 1, 0, 90));
AssertExtensions.Throws<ArgumentException>(null, () => graphics.FillPie(brush, 0f, 0f, 1f, 1f, 0, 90));
}
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/22221", TestPlatforms.AnyUnix)]
[ConditionalFact(Helpers.IsDrawingSupported)]
public void FillPie_ZeroWidth_ThrowsArgumentException()
{
using (var image = new Bitmap(10, 10))
using (Graphics graphics = Graphics.FromImage(image))
using (var brush = new SolidBrush(Color.Red))
{
AssertExtensions.Throws<ArgumentException>(null, () => graphics.FillPie(brush, new Rectangle(0, 0, 0, 1), 0, 90));
AssertExtensions.Throws<ArgumentException>(null, () => graphics.FillPie(brush, 0, 0, 0, 1, 0, 90));
AssertExtensions.Throws<ArgumentException>(null, () => graphics.FillPie(brush, 0f, 0f, 0f, 1f, 0, 90));
}
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/22221", TestPlatforms.AnyUnix)]
[ConditionalFact(Helpers.IsDrawingSupported)]
public void FillPie_ZeroHeight_ThrowsArgumentException()
{
using (var image = new Bitmap(10, 10))
using (Graphics graphics = Graphics.FromImage(image))
using (var brush = new SolidBrush(Color.Red))
{
AssertExtensions.Throws<ArgumentException>(null, () => graphics.FillPie(brush, new Rectangle(0, 0, 1, 0), 0, 90));
AssertExtensions.Throws<ArgumentException>(null, () => graphics.FillPie(brush, 0, 0, 1, 0, 0, 90));
AssertExtensions.Throws<ArgumentException>(null, () => graphics.FillPie(brush, 0f, 0f, 1f, 0f, 0, 90));
}
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/22221", TestPlatforms.AnyUnix)]
[ConditionalFact(Helpers.IsDrawingSupported)]
public void FillPie_Busy_ThrowsInvalidOperationException()
{
using (var image = new Bitmap(10, 10))
using (Graphics graphics = Graphics.FromImage(image))
using (var brush = new SolidBrush(Color.Red))
{
graphics.GetHdc();
try
{
Assert.Throws<InvalidOperationException>(() => graphics.FillPie(brush, new Rectangle(0, 0, 1, 1), 0, 90));
Assert.Throws<InvalidOperationException>(() => graphics.FillPie(brush, 0, 0, 1, 1, 0, 90));
Assert.Throws<InvalidOperationException>(() => graphics.FillPie(brush, 0f, 0f, 1f, 1f, 0, 90));
}
finally
{
graphics.ReleaseHdc();
}
}
}

[ConditionalFact(Helpers.IsDrawingSupported)]
public void FillPie_Disposed_ThrowsArgumentException()
{
using (var image = new Bitmap(10, 10))
using (var brush = new SolidBrush(Color.Red))
{
Graphics graphics = Graphics.FromImage(image);
graphics.Dispose();

AssertExtensions.Throws<ArgumentException>(null, () => graphics.FillPie(brush, new Rectangle(0, 0, 1, 1), 0, 90));
AssertExtensions.Throws<ArgumentException>(null, () => graphics.FillPie(brush, 0, 0, 1, 1, 0, 90));
AssertExtensions.Throws<ArgumentException>(null, () => graphics.FillPie(brush, 0f, 0f, 1f, 1f, 0, 90));
}
}

[ConditionalFact(Helpers.IsDrawingSupported)]
public void Clear_Color_Success()
{
Expand Down