生活随笔
收集整理的這篇文章主要介紹了
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 ?代碼
[csharp]?view plaincopy
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);?? ????????????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);?? ????????????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+ 绘图的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。