Search code examples
c#controlsdrawtobitmap

richTextBox.DrawToBitmap Does Not Draw Containing Text?


If I have a richTextBox and run DrawToBitmap on it, it doesn't draw any of the text inside of the richTextBox.

Bitmap b = new Bitmap(rtb.Width, rtb.Height);
inputControl.DrawToBitmap(b, new Rectangle(0, 0, b.Width, b.Height));

Is there any way to fix this?


Solution

  • This thread came up second in Google. Seems to have exactly what you want. Because I imagine you're using this inside your function from this question Accepting Form Elements As Method Arguments?, it's probably best to do something like this.

    if(inputControl is RichTextBox)
    {
        //do specifc magic here
    }
    else
    {
        //general case
    }
    

    You can check for a Control containing RichTextBox recursively

    bool ContainsOrIsRichTextBox(Control inputControl)
    {
        if(inputControl is RichTextBox) return true;
        foreach(Control control in inputControl.Controls)
        {
            if(ContainsOrIsRichTextBox(control)) return true;
        }
        return false;
    }
    

    I haven't compiled this, and there's a way of doing it without risking a StackOverflowException, but this should get you started.