標籤:c# gdi+ 繪圖
1 座標系統
(1) 座標原點:在表單或控制項的左上方,座標為(0,0)
(2) 正方向:X軸正方向為水平向右,Y軸正方向為豎直向下
(3) 單位:在設定時,一般以像素為單位,像素(Pixel)是由映像(Picture)和元素(Element)組成,是用來計算數位影像的一種單位。
把影像放大數倍,會發現這些連續的色調其實是有許多色彩相近的小方點組成,這些小方點是構成影像的最小單位—像素。
圖形的品質是有像素決定,像素越大,解析度也越大。
2 命名空間 --- System.Drawing
(1) System.Drawing 提供了對GDI+基本圖形功能的訪問
(2) System.Drawing 常用基本類及結構
|
類 |
說明 |
|
Bitmap |
用於處理有像素資料定義的映像的對象。 |
|
Brush |
定義用於填充圖形形狀的內部對象。 |
|
Font |
定義特定的文本格式。 |
|
Graphics |
封裝一個GDI+繪圖圖畫,無法繼承此類。 |
|
Pen |
用於繪製直線和曲線的對象,無法繼承此類。 |
|
Region |
指示由矩形和路徑構成的圖形形狀的內部,無法繼承此類。 |
|
Color |
表示RGB顏色。 |
|
Point |
定義二維平面中定義的點。 |
|
Rectangle |
儲存一組整數,共4個,表示一個矩形的位置和大小。 |
|
Size |
儲存一個有序整數對,通常為矩形的寬和高。 |
3 Graphics類
Graphics類封裝了一個GDI+繪製介面,提供將對象繪製到顯示介面的方法。使用GDI+建立圖形映像時,需要先建立Graphics對象,即在哪裡畫圖。
共有3種類型的繪圖介面:
(1) 表單和控制項
(2) 印表機
(3) 記憶體的位元影像
建立繪圖物件的3中方法:
(1)控制項類的OnPaint()方法參數PaintEventArgs擷取Graphics對象
(2)表單類或控制項類中的CreateGraphics()方法獲得Graphics對象
(3)從位元影像對象(Bitmap)產生一個Graphics對象
Graphics類的常用方法
|
名稱 |
說明 |
|
Dispose |
釋放Graphics使用的所有資源。 |
|
DrawEllipse |
繪製橢圓,有高度,寬度,一對座標。 |
|
DrawArc |
繪製弧形。 |
|
DrawLine |
繪製一條直線,由2個點指定。 |
|
DrawPolygon |
繪製由一組Point結構定義的多邊形。 |
|
DrawRectangle |
繪製矩形。 |
|
DrawPie |
繪製一個扇形。 |
|
DrawCurse |
繪製曲線,由參數Point數組指定。 |
|
FillEllipse |
填充邊框所定義的橢圓的內部。 |
|
FillRegion |
填充Region的內部。 |
|
ScaleTransform |
將制定的縮放操作應用於次Graphics。 |
|
TanslateTransform |
平移更改座標系統的原點。 |
4 繪圖工具類
|
類名 |
說明 |
|
Pen |
設定畫筆的顏色,線條寬度和線條樣式(實線和虛線)。 |
|
Brush |
用於填充圖形,設定筆刷的樣式,顏色及線條的粗細。 |
5 Brush類的衍生類別
|
名稱 |
說明 |
|
ImageBrush |
映像繪製地區。 |
|
LinearGradientBrush |
線性漸層繪製地區。 |
|
RadialGradientBrush |
放射狀漸層繪製地區,焦點定義漸層的開始,橢圓定義漸層的終點。 |
|
SolidColorBrush |
單色繪製地區。 |
|
VideoBrush |
視頻內容繪製地區。 |
6 案例 免費 http://download.csdn.net/detail/taoerit/8350869
7 代碼
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Drawing.Drawing2D;namespace GDI繪圖{ public partial class MainDialog : Form { public MainDialog() { InitializeComponent(); } private void MainDialog_Load(object sender, EventArgs e) { } private void lineButton_Click(object sender, EventArgs e) { // 畫直線 Graphics gra = this.CreateGraphics(); Pen pen = new Pen(Color.Red); pen.Width = 2; Point startPoint = new Point(20,20); Point endPoint = new Point(70,20); gra.DrawLine(pen,startPoint,endPoint); pen.Dispose(); gra.Dispose(); } private void rectangleButton_Click(object sender, EventArgs e) { //畫矩形 Graphics gra = this.CreateGraphics(); Pen pen = new Pen(Color.Red); gra.DrawRectangle(pen, 20,50, 100,100); pen.Dispose(); gra.Dispose(); } private void cyliderButton_Click(object sender, EventArgs e) { //圓柱體,有許多個橢圓有底部逐漸疊起來的,最後填充顏色 int height = this.ClientSize.Height - 150; int width = this.ClientSize.Width - 50; int vHeight = 200; int vWidth = 100; Graphics gra = this.CreateGraphics(); gra.Clear(Color.White); Pen pen = new Pen(Color.Gray,2); SolidBrush brush = new SolidBrush(Color.Gainsboro); for (int i = height / 2; i > 0;i-- ) { gra.DrawEllipse(pen,width/2,i,vHeight,vWidth); } gra.FillEllipse(brush,width/2,0,vHeight,vWidth); } private void fillRectangleButton_Click(object sender, EventArgs e) { //畫矩形 Graphics gra = this.CreateGraphics(); Pen pen = new Pen(Color.Red,3); Brush brush = pen.Brush; Rectangle rect = new Rectangle(20,50,100,100); gra.FillRectangle(brush,rect); gra.Dispose(); } private void drawEllispeButton_Click(object sender, EventArgs e) { Graphics gra = this.CreateGraphics(); Rectangle rect = new Rectangle(0,0,200,100); LinearGradientBrush brush = new LinearGradientBrush(rect,Color.Orange,Color.Purple,90); gra.FillEllipse(brush,rect); gra.Dispose(); } private void fontButton_Click(object sender, EventArgs e) { Graphics gra = this.CreateGraphics(); Font font = new Font("隸書",24,FontStyle.Italic); Pen pen = new Pen(Color.Blue,3); gra.DrawString("Windows應用程式設計",font,pen.Brush,10,100); } private void ellispeButton_Click(object sender, EventArgs e) { // 畫圓形 Graphics gra = this.CreateGraphics(); Pen pen = new Pen(Color.Red); gra.DrawEllipse(pen, 0, 0, 200,100); pen.Dispose(); gra.Dispose(); } private void moveEllispeButton_Click(object sender, EventArgs e) { // 移動圓形 Graphics gra = this.CreateGraphics(); Pen pen = new Pen(Color.Red); gra.TranslateTransform(10,10);// 改變起座標(10,10) gra.DrawEllipse(pen, 0, 0, 200, 100); gra.Dispose(); } private void scaleEllispeButton_Click(object sender, EventArgs e) { // 縮放圓形 float xScale = 1.5F; float yScale = 2F; Graphics gra = this.CreateGraphics(); Pen pen = new Pen(Color.Red); gra.ScaleTransform(xScale, yScale);// X軸放大1.5倍, Y軸放大2倍 gra.DrawEllipse(pen, 0, 0, 200, 100); gra.Dispose(); } private void curveButton_Click(object sender, EventArgs e) { //繪製曲線 Graphics gra = this.CreateGraphics(); Pen pen = 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); gra.DrawLine(pen,oo1,oo2); Point oo3 = new Point(30, 30); gra.DrawLine(pen, oo1, oo3); Font font = new System.Drawing.Font("宋體",12,FontStyle.Bold); gra.DrawString("X",font,pen.Brush,oo2); gra.DrawString("Y", font,pen.Brush,10,10); int x1 = 0, x2 = 0; double a = 0; double y1 = 0, y2 = this.ClientSize.Height - 100; for (x2 = 0; x2 < this.ClientSize.Width;x2++ ) { a = 2 * Math.PI * x2 / (this.ClientSize.Width); y2 = Math.Sin(a); y2 = (1 - y2) *(this.ClientSize.Height-100)/2; gra.DrawLine(pen,x1 +30,(float)y1 ,x2+30,(float)y2); x1 = x2; y1 = y2; } gra.Dispose(); } private void piechartButton_Click(object sender, EventArgs e) { //餅圖 Graphics gra = this.CreateGraphics(); Pen pen = new Pen(Color.Blue, 3); Rectangle rect = new Rectangle(50,50,200,100); Brush brush = new SolidBrush(Color.Blue); gra.FillPie(pen.Brush,rect,0,60); gra.FillPie(brush,rect,60,150); brush = new SolidBrush(Color.Yellow); gra.FillPie(brush,rect,210,150); } }}
C# GDI+ 繪圖