Draw text filled with text in C#

text filled with text

This program draws text filled with text. It’s very similar to the example Draw hollow text in C#, so you should 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.

private void Form1_Paint(object sender, PaintEventArgs e)
    // Make things smoother.
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

    // Create the text path.
    GraphicsPath path = new GraphicsPath(FillMode.Alternate);

    // Draw text using a StringFormat to center it on the form.
    using (FontFamily font_family =
        new FontFamily("Times New Roman"))
        using (StringFormat sf = new StringFormat())
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            path.AddString("Text", font_family,
                (int)FontStyle.Bold, 200,
                this.ClientRectangle, sf);

    // Make a bitmap containing the small 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.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,
            gr.DrawString("Text", small_font, Brushes.DarkOrange, 
                text_size.Width / 2,
            gr.DrawString("Text", small_font, Brushes.Blue,
                1.5f * text_size.Width,

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

    // Outline the path.
    using (Pen pen = new Pen(Color.Black, 3))
        e.Graphics.DrawPath(pen, path);

After setting the SmoothingMode property and creating a GraphicsPath, the code creates a small font to use to fill the larger text. 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 twice tall as the string. It clears the Bitmap and draws the string four times at different positions and in different colors on the Bitmap.

The program then uses the Bitmap to make a TextureBrush and uses it to fill the GraphicsPath containing the large text.

Download Example   Follow me on Twitter   RSS feed

This entry was posted in drawing, fonts, graphics, strings and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.