GDI+ 使用笔刷绘制字体
以下将采用十字架来绘制字体。如图:
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 }
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 }
使用线性渐变绘制字体
代码如下:
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);
}
{
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 }
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
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 }
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 }
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 }
笔刷现在有3种用途,1,用于填充图形2,通过笔刷创建的钢笔来绘制图形边框,3,通过笔刷来填充字体。