C# Graphics.FillEllipse 方法

本文介绍如何使用.NET Framework中的Graphics.FillEllipse方法绘制椭圆。该方法通过指定的画笔和椭圆边界参数填充椭圆。文章还提供了一个简单的示例代码,演示了如何在屏幕上绘制一个实心红色椭圆。

Graphics.FillEllipse 方法 (Brush, Int32, Int32, Int32, Int32)

.NET Framework (current version)
 

填充边框所定义的椭圆的内部,该边框由一对坐标、一个宽度和一个高度指定。

命名空间:   System.Drawing
程序集:  System.Drawing(位于 System.Drawing.dll)

C#
C++
F#
VB
public void FillEllipse(
	Brush brush,
	int x,
	int y,
	int width,
	int height
)
参数
brush
Type: System.Drawing.Brush

确定填充特性的 Brush

x
Type: System.Int32

定义椭圆的边框的左上角的 X 坐标。

y
Type: System.Int32

定义椭圆的边框的左上角的 Y 坐标。

width
Type: System.Int32

定义椭圆的边框的宽度。

height
Type: System.Int32

定义椭圆的边框的高度。

ExceptionCondition
ArgumentNullException

brushnull

此方法填充与的椭圆的内部 Brush 由该边框由表示定义椭圆 x, ,y, ,width, ,和height 参数。

下面的代码示例仅用于 Windows 窗体,并且它要求在 PaintEventArgse, ,这是一个参数的 Paint 事件处理程序。 该代码执行下列操作:

  • 创建实心的红色画笔。

  • 创建的位置和大小来限定椭圆的矩形。

  • 填充椭圆在屏幕上。

C#
C++
VB
public void FillEllipseInt(PaintEventArgs e)
{

    // Create solid brush.
    SolidBrush redBrush = new SolidBrush(Color.Red);

    // Create location and size of ellipse.
    int x = 0;
    int y = 0;
    int width = 200;
    int height = 100;

    // Fill ellipse on screen.
    e.Graphics.FillEllipse(redBrush, x, y, width, height);
}

.NET Framework
自 1.1 起可用
private void pictureBox1_Paint(object sender, PaintEventArgs e) { if (currentTransform != null) { e.Graphics.Transform = currentTransform; // 直接应用预计算矩阵 } e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; // 计算实际绘制区域 var clientRect = pictureBox1.ClientRectangle; // 自动计算数据边界(需要预先获取points的实际范围) float dataMinX = points.Min(p => p.fiveX); float dataMaxX = points.Max(p => p.fiveX); float dataMinY = points.Min(p => p.sexY); float dataMaxY = points.Max(p => p.sexY); // 计算缩放比例 float scaleX = clientRect.Width / (dataMaxX - dataMinX); float scaleY = clientRect.Height / (dataMaxY - dataMinY); float zoomLevel = Math.Min(scaleX, scaleY) * 0.9f; // 保留10%边距 // 计算偏移量 float offsetX = -dataMinX * zoomLevel + (clientRect.Width - (dataMaxX - dataMinX) * zoomLevel) / 2; float offsetY = -dataMinY * zoomLevel + (clientRect.Height - (dataMaxY - dataMinY) * zoomLevel) / 2; e.Graphics.TranslateTransform(offsetX, offsetY); e.Graphics.ScaleTransform(zoomLevel, zoomLevel); currentTransform = e.Graphics.Transform; foreach (var point in points) { // 限制缩放级别最小值 float safeZoom = Math.Max(zoomLevel, 0.001f); float pointSize = Math.Min(4f / safeZoom, 100f); // 限制点尺寸最大值 // 计算坐标(确保用 float 运算) float blueX = point.fiveX - pointSize / 2; float blueY = point.sexY - pointSize / 2; // 检查坐标有效性 if (float.IsInfinity(blueX) || float.IsInfinity(blueY) || float.IsNaN(blueX) || float.IsNaN(blueY)) { continue; } // 绘制蓝色点 e.Graphics.FillEllipse(Brushes.Blue, blueX, blueY, pointSize, pointSize); // 同理处理红色点 float redX = point.eightX - pointSize / 2; float redY = point.nineY - pointSize / 2; if (float.IsInfinity(redX) || float.IsInfinity(redY) || float.IsNaN(redX) || float.IsNaN(redY)) { continue; } e.Graphics.FillEllipse(Brushes.Red, redX, redY, pointSize, pointSize); } if (currentTransform != null) { // 应用相同的变换矩阵 e.Graphics.Transform = currentTransform; // 在此绘制内容,坐标会自动适配 e.Graphics.DrawLine(Pens.Black, dataMinX, dataMinY, dataMaxX, dataMaxY); } }// 绘制蓝色点 e.Graphics.FillEllipse(Brushes.Blue, blueX, blueY, pointSize, pointSize); e.Graphics.FillEllipse(Brushes.Red, redX, redY, pointSize, pointSize);OverflowException 怎么解决
03-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值