C# GDI+是Windows操作系统上的一种图形处理库,提供了绘制各种形状的函数,其中就包括绘制扇形的函数。通过使用GDI+,我们可以很方便地在窗体或者其他控件上绘制扇形,实现各种实用的功能。
除了绘制扇形之外,GDI+还提供了其他各种形状的绘制函数,比如线条、矩形、多边形等等。通过这些函数,我们可以轻松地实现各种图形的绘制和处理,为开发者提供了非常便利的工具。
当然,GDI+也有一些限制和缺陷。比如,它只能在Windows操作系统上使用,不能跨平台使用;同时,它的性能和渲染质量也不如现代的图形库,比如WPF和Unity等。但是,对于一些简单的图形处理需求,GDI+仍然是一个非常好用的工具,值得我们掌握和使用。
DrawPie(Pen, Single, Single, Single, Single, Single, Single) | 绘制一个扇形,该形状由一个坐标对、宽度、高度以及两条射线所指定的椭圆定义。 |
DrawPie(Pen, Rectangle, Single, Single) | 绘制由一个 Rectangle 结构和两条射线所指定的椭圆定义的扇形。 |
DrawPie(Pen, RectangleF, Single, Single) | 绘制由一个 RectangleF 结构和两条射线所指定的椭圆定义的扇形。 |
DrawPie(Pen, Int32, Int32, Int32, Int32, Int32, Int32) | 绘制一个 |
一个例子
C#protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Pen pen = new Pen(Color.Black, 3);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.DrawPie(pen, new RectangleF(0, 0, this.Width, this.Height - 50), 45, 270);
}
一个例子
C#System.Timers.Timer timer = new System.Timers.Timer();
float _endAngle = 0;
bool _direction = false;
public Form1()
{
InitializeComponent();
this.DoubleBuffered= true;
timer.Interval = 100;
timer.Elapsed += Timer_Elapsed;
timer.Start();
}
private void Timer_Elapsed(object? sender, System.Timers.ElapsedEventArgs e)
{
if (_endAngle < 180 && _direction==false)
{
_endAngle++;
this.Invalidate();
if (_endAngle == 180)
{
_direction = true;
}
}
if (_endAngle>0 && _direction)
{
_endAngle--;
this.Invalidate();
if (_endAngle == 0)
{
_direction = false;
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Pen pen = new Pen(Color.Black, 3);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Rectangle rect = new Rectangle((this.Width - 300) / 2, (this.Height - 300) / 2, 300, 300);
float startAngle = 180;
//从 startAngle 参数到扇形的第二条边沿顺时针方向度量的角(以度为单位)。
float endAngle = 180;
e.Graphics.DrawPie(pen, rect, startAngle, endAngle);
e.Graphics.FillPie(new SolidBrush(Color.DarkBlue), rect, startAngle, endAngle);
rect = new Rectangle((this.Width - 300) / 2, (this.Height - 300) / 2, 300, 300);
startAngle = 180;
endAngle = _endAngle;
e.Graphics.DrawPie(pen, rect, startAngle, endAngle);
e.Graphics.FillPie(new SolidBrush(Color.AliceBlue), rect, startAngle, endAngle);
}
一个例子
C#protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Pen pen = new Pen(Color.Black, 3);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
int x = (this.Width - 300) / 2;
int y = (this.Height - 300) / 2;
Rectangle rect = new Rectangle(x, y, 300, 300);
float startAngle = 180;
//从 startAngle 参数到扇形的第二条边沿顺时针方向度量的角(以度为单位)。
float endAngle = 180;
e.Graphics.DrawPie(pen, rect, startAngle, endAngle);
e.Graphics.FillPie(new SolidBrush(Color.DarkBlue), rect, startAngle, endAngle);
rect = new Rectangle(x, y, 300, 300);
startAngle = 180;
endAngle = _endAngle;
e.Graphics.DrawPie(pen, rect, startAngle, endAngle);
e.Graphics.FillPie(new SolidBrush(Color.AliceBlue), rect, startAngle, endAngle);
int count = 180;
var radians = (Math.PI/180) * Math.Round(360.0 / count);
int r = 150;
for (int i = 0; i < count; i++)
{
double px = x + 150 + r * Math.Cos(radians * i);
double py = y + 150 + r * Math.Sin(radians * i);
e.Graphics.DrawRectangle(new Pen(Color.Red), Convert.ToInt32(px), Convert.ToInt32(py), 2, 2);
}
}
本文作者:rick
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!