爱学习受.NET

www.icjyw.com 记录开发技术收藏天地
公告信息
www.icjyw.com 记录开发技术收藏天地
文章分类
文章档案
文章
GDI+ 使用笔刷绘制字体学习
2011/8/2 14:27:40

以下将采用十字架来绘制字体。如图:

 

 1.思路,先创建图案笔刷,然后再绘制图形。

 

代码如下:


View Code
 1    private void Form1_Paint(object sender, PaintEventArgs e)
 2         {
 3             Graphics g = e.Graphics;
 4
 5             Font f = new Font("Aries", 60, FontStyle.Bold);
 6             HatchBrush hb = new HatchBrush(HatchStyle.Cross, Color.Blue, Color.Gray);
 7             g.DrawString("我叫王王王", f, hb, 0f, 20f);
 8             g.DrawString("我是超人", f,hb, 0f, 100f);
 9
10         }
 使用线性渐变绘制字体

引自:http://pdflist.mmic.net.cn


代码如下:

View Code
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            Font f = new Font("Aries", 60, FontStyle.Bold);
            HatchBrush hb = new HatchBrush(HatchStyle.Cross, Color.Blue, Color.Gray);
            LinearGradientBrush lb = new LinearGradientBrush(new Point(0, 0), new Point(0, 20), Color.Gray, Color.Red);
            g.DrawString("我叫王王王", f, hb, 0f, 20f);
            g.DrawString("我是超人", f, lb, 0f, 100f);

        }
 使用路径渐变来填充字体。如图:

 


思路先创建一个路径渐变笔刷,然后再绘制字体。

代码如下:

View Code
 1 private void Form1_Paint(object sender, PaintEventArgs e)
 2         {
 3             Graphics g = e.Graphics;
 4
 5             Font f = new Font("Aries", 60, FontStyle.Bold);
 6             HatchBrush hb = new HatchBrush(HatchStyle.Cross, Color.Blue, Color.Gray);
 7             LinearGradientBrush lb = new LinearGradientBrush(new Point(0, 0), new Point(0, 20), Color.Gray, Color.Red);
 8             GraphicsPath gp = new GraphicsPath();
 9        //    gp.AddEllipse(new Rectangle(0, 0, 120, 60));
10            gp.AddLine(0, 0, 50, 0);
11            gp.AddLine(50, 00, 50, 50);
12            gp.AddLine(50, 50, 00, 50);
13             PathGradientBrush pgb = new PathGradientBrush(gp);
14             pgb.CenterColor = Color.Blue;
15             pgb.SurroundColors = new Color[] {
16             Color.White,
17             Color.Red,
18             Color.Purple
19             };
20             pgb.WrapMode = WrapMode.TileFlipXY;
21            // g.FillPath(pgb, gp);
22            // pgb.CenterPoint = new PointF(325f,325f);
23       //   g.FillPath(pgb,gp);
24             //g.FillEllipse(pgb, new Rectangle(100, 300, 250, 100));
25             g.DrawString("我叫王王王", f, hb, 0f, 20f);
26             g.DrawString("我是超人", f, lb, 0f, 100f);
27             g.DrawString("我是超人", f, pgb, 0f,200f);
28
29         }
 

 关于径向渐变笔刷的应用:

 以下代码只是创建要填充的路径,先给出路径的本身大小图:

 

 

代码如下:


View Code
 1   //使用路径来填充矩形
 2         private void Form1_Paint(object sender, PaintEventArgs e)
 3         {
 4             Graphics g = e.Graphics;
 5             GraphicsPath gp = new GraphicsPath();
 6             gp.AddLine(0, 0, 50, 0);
 7             gp.AddLine(50, 00, 50, 50);
 8             gp.AddLine(50, 50, 00, 50);
 9             PathGradientBrush pgb = new PathGradientBrush(gp);
10             pgb.CenterColor = Color.Blue;
11             pgb.SurroundColors = new Color[] {
12             Color.White,
13             Color.Red,
14             Color.Purple
15             };
16             pgb.WrapMode = WrapMode.TileFlipXY;
17             g.FillPath(pgb, gp);
18            pgb.Dispose();
19            gp.Dispose();
20         }
21         

 接着使用该路径去填充以下图形。

 

 如果要在一个比较大范围以该路径进行填充,需要设置以下属性。来对路径进行重复的平铺设置。
1    pgb.WrapMode = WrapMode.TileFlipXY;
 比如 以宽高各50的路径去填充宽200,高100的矩形,代码如下:


 


 

代码如下:


View Code
 1    //使用路径来填充矩形
 2         private void Form1_Paint(object sender, PaintEventArgs e)
 3         {
 4             Graphics g = e.Graphics;
 5             GraphicsPath gp = new GraphicsPath();
 6             gp.AddLine(0, 0, 50, 0);
 7             gp.AddLine(50, 00, 50, 50);
 8             gp.AddLine(50, 50, 00, 50);
 9             PathGradientBrush pgb = new PathGradientBrush(gp);
10             pgb.CenterColor = Color.Blue;
11             pgb.SurroundColors = new Color[] {
12             Color.White,
13             Color.Red,
14             Color.Purple
15             };
16             pgb.WrapMode = WrapMode.TileFlipXY;
17            g.FillRectangle(pgb, new Rectangle(50, 100, 250, 200));
18            pgb.Dispose();
19            gp.Dispose();
20         }

 

 

 现在演使用路径绘制椭圆形,以说明使用路径渐变笔刷并不一定是绘制路径本身的图形,而是通过路径去填充其他图形。

 


 

 代码如下:


View Code
 1   private void Form1_Paint(object sender, PaintEventArgs e)
 2         {
 3             Graphics g = e.Graphics;
 4             GraphicsPath gp = new GraphicsPath();
 5             gp.AddLine(0, 0, 50, 0);
 6             gp.AddLine(50, 00, 50, 50);
 7             gp.AddLine(50, 50, 00, 50);
 8             PathGradientBrush pgb = new PathGradientBrush(gp);
 9             pgb.CenterColor = Color.Blue;
10             pgb.SurroundColors = new Color[] {
11             Color.White,
12             Color.Red,
13             Color.Purple
14             };
15             pgb.WrapMode = WrapMode.TileFlipXY;
16           g.FillEllipse(pgb, new Rectangle(50, 100, 250, 200));
17            pgb.Dispose();
18            gp.Dispose();
19         }

 

 


 

新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"
 MyQBlog   浏览(2787)   评论(0)   关键字
  
Copyright © 2010-2020 power by CYQ.Blog - 秋色园 v2.0 All Rights Reserved