淺析C#中的圖形編程(轉)

       像Java一樣,C#提供了一整套相當豐富的類庫、方法以及事件以供開發者使用。C#還引入了GDI+,它是由GDI演變而來的,具有比GDI更強大的功能而且簡化了程序員的編程工作。所以開發者運用這些,就可以很方便的開發出具有強大圖形圖像功能的應用程序了。本文,筆者就通過一些實例像讀者介紹一下C#中的圖形編程的基本知識。
簡單實例:
首先,讓我們從例子開始,以下是一個最簡單的實例:
using System;
using System.Windows.Forms;
using System.Drawing;
public class Hello:Form {
public Hello() {
this.Paint += new PaintEventHandler(f1_paint);
}
private void f1_paint(object sender,PaintEventArgs e) {
Graphics g = e.Graphics;
g.DrawString("你好,C#!",new Font("Verdana",20),
new SolidBrush(Color.Tomato),40,40);
g.DrawRectangle(new Pen(Color.Pink,3),20,20,150,100);
}
public static void Main() {
Application.Run(new Hello());
}
}
在上面的實例中,我們用到了一個方法:DrawString(),它帶有5個參數。同時,我們發現在運用DrawString()方法以前,我們先創建了一個Graphics類型的對象g=e.Graphics,這就說明了在運用任何圖形類的方法以前我們必須先創建該類的一個實例化對象。在DrawString()方法後,我們用到了DrawRectangle()方法,其實我們還可以運用其他的方法來畫橢圓或是多邊形等等。第一個實例還是相當簡單易懂的,不是嗎?
 
變換圖形的度量單位:
在圖形編程中,默認的圖形度量單位是象素。不過,你可以通過修改PageUnit屬性來修改圖形的度量單位,可以是英寸或是毫米等。實現方法如下:
Graphics g = e.Graphics;
g.PageUnit = GraphicsUnit.Inch
操作顏色選擇對話框:
在實際運用特別是圖形圖像編程過程中,我們可能會經常碰到顏色選擇對話框(以及下面要提到的字體選擇對話框)。使用顏色選擇對話框,我們可以讓用戶來選擇系統預定的顏色以及用戶自定義的顏色。在使用顏色選擇對話框之前,我們必須先創建一個ColorDialog類型的對象:
ColorDialog cd = new ColorDialog();
然後,我們就可以用ShowDialog()方法來顯示顏色選擇對話框了。之後,就可以通過調用用戶的顏色選擇進行相關的圖形操作了。
以下,我給大家一個實例。該實例中有一個按鈕和一個文本框,通過點擊按鈕可以調出顏色選擇對話框,根據用戶的顏色選擇就可以設置文本框的背景顏色了。
using System;
using System.Drawing;
using System.Windows.Forms;
public class Clr:Form{
Button b1 = new Button();
TextBox tb = new TextBox();
ColorDialog clg = new ColorDialog();
public Clr(){
b1.Click += new EventHandler(b1_click);
b1.Text = "選擇顏色";
tb.Location = new Point(50,50);
this.Controls.Add(b1);
this.Controls.Add(tb);
}
public void b1_click(object sender, EventArgs e){
clg.ShowDialog();
tb.BackColor = clg.Color;
}
public static void Main() {
Application.Run(new Clr());
}
}
操作字體選擇對話框:
字體是圖形編程的一個重要組成部分,通過設置不同的字體,你可以在程序中達到不同的視覺效果。和以上的顏色選擇對話框的創建差不多,你可以很方便地創建一個字體選擇對話框,並通過它來讓用戶選擇其所需的字體。
下面同樣給出一個實例,這個實例和上面的實例差不多,只是用來字體選擇對話框代替了原來的顏色選擇對話框,最後是根據用戶的字體選擇來設置文本框的字體。
using System;
using System.Drawing;
using System.Windows.Forms;
public class Fonts:Form {
Button b1 = new Button();
TextBox tb = new TextBox();
FontDialog flg = new FontDialog();
public Fonts() {
b1.Click += new EventHandler(b1_click);
b1.Text = "選擇字體";
tb.Location = new Point(50,50);
this.Controls.Add(b1);
this.Controls.Add(tb);
}
public void b1_click(object sender, EventArgs e) {
clg.ShowDialog();
tb.FontName = flg.Font;
}
public static void Main() {
Application.Run(new Fonts());
}
}
使用System.Drawing.Drawing2D名字空間:
如果你有一些圖形圖像編程的經驗,那麼你一定知道畫筆和畫刷的概念。它們在圖形編程有非常廣泛的運用。System.Drawing.Drawing2D名字空間提供了相當強大的功能,能讓開發者很容易地操作畫筆以及畫刷對象。比如,你可以通過設置畫筆的DashStyle屬性(有Dash、DashDot、Solid等風格)來確定直線的風格。同樣,通過運用SolidBrush、HatchBrush、GradientBrush等畫筆你可以很輕易地修改被填充區域的外觀。比如,你可以用SolidBrush將一個矩形區域用許許多多不同粗細的直線來填充。那麼,我們在什麼時候運用畫筆和畫刷呢?就像上面的例子中那樣,通常一個圖形輪廓(運用DrawXXX()方法)是用畫筆對象來實現的,而一個填充區域(運用FillXXX()方法)則是用畫刷對象來實現的。
使用畫筆對象:
在下面的實例中,我們用到了System.Drawing.Drawing2D名字空間。實例中我們用畫筆以不同的風格畫了直線、橢圓、餡餅圖形、多邊形等圖形。
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
public class Drawgra:Form {
public Drawgra() {
this.Text = "運用畫筆示例";
this.Size = new Size(450,400);
this.Paint += new PaintEventHandler(Draw_Graphics);
}
public void Draw_Graphics(object sender,PaintEventArgs e) {
Graphics g = e.Graphics;
Pen penline = new Pen(Color.Red,5);
Pen penellipse = new Pen(Color.Blue,5);
Pen penpie = new Pen(Color.Tomato,3);
Pen penpolygon = new Pen(Color.Maroon,4);
/*DashStyle有Dash、DashDot、DashDotDot、Dot、Solid等風格*/
//以Dash風格畫一條直線
penline.DashStyle = DashStyle.Dash;
g.DrawLine(penline,50,50,100,200);
//以DashDotDot風格畫一個橢圓
penellipse.DashStyle = DashStyle.DashDotDot;
g.DrawEllipse(penellipse,15,15,50,50);
//以Dot風格畫一個餡餅圖形
penpie.DashStyle = DashStyle.Dot;
g.DrawPie(penpie,90,80,140,40,120,100);
//以Solid風格畫一個多邊形
g.DrawPolygon(penpolygon,new Point[]{
new Point(30,140),
new Point(270,250),
new Point(110,240),
new Point(200,170),
new Point(70,350),
new Point(50,200)});
}
public static void Main() {
Application.Run(new Drawgra());
}
}
使用畫刷對象:
畫刷對象是用特定的顏色、模式或是圖像來填充一塊區域的。總共有四種類型的畫刷:SolidBrush(默認的畫刷)、HatchBrush、GradientBrush以及TexturedBrush。下面,我們分別給出實例來進行介紹。
1、運用SolidBrush:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
public class Solidbru:Form {
public Solidbru() {
this.Text = "運用SolidBrush示例";
this.Paint += new PaintEventHandler(Fill_Graph);
}
public void Fill_Graph(object sender,PaintEventArgs e) {
Graphics g = e.Graphics;
//創建一把SolidBrush並用它來填充一個矩形區域
SolidBrush sb = new SolidBrush(Color.Pink);
g.FillRectangle(sb,50,50,150,150);
}
public static void Main() {
Application.Run(new Solidbru());
}
}
2、運用HatchBrush:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
public class Hatchbru:Form {
public Hatchbru() {
this.Text = "運用HatchBrush示例";
this.Paint += new PaintEventHandler(Fill_Graph);
}
public void Fill_Graph(object sender,PaintEventArgs e) {
Graphics g = e.Graphics;
//創建一把HatchBrush並用它來填充一個矩形區域
/*該畫刷的HatchStyle有DiagonalCross、
ForwardDiagonal、Horizontal、 Vertical、 Solid等不同風格 */
HatchStyle hs = HatchStyle.Cross;
HatchBrush sb = new HatchBrush(hs,Color.Blue,Color.Red);
g.FillRectangle(sb,50,50,150,150);
}
public static void Main() {
Application.Run(new Hatchbru());
}
} 3、運用GradientBrush:
GradientBrush又可分爲LinearGradientBrush和PathGradientBrush兩種,從它們的名稱我們可以知道前者是線性漸變的,而後者則是路徑漸變的,因而能創造出更復雜和完美的效果。下面我就給大家分別舉例:
1)、運用LinearGradientBrush:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
public class LinearGradientbru:Form {
public LinearGradientbru() {
this.Text = "運用LinearGradientBrush示例";
this.Paint += new PaintEventHandler(Fill_Graph);
}
public void Fill_Graph(object sender,PaintEventArgs e) {
Rectangle r = new Rectangle(500, 300, 100, 100);
LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red, Color.Yellow,
LinearGradientMode.BackwardDiagonal);
e.Graphics.FillRectangle(lb, r);
}
public static void Main() {
Application.Run(new LinearGradientbru());
}
}
所得圖形如下:
2)、運用PathGradientBrush:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
public class PathGradientbru:Form {
public PathGradientbru() {
this.Text = "運用PathGradientBrush示例";
this.Paint += new PaintEventHandler(Fill_Graph);
}
public void Fill_Graph(object sender,PaintEventArgs e) {
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased;
e.Graphics.FillRectangle(backgroundBrush, ClientRectangle);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle);
//先設置好一個路徑
GraphicsPath path = new GraphicsPath(new Point[] {
new Point(40, 140),
new Point(275, 200),
new Point(105, 225),
new Point(190, 300),
new Point(50, 350),
new Point(20, 180),
}, new byte[] {
(byte)PathPointType.Start,
(byte)PathPointType.Bezier,
(byte)PathPointType.Bezier,
(byte)PathPointType.Bezier,
(byte)PathPointType.Line,
(byte)PathPointType.Line,
});
//創建一把PathGradientBrush
PathGradientBrush pgb = new PathGradientBrush(path);
//設置畫刷的周圍顏色
pgb.SurroundColors = new Color[] {
Color.Green,
Color.Yellow,
Color.Red,
Color.Blue,
Color.Orange,
Color.White,
};
//用畫刷進行填充
e.Graphics.FillPath(pgb, path);
}
public static void Main() {
Application.Run(new PathGradientbru());
}
}
所得圖形如下:
4、運用TexturedBrush:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
public class Texturedbru:Form {
Brush bgbrush;
public Texturedbru() {
//創建一幅圖像以供填充橢圓的背景用
Image bgimage = new Bitmap("dotnet.gif");
bgbrush = new TextureBrush(bgimage);
this.Paint+=new PaintEventHandler(Text_bru);
}
public void Text_bru(object sender,PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillEllipse(bgbrush,50,50,500,300);
}
public static void Main() {
Application.Run(new Texturedbru());
}
}
使用圖像:
圖像在圖形編程中經常要用到的,其對用戶界面的作用也是非常明顯的。在以前的編程過程中,對圖像的操作細節是相當繁瑣的而且很容易出錯。現在,在GDI+下面,你可以用C#語言很容易的完成你所希望的圖像編程。
很簡單,你只要通過以下步驟就可以實現圖像的編程。
1、 創建一個位圖類對象如下:
Image img = new Bitmap("image.bmp");
2、 在DrawImage()方法中運用上述對象:
g.DrawImage(img,20,20,100,90);
至於使用圖像的實例,限於篇幅,我就不再這裏介紹了。相信大家能很容易地完成一個運用圖像的實例的。
總結:
在這篇文章中,我主要用到了兩個非常核心的名字空間:一個是System.Drawing、一個是System.Drawing.Drawing2D。有了它們,我們就可以很方便的調用其中的方法、屬性來實現以往進行圖形編程需要付出很大代價才能完成的任務,這不能不說是GDI+給我們帶來的優點。所以,掌握好GDI+,我相信你的圖形編程能力會更上一層樓的。
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章