<meta name="wolai" content="dDWcdCV1EWBV44ZzciXDte1760476823475">
摘要
在計算機(jī)圖形學(xué)中,使用 GDI(圖形設(shè)備接口)是一種繪制圖形的方法。它是一種基于設(shè)備的方法,可以通過控制設(shè)備的像素來創(chuàng)建圖像。使用 GDI 繪制圖形是許多應(yīng)用程序中必不可少的一部分。
在 C# 中,使用 GDI 可以輕松地繪制圖形。要繪制一條線條,可以使用 Line 類。Line 類定義了一個由兩個點組成的直線。在創(chuàng)建 Line 對象時,需要指定兩個坐標(biāo)點,這兩個點將決定直線的位置和方向。
繪制完成后,我們需要釋放資源。Graphics 對象和 Pen 對象都是在創(chuàng)建時分配內(nèi)存的,因此在不需要它們時應(yīng)該及時釋放,以避免內(nèi)存泄漏。
正文
重載
| DrawLine(Pen, PointF, PointF) | 繪制一條連接兩個 PointF 結(jié)構(gòu)的線。 |
| DrawLine(Pen, Int32, Int32, Int32, Int32) | 繪制一條連接由坐標(biāo)對指定的兩個點的線條。 |
| DrawLine(Pen, Single, Single, Single, Single) | 繪制一條連接由坐標(biāo)對指定的兩個點的線條。 |
| DrawLine(Pen, Point, Point) | 繪制一條 |
一個例子
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//抗鋸齒
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Pen pen =new Pen(Color.Red, 2);
e.Graphics.DrawLine(pen, 30, 20, 200, 200);
}

image.png
使用Point
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//抗鋸齒
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Pen pen = new Pen(Color.Red, 2);
Point p1 = new Point(30, 30);
Point p2 = new Point(200, 300);
e.Graphics.DrawLine(pen, p1, p2);
}
使用刷子
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//抗鋸齒
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Brush brush = new HatchBrush(HatchStyle.DottedGrid, Color.DarkRed);
Pen pen = new Pen(brush, 20);
Point p1 = new Point(30, 30);
Point p2 = new Point(200, 300);
e.Graphics.DrawLine(pen, p1, p2);
}

image.png
動畫樣式
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
timer.Interval = 100;
timer.Elapsed += Timer_Elapsed;
brush = new LinearGradientBrush(new Point(0, 0), new Point(10, 10), Color.Red, Color.RoyalBlue);
}
private void Form1_Load(object sender, EventArgs e)
{
timer.Enabled = true;
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (isRun)
{
brush = new LinearGradientBrush(new Point(0, 0), new Point(10, 10), Color.Red, Color.RoyalBlue);
isRun = false;
}
else
{
brush = new LinearGradientBrush(new Point(0, 0), new Point(10, 10), Color.RoyalBlue, Color.Red);
isRun = true;
}
this.Invoke(new Action(this.Refresh));
}
LinearGradientBrush brush;
System.Timers.Timer timer = new System.Timers.Timer();
bool isRun = false;
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//抗鋸齒
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Pen pen = new Pen(brush, 20);
e.Graphics.DrawLine(pen, new Point(0, 0), new Point(this.Width, 0));
e.Graphics.DrawLine(pen, new Point(0, 0), new Point(0, this.Height));
e.Graphics.DrawLine(pen, new Point(0, this.Height-40), new Point(this.Width, this.Height- 40));
e.Graphics.DrawLine(pen, new Point(this.Width-20,0), new Point(this.Width-20, this.Height));
this.Update();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
this.Text = e.Location.X + ":" + e.Location.Y;
}
}

image.png