Draw ransom note text in a RichTextBox in C#

[ransom note text]

This example is similar to the post Draw ransom note text in C# but it writes text into a RichTextBox instead of on a PictureBox. This has the following advantages.

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

When you change the text in the program’s TextBox, that control’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 of the program 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 that 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 of the method doesn’t need to measure and position characters. It sets the RichTextBox control’s selected font characteristics randomly and then appends the new character. The RichTextBox does the rest.


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 control’s contents in a format that might work with Hotmail or Gmail.

Download Example   Follow me on Twitter   RSS feed   Donate

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

Leave a Reply

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