C# GDI+技術

高洛峰
發布: 2016-12-17 10:01:34
原創
1366 人瀏覽過

GDI+概述

GDI+是GDI(即Windows早期版本中附帶的Graphics Device Interface)的後繼者。它是一種構成Windows XP作業系統的子系統的應用程式介面(API)。 GDI+基底類別的主要命名空間及說明: System.Drawing--包含與基本繪圖功能相關的大多數類別、結構、枚舉和委託。 System.Drawing.Drawing2D--為大多數高級2D和向量繪圖操作提供了支持,包括消除鋸齒、幾何轉換和圖形路徑。 System.Drawing.Imaging--幫助處理影像(點陣圖和GIF檔案等)的各種類別。 System.Drawing.Printing--把印表機或列印預覽視窗當作輸出設備時所使用的類別。 System.Drawing.Design--一些預先定義的對話框、屬性表和其他使用者介面元素,與在設計期間擴展使用者介面相關。 System.Drawing.Text--對字型和字型系列執行更進階操作的類別。

基本圖形繪製

Graphics類別是GDI+的核心,Graphics物件表示GDI+繪圖表面,提供了物件繪製到顯示設備的方法。 Graphics類封裝了繪製直線、曲線、圖形、圖像和文字的方法,是GDI+實現繪製直線、曲線、圖形、圖像和文字的類,是進行一切GDI+操作的基礎類。

繪製直線

Graphics類別中的DrawLine方法,可重載,主要用來繪製一條連接由座標對指定的兩個點的線條。 (1)繪製一條連接兩個Point結構的線。

public void DrawLine(Pen pen, Point pt1,Point pt2)
登入後複製

   

pen:Pen對象,決定線條顏色、寬度和樣式。 pt1:Point結構,表示要連接的第一個點。 pt2:Point結構,表示要連接的第二個點。 (2)繪製一條連接由座標對指定的兩點的線條。

Public void DrawLine(Pen pen,int x1,int y1,int x2,int y2)
登入後複製

   

繪製直線的範例程式碼:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Blue, 2);
    graphics.DrawLine(myPen, 50, 30, 170, 30);
}
登入後複製

   

繪製矩形

Graphics的DrawRectle,可重載方法。 (1)繪製由Rectangle結構指定的矩形。

public void DrawRectangle(Pen pen,Rectangle rect)
登入後複製

   

pen:Pen對象,決定線條顏色、寬度和樣式。 rect:表示要繪製矩形的Rectangle結構。 例如:

Rectangle rect = new Rectangle(0, 0, 80, 50);
登入後複製

   

(2)繪製由座標對、寬度和高度指定的長方形。

public void DrawRectangle(Pen pen, int x, int y, int width, int height)
登入後複製

   

pen:Pen對象,決定線條顏色、寬度和樣式。 x:要繪製矩形的左上角x座標。 y:要繪製矩形的左上角y座標。 width和height分別表示寬度和高度。 繪製矩形的範例程式碼:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Blue, 2);
    graphics.DrawRectangle(myPen, 70, 20, 80, 50);
}
登入後複製

   

繪製橢圓

Graphics類別中的DrawEllipse方法,可重載。主要用來繪製邊界由Rectangle結構指定的橢圓。 (1)繪製邊界由Rectangle結構指定的橢圓。

public void DrawEllipse(Pen pen, Rectangle rect)
登入後複製

   

(2)繪製一個由邊框(該邊框由一對座標、高度和寬度指定)定義的橢圓。

public void DrawEllipse(Pen pen, int x, int y, int width, int height)
登入後複製

   

繪製橢圓的範例程式碼:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Blue, 3);
    Rectangle myRectangle = new Rectangle(70, 20, 100, 60);
    graphics.DrawEllipse(myPen, myRectangle);
}
登入後複製

   

繪製圓弧

Graphics中的可載方法。 (1)繪製一段弧線,它表示由Rectangle結構指定的橢圓的一部分。

public void DrawArc(Pen pen, Rectangle rect, float startAngle, float sweepAngle)
登入後複製

   

pen:Pen對象,決定線條顏色、寬度和樣式。 rect:Rectangle結構,定義橢圓邊界。 startAngle:從x軸到弧線的起始點沿著順時針方向度量的角(以度為單位)。 sweepAngle:從startAngle參數到弧線的結束點沿著順時針方向度量的角(以度為單位)。 (2)繪製一段弧線,它表示由一對座標、寬度和高度指定的橢圓部分。

public void DrawArc(Pen pen, int x, int y, int width, int height, int startAngle, int sweepAngle)
登入後複製

   

繪製圓弧的實例代碼:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Blue, 5);
    Rectangle myRectangle = new Rectangle(70, 20, 100, 60);
    graphics.DrawArc(myPen, myRectangle,210,120);
}
登入後複製

   

en2邊形

Point

。 Graphics類別提供DrawPolygon方法,Pen物件儲存用於呈現多邊形的線條屬性,如寬度和顏色等,Point(或PointF)物件陣列儲存多邊形的各個頂點。可重載。 (1)繪製由一組Point結構定義的多邊形。

public void DrawPolygon(Pen pen, Point[] pints)
登入後複製

   

(2)繪製一組PointF結構所定義的多邊形。

public void DrawPolygon(Pen pen, PointF[] pints)
登入後複製

   

繪製多邊形範例程式碼:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Red, 5);
    Point point1 = new Point(80, 20);
    Point point2 = new Point(40, 50);
    Point point3 = new Point(80, 80);
    Point point4 = new Point(160, 80);
    Point point5 = new Point(200, 50);
    Point point6 = new Point(160, 20);
    Point[] myPoints = { point1, point2, point3, point4, point5, point6 };
    graphics.DrawPolygon(myPen, myPoints);
}
登入後複製

   

組成基數樣條

基串是一個單獨的曲線連接起來組成基底數樣條的連結組成。由點的數組和張力參數指定,樣條平滑地經過數組的每個點,曲線的陡度上沒有尖角和突然的變化。 (1)繪製經過一組指定Point結構的基數樣條。

public void DrawCurve(Pen pen, Point[] points)
登入後複製

   

(2)使用指定的張力,繪製經過一組指定Point結構的基數樣條。

public void DrawCurve(Pen pen, Point[] points, float tension)
登入後複製

   

tension:大於或等於0.0F的值,指定曲線的張力。 (3)從相對於陣列開始位置的偏移量開始,繪製經過一組指定PointF結構的基底數樣條。

public void DrawCurve(Pen pen, Point[] points, int offset, int numberOfSegments)
登入後複製

   

offset:从points参数数组中的第一个元素到曲线中起始点的偏移量。numberOfSegments:起始点之后要包含在曲线中的段数。 (4)使用指定张力,绘制经过一组指定Point结构的基数样条。

public void DrawCurve(Pen pen, Point[] points, int offset, int numberOfSegments, float tension)
登入後複製

绘制基数样条示例代码:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Red, 5);
    Point point1 = new Point(50, 20);
    Point point2 = new Point(60, 30);
    Point point3 = new Point(70, 25);
    Point point4 = new Point(100, 50);
    Point point5 = new Point(130, 30);
    Point point6 = new Point(150, 45);
    Point[] myPoints = { point1, point2, point3, point4, point5, point6 };
    graphics.DrawCurve(myPen, myPoints, 1.0F);
}
登入後複製

绘制贝赛尔样条

贝塞尔样条是由4个点指定的曲线:两个端点(p1,p2)和两个控制点(c1,c2)。曲线开始于p1,结束于p2。曲线不经过控制点,但是控制点像磁铁一样,在某些方向上拉伸曲线并影响曲线弯曲的方式。 调用Graphics类的DrawBezier方法,可重载。 (1)绘制由4个Point结构定义的贝塞尔样条。

public void DrawBezier(Pen pen, Point pt1, Point pt2, Point pt3, Point pt4)
登入後複製

4个Point点分别表示起始点、第一个控制点、第二个控制点和结束点。
(2)绘制由4个表示点的有序坐标对定义的贝塞尔样条。

public void DrawBezier(Pen pen, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)
登入後複製

x2,y2及x3,y3分别表示第1个、第2个控制点相应坐标。顺序和第一种方法类似。
绘制贝塞尔样条示例代码:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Red, 5);
    float startX = 50.0F;
    float startY = 80.0F;
    float controlX1 = 150.0F;
    float controlY1 = 20.0F;
    float controlX2 = 230.0F;
    float controlY2 = 50.0F;
    float endX = 190.0F;
    float endY = 80.0F;
    graphics.DrawBezier(myPen, startX, startY, controlX1, controlY1, controlX2, controlY2, endX, endY);
}
登入後複製

绘制图形路径

路径是通过组合直线、矩形和简单的曲线而形成的。在GDI+中,GraphicsPath对象允许将基本构造块收集到一个单元中,调用一次Graphics类的DrawPath方法,就可以绘制出整个单元的直线、矩形、多边形和曲线。

public void DrawPath(Pen pen, GraphicsPath path)
登入後複製

pen:Pen对象,确定线条颜色、宽度和样式。path:要绘制的GraphicsPath图形路径。 PS:注意要引用System.Drawing.Drawing2D命名空间。
绘制图形路径示例代码:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    GraphicsPath myGraphicsPath = new GraphicsPath();
    Pen myPen = new Pen(Color.Blue, 1);
    Point[] myPoints = { new Point(15, 30), new Point(30, 40), new Point(50, 30) };
    myGraphicsPath.AddArc(15, 20, 80, 50, 210, 120);
    myGraphicsPath.StartFigure();
    myGraphicsPath.AddCurve(myPoints);
    myGraphicsPath.AddString("图形路径", new FontFamily("华文行楷"), (int)FontStyle.Underline, 50, new PointF(20, 50), new StringFormat());
    myGraphicsPath.AddPie(180,20,80,50,210,120);
    graphics.DrawPath(myPen, myGraphicsPath);
}
登入後複製

   


更多C# GDI+技术相关文章请关注PHP中文网!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!