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);

本文出自 “章立民” 博客,转载请与作者联系!

你可能感兴趣的:(职场,C#,休闲,Visual,笔刷字)