Visual C# 2005 - 如何利用程序代码产生多变化字体之笔刷字

除了图像之外,如果要针对文字做出绘图变化,则 .Net Framework System.Drawing 命名空间中的 Graphics  Brush 类别将最是简便的。接下来,,我们将介绍如何运用笔刷来产生带有花纹与渐层效果的文字。 

 

程序范例 


图表1 


图表2 

 

我们撰写了一个程序范例,示范如何利用 Graphics 类别的 DrawString 方法来产生带有花纹与渐层效果的文字,执行结果如图表 1  2 所示,程序代码如下所示: 

SizeF textSize;
Graphics g;
Brush myBrush;
RectangleF gradientRectangle;
Font myFont = new Font("Times New Roman",
  (float)this.nudFontSize.Value, FontStyle.Regular);

g = picDemoArea.CreateGraphics();
g.Clear(Color.White);

textSize = g.MeasureString(this.txtShortText.Text, myFont);

if(this.optHatch.Checked)
{
 myBrush = new HatchBrush(HatchStyle.DiagonalBrick, Color.Blue,
   Color.Yellow);
}
else
{
gradientRectangle = new RectangleF(new PointF(0, 0), textSize);
myBrush = new LinearGradientBrush(gradientRectangle,
  Color.Blue,
  Color.Yellow, LinearGradientMode.ForwardDiagonal);
}

g.DrawString(txtShortText.Text, myFont, myBrush,
  (picDemoArea.Width - textSize.Width) / 2,
  (picDemoArea.Height - textSize.Height) / 2);

posted on 2006-11-15 13:48  章立民研究室  阅读(2292)  评论(0编辑  收藏  举报

导航