0

I have an issue with printing from a panel (C#, windows form). I created a Print button, I wrote the necessary codes for that and it seems that only one textboxes are able to shown as it is supposed to be. The rest of the controls, such as the richtextboxes are blank, even if I write into them.

Bitmap bitmap;

private void btn_Print_Click(object sender, EventArgs e)
{
    Print(this.panel_Doc_Print);
}

public void Print(Panel panel)
{
    PrinterSettings set = new PrinterSettings();
    panel_Doc_Print = panel;
    PrintArea(panel);
    print.Document = print_doc;
    print_doc.PrintPage += new PrintPageEventHandler(PrintImage);
    print.ShowDialog();
        
}

public void PrintImage(object sender, PrintPageEventArgs e)
{
    Rectangle page_area = e.PageBounds;
    e.Graphics.DrawImage(bitmap, (page_area.Width / 2) - (this.panel_Doc_Print.Width / 2), this.panel_Doc_Print.Location.Y);
}

public void PrintArea(Panel panel)
{
    bitmap = new Bitmap(panel.Width, panel.Height);
    panel.DrawToBitmap(bitmap, new Rectangle(0, 0, panel.Width, panel.Height));
}

Before the printing the Windows form looks like this: Windows form before printing.
Before I click on the print button: before
After I click on the print button the following happens: after

What did I do wrong? When I click on the button I want the program to print the whole panel, including all items and control in it. It happens only to the textboxes. As you can see on the 3rd picture the size of the printing image is quite small. I want it to roughly fill out an A4 sheet of paper.

At this point I am too confused to solve this problem alone, therefore I would like to ask for your help.

mitiko
  • 751
  • 1
  • 8
  • 20
  • 1
    The RichTextBox control doesn't print its content and [Control.DrawToBitmap()](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.drawtobitmap) has more limitations (see the Remarks section). Take the `ControlPrinter` class from here: [How to use PrintDocument with a scrollable Panel?](https://stackoverflow.com/a/57257205/7444103), it also contains a RichTextBox printer (read the notes, most of all about DpiAwarenes). – Jimi Apr 12 '21 at 11:54
  • RTBs should be the only issues; also note the other controls will print in the reversed stacking order.. – TaW Apr 12 '21 at 14:21

0 Answers0