Draw text that's filled with smaller text in C#

This program is very similar to the example Draw hollow text in C#. See that example for most of the details.

The big difference between the two programs is the brush they use to fill the large text. This example uses the following code.

// Make a bitmap containing the brush's text.
using (Font small_font = new Font("Times New Roman", 8))
{
// See how big the text will be.
SizeF text_size = e.Graphics.MeasureString("Text", small_font);

// Make a Bitmap to hold the text.
Bitmap bm = new Bitmap(
(int)(2 * text_size.Width),
(int)(2 * text_size.Height));
using (Graphics gr = Graphics.FromImage(bm))
{
gr.Clear(Color.LightBlue);
gr.DrawString("Text", small_font, Brushes.Red, 0, 0);
gr.DrawString("Text", small_font, Brushes.Green,
text_size.Width, 0);
gr.DrawString("Text", small_font, Brushes.Blue,
-text_size.Width / 2,
text_size.Height);
gr.DrawString("Text", small_font, Brushes.DarkOrange,
text_size.Width / 2,
text_size.Height);
gr.DrawString("Text", small_font, Brushes.Blue,
1.5f * text_size.Width,
text_size.Height);
}

// Fill the path.
using (TextureBrush br = new TextureBrush(bm))
{
e.Graphics.FillPath(br, path);
}
}

This code creates a small font. It uses the e.Graphics object's MeasureString method to see how big the string "Text" will be in this font. The program then makes a Bitmap twice as wide and tall as the string.

The code then clears the Bitmap and draws the string four times at different positions and in different colors on the Bitmap.

Finally the program uses the Bitmap to make a TextureBrush and fills the GraphicsPath containing the large text.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.