C#绘图基础

来源:互联网 发布:overlay网络百度百科 编辑:程序博客网 时间:2024/05/22 06:30

首先先了解什么是 GDI ?GDI 是从 Windows 95 到 Windows 2000 随附的旧版绘图装置接口 (Graphics Device Interface), 是属于绘图方面的 API (Application Programming Interface)。因为应用程序不能直接控制硬件, 所以当我们要进行绘图的动作时, 必须透过 GDI 才能完成。

那 GDI+ 又是什么呢?GDI+ 是 GDI 的后续产品, 是一种绘图装置接口, 可将应用程序和绘图硬件分隔, 让我们能够撰写与装置无关的应用程序。它可以让我们不需注意特定显示装置的详细数据, 便可在屏幕或打印机显示信息。我们可以呼叫 GDI+ 类别所提供的方法, 然后这些方法会适当地呼叫特定的装置驱动程序, 而完成绘图。

下面我将开始逐步讲解一些C#绘图知识


1.简单的画线条、矩形、椭圆、画刷和文字添加

新建一个Windows窗体应用程序,命名为GDItest,选择窗体的Paint事件,开始编写:

 private void Form1_Paint(object sender, PaintEventArgs e)        {            Graphics g = e.Graphics;  //创建一个画图设备            Pen p = new Pen(Color.Red, 5);//定义了一个红色,宽度为5的画笔            g.DrawLine(p, 20, 20, 120, 20);//在画板上画直线,起始坐标为(20,20),终点坐标为(120,20)            p.Color = Color.Blue;//重新定义画笔颜色为蓝色            g.DrawRectangle(p, 20, 40, 100, 40);//在画板上画矩形,起始坐标为(20,40),宽为100,高为40            p.Color = Color.Black;//重新定义画笔颜色为黑色            g.DrawEllipse(p, 20, 100, 100, 100);//在画板上画椭圆,起始坐标为(20,100),外接矩形的宽为100,高为100            SolidBrush brush = new SolidBrush(Color.Blue);//创建一个画刷            g.FillRectangle(brush, 20, 40, 100, 40);//用画刷填充矩形            Font strFont = new Font("Arial Black", 30); //创建字体            g.DrawString("Hello", strFont, new SolidBrush(Color.OrangeRed), new Point(150, 40));                   }

运行结果如图:


Graphics对象提供了一系列绘制图形的方法,部分如下:

DrawLine(直线)
DrawRectangle (矩形)
DrawEllipse (椭圆)
DrawCurve (曲线)
DarwArc (弧线)
DrawPie (扇形)
DrawLines (多边形)
DrawPolygon (封闭多边形)
DrawBezier (贝兹曲线)等

FillCircle 画填充圆(有边框)
FillEllipse 画填充椭圆(有边框)
FillPie 画填充椭圆扇形(有边框)
FillPolygon 画填充多边形(有边框)
FillRectangle 画填充矩形(有边框)


2.绘制扇形、折线、多边形、坐标系统

eg:左上角位于 (50, 50), 宽度为 300, 高度为 150 的矩形內, 绘出一起始角为 0 度, 弧角为 90 度的扇形。

  private void Form1_Paint(object sender, PaintEventArgs e)        {            Graphics g = e.Graphics;  //创建一个画图设备            Pen p = new Pen(Color.Red, 5);//定义了一个红色,宽度为5的画笔            e.Graphics.DrawPie(p, 50, 50, 300, 150, 0, 90);        }
运行效果如图:



eg:绘出一条起点为 (100, 10), 终点为 (200, 110), 并通过 (120, 70) 及 (160, 30)两点的连续线段。

 private void Form1_Paint(object sender, PaintEventArgs e)        {            Graphics g = e.Graphics;  //创建一个画图设备            Pen p = new Pen(Color.Red, 5);//定义了一个红色,宽度为5的画笔            Point p1, p2, p3, p4;            p1 = new Point(100, 10);            p2 = new Point(120, 70);            p3 = new Point(160, 30);            p4 = new Point(200, 110);            Point[] points = { p1, p2, p3, p4 };            g.DrawLines(p, points);        }
运行效果如图:



eg:绘出一個封闭多边形, 其起点为 (100, 10), 终点为300, 10), 并通过(120, 70) 及 (200,110) 两点.

 private void Form1_Paint(object sender, PaintEventArgs e)        {            Graphics g = e.Graphics;  //创建一个画图设备            Pen p = new Pen(Color.Red, 5);//定义了一个红色,宽度为5的画笔            Point p1, p2, p3, p4;            p1 = new Point(100, 10);            p2 = new Point(120, 70);            p3 = new Point(200, 110);            p4 = new Point(300, 10);            Point[] points = { p1, p2, p3, p4 };            g.DrawPolygon(p, points);        }
运行效果如图:



在GDI+中所采用的坐标系统,与平时人们较常用的坐标系统不同,主要差别在于,一般的二维坐标系,x轴与y轴分别是往右往上递增(左图),而「GDI+」所采用的坐标系,x轴与y轴则分别是往右往下递增(右图)

在数学定义中,坐标上的最基本元素:「点」,其实是个长度与宽度都无穷小的概念单位,但是在计算器图学中,作画的最基本元素是「像素(pixel)」

当我们的屏幕分辨率设定为1024*768时,表示在x轴横坐标总共可以画1024个「像素」,在y轴纵坐标总共可以画768个「像素」

绘制坐标轴:

 private void Form1_Paint(object sender, PaintEventArgs e)        {            Graphics g = this.CreateGraphics();            Pen p = new Pen(Color.Blue, 3);            Point oo1 = new Point(30, this.ClientSize.Height - 100);            Point oo2 = new Point(this.ClientSize.Width - 50, this.ClientSize.Height - 100);            g.DrawLine(p, oo1, oo2);            Point oo3 = new Point(30, 30);            g.DrawLine(p, oo1, oo3);            Font f = new Font("宋体", 12, FontStyle.Bold);            g.DrawString("x", f, p.Brush, oo2);            g.DrawString("y", f, p.Brush, 10, 10);            //绘制正弦曲线            float x1, x2, y1, y2, a;            x1 = x2 = 0;            y1 = 0; y2 = this.ClientSize.Height - 100;            for (x2 = 0; x2 < this.ClientSize.Width; x2++)            {                a = (float)(2 * Math.PI * x2 / (this.ClientSize.Width));                y2 = (float)Math.Sin(a);                y2 = (1 - y2) * (this.ClientSize.Height - 100) / 2;                g.DrawLine(p, x1 + 30, (float)y1, x2 + 30, (float)y2);                x1 = x2;                y1 = y2;            }        }
运行结果如图:




绘制饼形图:

 private void Form1_Paint(object sender, PaintEventArgs e)        {            Graphics g = this.CreateGraphics();            g.Clear(Color.White);            Pen p = new Pen(Color.Blue);            Rectangle r = new Rectangle(50, 50, 200, 100);            Brush b = new SolidBrush(Color.Blue);            g.FillPie(p.Brush, r, 0, 60);            g.FillPie(b, r, 60, 150);            b = new SolidBrush(Color.Yellow);            g.FillPie(b, r, 210, 250);        }
运行结果如图:




利用方法动态绘制饼形图:

private void Fill(int[] percent, Color[] percolor)        {            //饼形图绘制方法            Graphics g = this.CreateGraphics();            Rectangle r = new Rectangle(10, 10, 400, 400);            Brush b;            int beginAngle = 0;            for (int i = 0; i <= percent.GetUpperBound(0); i++)            {                b = new SolidBrush(percolor[i]);                g.FillPie(b, r, beginAngle, percent[i]);                beginAngle += percent[i];            }        }        private void Form1_Paint(object sender, PaintEventArgs e)        {            //绘制饼形图            int[] percent = new int[] { 40, 100, 50, 80, 70, 20 };//总数达到360即可            Color[] percolor = new Color[] { Color.Tan, Color.Orange, Color.Red, Color.Black, Color.Blue, Color.BurlyWood };            Fill(percent, percolor);        }

运行效果如图:




其他:

eg:將画笔物件的线条样式设定为虚线。

 private void Form1_Paint(object sender, PaintEventArgs e)        {            Graphics g = e.Graphics;  //创建一个画图设备            Pen p = new Pen(Color.Red, 5);//定义了一个红色,宽度为5的画笔            p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;//將画笔物件的线条样式设定为虚线            g.DrawLine(p, 20, 20, 120, 20);//在画板上画直线,起始坐标为(20,20),终点坐标为(120,20)        }
运行效果如图:



eg:将画笔对象的线条端点设为箭头及菱形。

 private void Form1_Paint(object sender, PaintEventArgs e)        {            Graphics g = e.Graphics;  //创建一个画图设备            Pen p = new Pen(Color.Red, 5);//定义了一个红色,宽度为5的画笔            p.StartCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;//将线条前端设为箭头            p.EndCap = System.Drawing.Drawing2D.LineCap.DiamondAnchor;//将线条末端设为菱形            g.DrawLine(p, 20, 20, 120, 20);//在画板上画直线,起始坐标为(20,20),终点坐标为(120,20)        }
运行效果如图:





1 0
原创粉丝点击