午夜视频在线网站,日韩视频精品在线,中文字幕精品一区二区三区在线,在线播放精品,1024你懂我懂的旧版人,欧美日韩一级黄色片,一区二区三区在线观看视频

分享

C# GDI+ 繪圖

 VeryHappyAG 2018-08-10

 (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  代碼

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Drawing.Drawing2D;  
  10.   
  11.   
  12. namespace GDI繪圖  
  13. {  
  14.     public partial class MainDialog : Form  
  15.     {  
  16.         public MainDialog()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.   
  21.         private void MainDialog_Load(object sender, EventArgs e)  
  22.         {  
  23.   
  24.         }  
  25.   
  26.         private void lineButton_Click(object sender, EventArgs e)  
  27.         {  
  28.             // 畫直線  
  29.             Graphics gra = this.CreateGraphics();  
  30.             Pen pen = new Pen(Color.Red);  
  31.             pen.Width = 2;  
  32.             Point startPoint = new Point(20,20);  
  33.             Point endPoint = new Point(70,20);  
  34.             gra.DrawLine(pen,startPoint,endPoint);  
  35.               
  36.             pen.Dispose();  
  37.             gra.Dispose();  
  38.         }  
  39.   
  40.         private void rectangleButton_Click(object sender, EventArgs e)  
  41.         {  
  42.             //畫矩形  
  43.             Graphics gra = this.CreateGraphics();  
  44.             Pen pen = new Pen(Color.Red);  
  45.             gra.DrawRectangle(pen, 20,50, 100,100);  
  46.             pen.Dispose();  
  47.             gra.Dispose();  
  48.         }  
  49.         private void cyliderButton_Click(object sender, EventArgs e)  
  50.         {  
  51.             //圓柱體,有許多個橢圓有底部逐漸疊起來的,最后填充顏色  
  52.   
  53.             int height = this.ClientSize.Height - 150;  
  54.             int width = this.ClientSize.Width - 50;  
  55.             int vHeight = 200;  
  56.             int vWidth = 100;  
  57.             Graphics gra = this.CreateGraphics();  
  58.             gra.Clear(Color.White);  
  59.             Pen pen = new Pen(Color.Gray,2);  
  60.             SolidBrush brush = new SolidBrush(Color.Gainsboro);  
  61.   
  62.             for (int i = height / 2; i > 0;i-- )  
  63.             {  
  64.                 gra.DrawEllipse(pen,width/2,i,vHeight,vWidth);  
  65.             }  
  66.   
  67.             gra.FillEllipse(brush,width/2,0,vHeight,vWidth);  
  68.         }  
  69.   
  70.         private void fillRectangleButton_Click(object sender, EventArgs e)  
  71.         {  
  72.             //畫矩形  
  73.             Graphics gra = this.CreateGraphics();  
  74.             Pen pen = new Pen(Color.Red,3);  
  75.             Brush brush = pen.Brush;  
  76.             Rectangle rect = new Rectangle(20,50,100,100);  
  77.             gra.FillRectangle(brush,rect);  
  78.             gra.Dispose();  
  79.         }  
  80.   
  81.         private void drawEllispeButton_Click(object sender, EventArgs e)  
  82.         {  
  83.             Graphics gra = this.CreateGraphics();  
  84.             Rectangle rect = new Rectangle(0,0,200,100);  
  85.             LinearGradientBrush brush = new LinearGradientBrush(rect,Color.Orange,Color.Purple,90);  
  86.             gra.FillEllipse(brush,rect);  
  87.             gra.Dispose();  
  88.         }  
  89.   
  90.         private void fontButton_Click(object sender, EventArgs e)  
  91.         {  
  92.             Graphics gra = this.CreateGraphics();  
  93.             Font font = new Font("隸書",24,FontStyle.Italic);  
  94.             Pen pen = new Pen(Color.Blue,3);  
  95.             gra.DrawString("Windows應(yīng)用程序設(shè)計",font,pen.Brush,10,100);  
  96.         }  
  97.   
  98.         private void ellispeButton_Click(object sender, EventArgs e)  
  99.         {  
  100.             // 畫圓形  
  101.             Graphics gra = this.CreateGraphics();  
  102.             Pen pen = new Pen(Color.Red);  
  103.             gra.DrawEllipse(pen, 0, 0, 200,100);  
  104.             pen.Dispose();  
  105.             gra.Dispose();  
  106.         }  
  107.   
  108.         private void moveEllispeButton_Click(object sender, EventArgs e)  
  109.         {  
  110.             // 移動圓形  
  111.             Graphics gra = this.CreateGraphics();  
  112.             Pen pen = new Pen(Color.Red);  
  113.             gra.TranslateTransform(10,10);// 改變起坐標(biāo)(10,10)  
  114.             gra.DrawEllipse(pen, 0, 0, 200, 100);  
  115.   
  116.             gra.Dispose();  
  117.         }  
  118.   
  119.         private void scaleEllispeButton_Click(object sender, EventArgs e)  
  120.         {  
  121.             // 縮放圓形  
  122.             float xScale = 1.5F;  
  123.             float yScale = 2F;  
  124.             Graphics gra = this.CreateGraphics();  
  125.             Pen pen = new Pen(Color.Red);  
  126.             gra.ScaleTransform(xScale, yScale);// X軸放大1.5倍, Y軸放大2倍  
  127.             gra.DrawEllipse(pen, 0, 0, 200, 100);  
  128.             gra.Dispose();  
  129.         }  
  130.   
  131.         private void curveButton_Click(object sender, EventArgs e)  
  132.         {  
  133.             //繪制曲線  
  134.             Graphics gra = this.CreateGraphics();  
  135.             Pen pen = new Pen(Color.Blue,3);  
  136.             Point oo1 = new Point(30,this.ClientSize.Height -100);  
  137.             Point oo2 = new Point(this.ClientSize.Width - 50 ,this.ClientSize.Height - 100);  
  138.             gra.DrawLine(pen,oo1,oo2);  
  139.             Point oo3 = new Point(30, 30);  
  140.             gra.DrawLine(pen, oo1, oo3);  
  141.             Font font = new System.Drawing.Font("宋體",12,FontStyle.Bold);  
  142.             gra.DrawString("X",font,pen.Brush,oo2);  
  143.             gra.DrawString("Y", font,pen.Brush,10,10);  
  144.   
  145.             int x1 = 0, x2 = 0;  
  146.             double a = 0;  
  147.             double y1 = 0, y2 = this.ClientSize.Height - 100;  
  148.             for (x2 = 0; x2 < this.ClientSize.Width;x2++ )  
  149.             {  
  150.                 a = 2 * Math.PI * x2 / (this.ClientSize.Width);  
  151.                 y2 = Math.Sin(a);  
  152.                 y2 = (1 - y2) *(this.ClientSize.Height-100)/2;  
  153.                 gra.DrawLine(pen,x1 +30,(float)y1 ,x2+30,(float)y2);  
  154.                 x1 = x2;  
  155.                 y1 = y2;  
  156.             }  
  157.             gra.Dispose();  
  158.         }  
  159.   
  160.         private void piechartButton_Click(object sender, EventArgs e)  
  161.         {  
  162.             //餅圖  
  163.             Graphics gra = this.CreateGraphics();  
  164.             Pen pen = new Pen(Color.Blue, 3);  
  165.             Rectangle rect = new Rectangle(50,50,200,100);  
  166.             Brush brush = new SolidBrush(Color.Blue);  
  167.             gra.FillPie(pen.Brush,rect,0,60);  
  168.             gra.FillPie(brush,rect,60,150);  
  169.             brush = new SolidBrush(Color.Yellow);  
  170.             gra.FillPie(brush,rect,210,150);  
  171.   
  172.         }  
  173.   
  174.     }  
  175. }  

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多