编辑
2025-02-03
C# 应用
00
请注意,本文编写于 92 天前,最后修改于 92 天前,其中某些信息可能已经过时。

目录

摘要
正文

摘要

在计算机图形学中,使用 GDI(图形设备接口)是一种绘制图形的方法。它是一种基于设备的方法,可以通过控制设备的像素来创建图像。使用 GDI 绘制图形是许多应用程序中必不可少的一部分。

在 C# 中,使用 GDI 可以轻松地绘制图形。要绘制一条线条,可以使用 Line 类。Line 类定义了一个由两个点组成的直线。在创建 Line 对象时,需要指定两个坐标点,这两个点将决定直线的位置和方向。

绘制完成后,我们需要释放资源。Graphics 对象和 Pen 对象都是在创建时分配内存的,因此在不需要它们时应该及时释放,以避免内存泄漏。

正文

重载

DrawLine(Pen, PointF, PointF)绘制一条连接两个 PointF 结构的线。
DrawLine(Pen, Int32, Int32, Int32, Int32)绘制一条连接由坐标对指定的两个点的线条。
DrawLine(Pen, Single, Single, Single, Single)绘制一条连接由坐标对指定的两个点的线条。
DrawLine(Pen, Point, Point)绘制一条

一个例子

C#
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

C#
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); }

使用刷子

C#
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

动画样式

C#
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

本文作者:rick

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!