Draw “ransom note style” text in a RichTextBox in C#

This example is similar to Draw “ransom note style” text where each character has a different font in C# but it writes text into a RichTextBox instead of on a PictureBox. This has several advantages:

  • The code is simpler
  • The RichTextBox can arrange text more precisely, breaking lines between words
  • You can copy and paste the text into Word

When you change the text in the TextBox, the TextBox’s TextChanged event handler calls the following DrawText method.

// Draw the text in the PictureBox.
private void DrawText()
rchText.Text = "";
foreach (char ch in txtText.Text)

This version doesn’t need to set up Bitmap and Graphics objects. It simply blanks the RichTextBox control and calls the following DrawCharacter method for each of the characters it needs to draw.

// Draw a character in a random font.
private void DrawCharacter(char ch)
const float min_size = 25;
const float max_size = 35;
// Pick the random font characteristics.
string font_name = FontNames[Rand.Next(0, FontNames.Length)];
float font_size = (float)(min_size + Rand.NextDouble() * (max_size - min_size));
FontStyle font_style = FontStyle.Regular;
if (Rand.Next(0, 2) == 1) font_style |= FontStyle.Bold;
if (Rand.Next(0, 2) == 1) font_style |= FontStyle.Italic;
//if (Rand.Next(0,2) == 1) font_style |= FontStyle.Strikeout;
//if (Rand.Next(0,2) == 1) font_style |= FontStyle.Underline;
rchText.SelectionFont = new Font(font_name, font_size, font_style);
rchText.SelectionColor = FontColors[Rand.Next(0, FontColors.Length)];

This version doesn’t need to measure and position characters. It sets the RichTextBox control’s selected font characteristics randomly and then appends the new character.


You can select the text and press Ctrl+C to copy it. For some reason, if you paste the text into a Hotmail or Gmail message, only the text gets pasted not the fonts and colors.

However, if you paste the text into Word, you get the fonts and colors. You can then select the text in Word and copy it again. Now if you paste in a Hotmail or Gmail message, you get the fonts and colors. Apparently Word supports a clipboard format that Hotmail and Gmail understand that isn’t supported by the RichTextBox. The following table shows the formats supported by each.

Format RichTextBox Word
Rich Text Format X X
Rich Text Format Without Objects X  
RTF As Text X  
System.String X X
UnicodeText X X
Text X X
Object Descriptor   X
HTML Format   X
EnhancedMetafile   X
MetaFilePict   X
Embed Source   X
Link Source   X
Link Source Descriptor   X
ObjectLink   X

I don’t know of an easy way to get the RichTextBox’s contents in a format that might work with Hotmail or Gmail.


This entry was posted in graphics, strings. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *