0

I am trying to create a Bitmap from a RichTextBox and set it as the background image for a panel, but unfortunately the text is not shown.

Bitmap l_bitmap = new Bitmap(m_control.Width, m_control.Height);
m_control.DrawToBitmap(l_bitmap, new Rectangle(0, 0, l_bitmap.Width, l_bitmap.Height));
m_panel.BackgroundImage = l_bitmap;
m_panel.Refresh();

m_control is my RichTextBox. When I debug, I can see that the control contains the text I wrote, but the bitmap just shows an empty RichTextBox.

I use the same code for other types of controls (Button, CheckBox, TextBox...). The text is shown with no problems.

CharithJ
  • 46,289
  • 20
  • 116
  • 131
schmimona
  • 849
  • 3
  • 20
  • 40
  • There are a few controls in the toolbox that do not support DrawToBitmap(), WebBrowser and RichTextBox don't. There is a good reason they don't, if you don't tell us why this very unusual code is important to you then very high odds that an alternative anybody here proposes won't get the job done either. – Hans Passant Jun 01 '17 at 06:53
  • I am trying to create a sort of GUI designer, in which the user (just like in Visual Studio), controls in a preview screen drags-and-drops and their properties changes. For that I created a class in which a control and a panel are defined. I am creating the control in code, changing its properties, creating a bitmap for it and setting that bitmap as the background image of the panel. – schmimona Jun 01 '17 at 07:01
  • According to [an answer here](https://stackoverflow.com/a/30674417/5097178), there's a newer version of RichTextBox that does support DrawToBitmap. Perhaps you can look into it. – Bloopy Jun 03 '17 at 02:12

1 Answers1

0

Well you are trying to create a bitmap from the control. The text you put in there isn't the control, so it won't bother to chow it as bitmap. Try to create a picture from screen (like a screenshot).

Example:

Graphics gr = Graphics.FromImage(l_bitmap);
gr.CopyFromScreen(m_control.PointToScreen(Point.Empty),  point.Empty, m_control.Size);

This will make a bitmap from your given points. This will additional show you the text.

EDIT


Maybe you can use this instead. In addition to your idea, I simply put a label onto my panel. (L for Label and P for Panel) Screenshot from Form1 in Designermode

As you can see, the label is empty because I cleared the Text property. Now, when you click one of the buttons below the panel, it will update the label.Text propertie and there will be the text you gave the control.

Here is some example:

Screenshot from Form1 in debug mode

As you can see, the label shows the Name of the control. Completly custom as you can see on my source code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

    }

    public RichTextBox tmpRtf = new RichTextBox();

    //Poor button name incoming...
    private void button1_Click(object sender, EventArgs e)
    {
        if (tmpRtf == null)
            tmpRtf = new RichTextBox();

        //You can add any text here and it will be shown on the label.
        this.tmpRtf.Text = "Richtextbox";
        this.UpdatePanel(this.tmpRtf);
    }

    //Custom method to update the panel for any control. Can pobably be done way better than this, but hey.
    private void UpdatePanel(object pControl)
    {
        //Checks if control is a rtf
        if(pControl is RichTextBox)
        {
            //This is your code! Ay.
            Bitmap l_bitmap = new Bitmap(this.panel1.Width / 2, this.panel1.Height / 2);
            (pControl as RichTextBox).DrawToBitmap(l_bitmap, new Rectangle(0, 0, l_bitmap.Width, l_bitmap.Height));
            this.tmpRtf.BackColor = Color.LightGray;

            this.panel1.BackgroundImage = l_bitmap;
            this.panel1.BackgroundImageLayout = ImageLayout.Center;

            this.labelControlName.Text = this.tmpRtf.Text;

            this.panel1.Refresh();
        }
    }
}

Its not possible to show text on a control thats not visualized. But you can build a workaround! Or, instead of taking a picture you can simply create the control on top of it, that will also show the Text and maybe the user can test it (e.g. click on buttons, look at the control behaviour).

Hopefully this is something to get you inspired that there are always more ways to accomplish.

Cataklysim
  • 637
  • 6
  • 21
  • The control is not on the screen. I just create it in code, add the text, try to get a bitmap from it in order to set it as the background image of the panel (which is on the screen). – schmimona Jun 01 '17 at 06:20
  • Oh wow. Explain what you are trying to achieve, so maybe we can understand that better. Because, it doesn't sound like the best solution for your problem. – Cataklysim Jun 01 '17 at 06:23
  • I am trying to create a sort of GUI designer, in which the user (just like in Visual Studio), controls in a preview screen drags-and-drops and their properties changes. For that I created a class in which a control and a panel are defined. I am creating the control in code, changing its properties, creating a bitmap for it and setting that bitmap as the background image of the panel. As I said, the code works for all other controls, just not for the RichTextBox. – schmimona Jun 01 '17 at 06:44
  • Does that need a Text to be shown on it? Why don't give the Panel a text value? – Cataklysim Jun 01 '17 at 11:47