(1) 坐標(biāo)原點(diǎn):在窗體或控件的左上角,坐標(biāo)為(0,0)
(2) 正方向:X軸正方向?yàn)樗较蛴?,Y軸正方向?yàn)樨Q直向下
(3) 單位:在設(shè)置時,一般以像素為單位,像素(Pixel)是由圖像(Picture)和元素(Element)組成,是用來計算數(shù)碼影像的一種單位。
把影像放大數(shù)倍,會發(fā)現(xiàn)這些連續(xù)的色調(diào)其實(shí)是有許多色彩相近的小方點(diǎn)組成,這些小方點(diǎn)是構(gòu)成影像的最小單位—像素。
圖形的質(zhì)量是有像素決定,像素越大,分辨率也越大。
2 命名空間 --- System.Drawing
(1) System.Drawing 提供了對GDI+基本圖形功能的訪問
(2) System.Drawing 常用基本類及結(jié)構(gòu)
類 |
說明 |
Bitmap |
用于處理有像素數(shù)據(jù)定義的圖像的對象。 |
Brush |
定義用于填充圖形形狀的內(nèi)部對象。 |
Font |
定義特定的文本格式。 |
Graphics |
封裝一個GDI+繪圖圖畫,無法繼承此類。 |
Pen |
用于繪制直線和曲線的對象,無法繼承此類。 |
Region |
指示由矩形和路徑構(gòu)成的圖形形狀的內(nèi)部,無法繼承此類。 |
Color |
表示RGB顏色。 |
Point |
定義二維平面中定義的點(diǎn)。 |
Rectangle |
存儲一組整數(shù),共4個,表示一個矩形的位置和大小。 |
Size |
存儲一個有序整數(shù)對,通常為矩形的寬和高。 |
3 Graphics類
Graphics類封裝了一個GDI+繪制界面,提供將對象繪制到顯示界面的方法。使用GDI+創(chuàng)建圖形圖像時,需要先創(chuàng)建Graphics對象,即在哪里畫圖。
共有3種類型的繪圖界面:
(1) 窗體和控件
(2) 打印機(jī)
(3) 內(nèi)存的位圖
創(chuàng)建圖形對象的3中方法:
(1)控件類的OnPaint()方法參數(shù)PaintEventArgs獲取Graphics對象
(2)窗體類或控件類中的CreateGraphics()方法獲得Graphics對象
(3)從位圖對象(Bitmap)產(chǎn)生一個Graphics對象
Graphics類的常用方法
名稱 |
說明 |
Dispose |
釋放Graphics使用的所有資源。 |
DrawEllipse |
繪制橢圓,有高度,寬度,一對坐標(biāo)。 |
DrawArc |
繪制弧形。 |
DrawLine |
繪制一條直線,由2個點(diǎn)指定。 |
DrawPolygon |
繪制由一組Point結(jié)構(gòu)定義的多邊形。 |
DrawRectangle |
繪制矩形。 |
DrawPie |
繪制一個扇形。 |
DrawCurse |
繪制曲線,由參數(shù)Point數(shù)組指定。 |
FillEllipse |
填充邊框所定義的橢圓的內(nèi)部。 |
FillRegion |
填充Region的內(nèi)部。 |
ScaleTransform |
將制定的縮放操作應(yīng)用于次Graphics。 |
TanslateTransform |
平移更改坐標(biāo)系統(tǒng)的原點(diǎn)。 |
4 繪圖工具類
類名 |
說明 |
Pen |
設(shè)置畫筆的顏色,線條粗細(xì)和線條樣式(實(shí)線和虛線)。 |
Brush |
用于填充圖形,設(shè)置筆刷的樣式,顏色及線條的粗細(xì)。 |
5 Brush類的派生類
名稱 |
說明 |
ImageBrush |
圖像繪制區(qū)域。 |
LinearGradientBrush |
線性漸變繪制區(qū)域。 |
RadialGradientBrush |
徑向漸變繪制區(qū)域,焦點(diǎn)定義漸變的開始,橢圓定義漸變的終點(diǎn)。 |
SolidColorBrush |
單色繪制區(qū)域。 |
VideoBrush |
視頻內(nèi)容繪制區(qū)域。 |
6 案例 免費(fèi)下載地址 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應(yīng)用程序設(shè)計",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);// 改變起坐標(biāo)(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);
-
- }
-
- }
- }
|