使用画笔对象:   在下面的实例中,我们用到了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()); 
} }  
浅析C#中图形编程(二)
 作者:
   使用画刷对象:   画刷对象是用特定的颜色、模式或是图像来填充一块区域的。总共有四种类型的画刷: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()); 


解决方案 »

  1.   

    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+,我相信你的图形编程能力会更上一层楼的。