C#基礎GDI繪圖

GDI繪圖

畫筆

畫筆使用Pen類表示,主要用於繪製線條,或者線條組合成的其他幾何形狀。

public Pen (Color color,float width)
屬性 說明
Brush 獲取或設置Brush,用於確定此Pen的屬性
Color 獲取或設置此Pen的顏色
CustomEndCap 獲取或設置要在通過此Pen繪製的直線終點使用的自定義線帽
CustomStartCap 獲取或設置要在通過此Pen繪製的直線起點使用的自定義線帽
DashCap 獲取或設置用在短劃線終點的線帽樣式,這些短劃線構成通過此Pen繪製的虛線
DashStyle 獲取或設置用於通過此Pen繪製的虛線的樣式
EndCap 獲取或設置要在通過此Pen繪製的直線終點使用的線帽樣式
StartCap 獲取或設置在通過此Pen繪製的直線起點使用的線帽樣式
Transform 獲取或設置此Pen的幾何變換的副本
Width 獲取或設置此Pen的寬度,以用於繪圖的Graphics對象爲單位

畫刷

畫刷使用Brush類表示,主要用於填充幾何圖形,如將正方形和圓形填充其他顏色等。Brush類是一個抽象基類,不能進行實例化。如果要創建一個畫刷對象,需要使用從Brush派生出的類。

Brush mybs = new SolidBrush(Color.Red);
HatchBrush brush = new HatchBrush(HatchStyle.DiagonalBrick,Color.Yellow);
LinearGradientBrush lgb=new LinearGradientBrush(rt,Color.Blue,Color.White, LinearGradientMode.ForwardDiagonal);
TextureBrush texture = new TextureBrush(image1);

派生類 說明
SolidBrush 定義單色畫刷
HatchBrush 提供一種特定樣式的圖形,用來製作填滿整個封閉區域的繪圖效果,該類位於System.Drawing.Drawing2D命名空間下
LinerGradientBrush 提供一種漸變色彩的特效,填滿圖形的內部區域,該類位於System.Drawing.Drawing2D命名空間下
TextureBrush 使用圖像來填充形狀的內部

繪製直線

調用Graphics類中的DrawLine方法,結合Pen對象可以繪製直線。

public void DrawLine (Pen pen,Point pt1,Point pt2)
public void DrawLine (Pen pen,int x1,int y1,int x2,int y2)

繪製矩形

通過Graphics類中的DrawRectangle或者FillRectangle方法可以繪製矩形。

//DrawRectangle方法——繪製矩形
public void DrawRectangle (Pen pen,int x,int y,int width,int height)

//FillRectangle方法——填充矩形
public void FillRectangle(Brush brush,int x,int y,int width,int height)

綜合例子

Graphics g = this.CreateGraphics();//創建Graphics對象
int halfWidth = this.Width / 2;
int halfHeight = this.Height / 2;
Pen pen = new Pen(Color.Blue, 2);//創建畫筆
AdjustableArrowCap arrow = new AdjustableArrowCap(8, 8, false);//定義畫筆線帽
pen.CustomEndCap = arrow;
g.DrawLine(pen, 50, halfHeight-20, Width - 50, halfHeight-20);//畫橫座標軸
g.DrawLine(pen, halfWidth, Height - 60, halfWidth, 20);//畫縱座標軸
int[] saleNum = { 300, 500, 400, 450, 600, 630, 580, 650, 700, 620, 500, 480 };
Graphics g = this.CreateGraphics();//創建Graphics對象
Font font = new Font("Arial", 9, FontStyle.Regular);
Pen mypen = new Pen(Color.Blue, 1);
//繪製橫向線條
int x = 100;
for (int i = 0; i < 11; i++)
{
g.DrawLine(mypen, x, 80, x, 366);
x = x + 40;
}
g.DrawLine(mypen, x - 480, 80, x - 480, 366);
//繪製縱向線條
int y = 127;
for (int i = 0; i < 10; i++)
{
g.DrawLine(mypen, 60, y, 540, y);
y = y + 24;
}
g.DrawLine(mypen, 60, y, 540, y);
#region 繪製X軸和Y軸的文字
////x軸
//String[] n = {"  一月", "  二月", "  三月", "  四月", "  五月", "  六月", "  七月",
//         "  八月", "  九月", "  十月", "十一月", "十二月"};
//x = 65;
//for (int i = 0; i < 12; i++)
//{
//    g.DrawString(n[i].ToString(), font, Brushes.Red, x, 374);//設置文字內容及輸出位置
//    x = x + 40;
//}
////y軸
//String[] m = {"1000", " 900", " 800", " 700", " 600", " 500", " 400", " 300",
//         " 200", " 100", "  0"};
//y = 120;
//for (int i = 0; i < 11; i++)
//{
//    g.DrawString(m[i].ToString(), font, Brushes.Red, 25, y);//設置文字內容及輸出位置
//    y = y + 24;
//}
#endregion
//顯示柱狀效果
x = 70;
for (int i = 0; i < 12; i++)
{
SolidBrush mybrush = new SolidBrush(Color.YellowGreen);
g.FillRectangle(mybrush, x, 370 - saleNum[i] / 4, 20, saleNum[i] / 4 - 3);
x = x + 40;
}
g.Dispose();

繪製橢圓

通過Graphics類中的DrawEllipse方法或者FillEllipse方法可以繪製橢圓。

//DrawEllipse方法——繪製橢圓
public void DrawEllipse (Pen pen,int x,int y,int width,int height)
    
//FillEllipse方法——填充橢圓
public void FillEllipse(Brush brush,int x,int y,int width,int height)

//空心橢圓
Graphics graphics = this.CreateGraphics();//創建Graphics對象
Pen myPen = new Pen(Color.Green, 3);//創建Pen對象
graphics.DrawEllipse(myPen, 50, 10, 120, 80); //繪製空心橢圓

//實心橢圓
Graphics graphics = this.CreateGraphics();//創建Graphics對象
Brush brush = new SolidBrush(Color.Red);//創建畫刷對象
graphics.FillEllipse(brush, 210, 10, 120, 80);//繪製實心橢圓

繪製圓弧

通過Graphics類中的DrawArc方法,可以繪製圓弧,該方法用來繪製由一對座標、寬度和高度指定的圓弧。

public void DrawArc (Pen pen,Rectangle rect,float startAngle,float sweepAngle)

Graphics ghs = this.CreateGraphics();//創建Graphics對象
Pen myPen = new Pen(Color.Blue, 3);//創建畫筆
Rectangle myRectangle = new Rectangle(70, 20, 100, 60);//定義一個Rectangle結構
//調用Graphics對象的DrawArc方法繪製圓弧
ghs.DrawArc(myPen, myRectangle, 90, 160);

繪製扇形

過Graphics類中的DrawPie方法和FillPie方法可以繪製扇形。

//DrawPie方法——繪製扇形
public void DrawPie (Pen pen,float x,float y,float width,float height,float startAngle,float sweepAngle)

//FillPie方法——填充扇形
public void FillPie(Brush brush,float x,float y,float width,float height,float startAngle,float sweepAngle)

int[] saleNum = { 300, 500, 400 };
//獲取總銷量和各月分別銷量
int sum = 0, threeNum = 0, fourNum = 0, fiveNum = 0;
for (int i = 0; i < saleNum.Length; i++)
{
sum += saleNum[i];
if (i == 0)
threeNum = saleNum[0];
else if (i == 1)
fourNum = saleNum[1];
else
fiveNum = saleNum[2];
}
Graphics g = this.CreateGraphics();
//清空背景色
g.Clear(Color.White);
Pen pen1 = new Pen(Color.Red);//實例化Pen類
//創建4個Brush對象用於設置顏色
Brush brush = new SolidBrush(Color.Black);
Brush brush2 = new SolidBrush(Color.Blue);
Brush brush3 = new SolidBrush(Color.Wheat);
Brush brush4 = new SolidBrush(Color.Orange);
//創建Font對象用於設置字體
Font font = new Font("Courier New", 12);
int piex = 30, piey = 30, piew = 200, pieh = 200;
//3月份銷量在圓中分配的角度
float angle1 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(threeNum));
//4月份銷量在圓中分配的角度
float angle2 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(fourNum));
//5月份銷量在圓中分配的角度
float angle3 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(fiveNum));
g.FillPie(brush2, piex, piey, piew, pieh, 0, angle1);//繪製3月份銷量所佔比例
g.FillPie(brush3, piex, piey, piew, pieh, angle1, angle2);//繪製4月份銷量所佔比例
g.FillPie(brush4, piex, piey, piew, pieh, angle1 + angle2, angle3); //繪製5月份銷量所佔比例

繪製多邊形

通過Graphics類中的DrawPolygon方法或者FillPolygon方法可以繪製多邊形。多邊形是有3條或更多條邊的閉合圖形。如果要繪製多邊形,需要Graphics對象、Pen對象和Point(或PointF)對象數組。

//DrawPolygon方法——繪製多邊形
 public void DrawPolygon (Pen pen,PointF[] points)
     
//FillPolygon方法——填充多邊形
public void FillPolygon(Brush brush,Point[] points)

顏色

1.系統定義的顏色
系統定義的顏色使用Color結構的屬性來表示,例如:

Color myColor = Color.Red;

2.用戶定義的顏色
使用Color結構的FromArgb方法,其語法格式如下:

public static Color FromArgb(int red,int green,int blue)

例如,下面代碼使用紅色的R、G、B值自定義顏色:

Color myColor = Color.FromArgb(255, 0, 0);

3.Alpha混合處理(透明度)
Alpha使用256級灰度來記錄圖像中的透明度信息,主要用來定義透明、不透明和半透明區域,其中黑表示透明,白表示不透明,灰表示半透明。如果在定義顏色時,需要指定Alpha透明度,則需要使用FromArgb方法的另外一種形式,語法如下:

public static Color FromArgb(int alpha,int red,int green,int blue)

例如,使用紅色的R、G、B值自定義顏色,並將透明度設置爲大約50%,代碼如下:

Color myColor = Color.FromArgb(128, 255, 0, 0);

字體

例如,創建一個Font對象,字體設置爲“宋體”,大小設置爲16,樣式設置爲加粗樣式,代碼如下:

Font myFont = new Font("宋體", 16, FontStyle.Bold);

輸出文本

通過Graphics類中的DrawString方法,可以指定位置以指定的Brush和Font對象繪製指定的文本字符串。

public void DrawString(string s,Font font,Brush brush,float x,float y)

string str = "商品銷售柱形圖";//定義繪製的文本
Font myFont = new Font("宋體", 16, FontStyle.Bold);//創建字體對象
SolidBrush myBrush = new SolidBrush(Color.Black);//創建畫刷對象
Graphics myGraphics = this.CreateGraphics();//創建Graphics對象
myGraphics.DrawString(str, myFont, myBrush, 60, 20);//在窗體的指定位置繪製文本

繪製圖像

//public void DrawImage(Image image,int x,int y,int width,int height)

Image myImage = Image.FromFile("logo.jpg");//創建Image對象
Graphics myGraphics = this.CreateGraphics();//創建Graphics對象
myGraphics.DrawImage(myImage, 50, 20,90,92);//繪製圖像

刷新圖像

前面介紹的繪製圖像的實例,都是使用窗體或者控件的CreateGraphics方法創建的Graphics繪圖對象,這導致繪製的圖像都是暫時的,如果當前窗體被切換或者被其他窗口覆蓋,這些圖像就會消失,爲了使圖像永久顯示,可以通過在窗體或者控件的Bitmap對象上繪製圖像來實現。
Bitmap對象用來封裝GDI+位圖,此位圖由圖形圖像及其特性的像素數據組成,它是用於處理由像素數據定義的圖像的對象。使用Bitmap對象繪製圖像時,可以先創建一個Bitmap對象,並在其上繪製圖像,然後再將其賦值給窗體或者控件的Bitmap對象,這樣繪製出的圖像就可以自動刷新,不用再使用程序來重繪圖像,具體步驟如下:
(1)創建Bitmap對象時,需要使用Bitmap類的構造函數,代碼如下:

Bitmap bmp = new Bitmap(120, 80);

(2)創建完Bitmap對象之後,使用創建的Bitmap對象生成Graphics繪圖對象,然後調用Graphics繪圖對象的相關方法繪製圖像,代碼如下:

Graphics g = Graphics.FromImage(bmp);
Pen myPen = new Pen(Color.Green, 3);
g.DrawEllipse(myPen, 50, 10, 120, 80);

(3)最後將Bitmap對象指定給窗體或者控件的Bitmap對象,例如,下面代碼將Bitmap對象指定給窗體的BackgroundImage屬性,代碼如下:

this.BackgroundImage = bmp;

通過以上步驟繪製出的圖像就可以自動刷新,並永久顯示。

綜合例子

int[] saleNum = { 300, 500, 400 };
//獲取總銷量和各月分別銷量
int sum = 0, threeNum = 0, fourNum = 0, fiveNum = 0;
for (int i = 0; i < saleNum.Length; i++)
{
    sum += saleNum[i];
    if (i == 0)
        threeNum = saleNum[0];
    else if (i == 1)
        fourNum = saleNum[1];
    else
        fiveNum = saleNum[2];
}
//創建畫圖對象
int width = 400, height = 450;
Bitmap bitmap = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bitmap);
//清空背景色
g.Clear(Color.White);
Pen pen1 = new Pen(Color.Red);					//實例化Pen類
//創建4個Brush對象用於設置顏色
Brush brush1 = new SolidBrush(Color.PowderBlue);
Brush brush2 = new SolidBrush(Color.Blue);
Brush brush3 = new SolidBrush(Color.Wheat);
Brush brush4 = new SolidBrush(Color.Orange);
//創建兩個Font對象用於設置字體
Font font1 = new Font("Courier New", 16, FontStyle.Bold);
Font font2 = new Font("Courier New", 10);
//繪製背景圖
g.FillRectangle(brush1, 0, 0, width, height);
g.DrawString("每月商品銷量佔比餅形圖", font1, brush2, new Point(70, 20));//書寫標題
int piex = 100, piey = 60, piew = 200, pieh = 200;
//3月份銷量在圓中分配的角度
float angle1 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(threeNum));
//4月份銷量在圓中分配的角度
float angle2 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(fourNum));
//5月份銷量在圓中分配的角度
float angle3 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(fiveNum));
g.FillPie(brush2, piex, piey, piew, pieh, 0, angle1);		//繪製3月份銷量所佔比例
g.FillPie(brush3, piex, piey, piew, pieh, angle1, angle2);//繪製4月份銷量所佔比例
//繪製5月份銷量所佔比例
g.FillPie(brush4, piex, piey, piew, pieh, angle1 + angle2, angle3);
//繪製標識
g.DrawRectangle(pen1, 50, 300, 310, 130);				//繪製範圍框
g.FillRectangle(brush2, 90, 320, 20, 10);				//繪製小矩形
g.DrawString(string.Format("3月份銷量佔比:{0:P2}", Convert.ToSingle(threeNum) / Convert.ToSingle(sum)), font2, brush2, 120, 320);
g.FillRectangle(brush3, 90, 360, 20, 10);
g.DrawString(string.Format("4月份銷量佔比:{0:P2}", Convert.ToSingle(fourNum) / Convert.ToSingle(sum)),font2, brush2, 120, 360);
g.FillRectangle(brush4, 90, 400, 20, 10);
g.DrawString(string.Format("5月份銷量佔比:{0:P2}", Convert.ToSingle(fiveNum) / Convert.ToSingle(sum)), font2, brush2, 120, 400);
this.BackgroundImage = bitmap;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章