0

I have a Panel that has exactly 4 picture boxes, and labels inside. I want to print all the contents inside the Panel but it is only showing the two picture boxes - in the preview.

I have this:

enter image description here

But it is only showing this (when I click the print button):

enter image description here

Here is my code for printing:

private void BtnPrint_Click(object sender, EventArgs e)
    {
        Print(this.pnlID);
    }

    public void Print(Panel pnl)
    {
        PrinterSettings ps = new PrinterSettings();
        pnlID = pnl;
        GetPrintArea(pnl);
        prntprvw.Document = printdoc;
        printdoc.PrintPage += new PrintPageEventHandler(printdoc_printpage);
        prntprvw.ShowDialog();

    }

    public void printdoc_printpage(Object sender, PrintPageEventArgs e)
    {
        Rectangle pagearea = e.PageBounds;
        e.Graphics.DrawImage(MemoryImage, (pagearea.Width / 2) - (this.pnlID.Width / 2), this.pnlID.Location.Y);
    }


    Bitmap MemoryImage;
    public void GetPrintArea(Panel pnl)
    {
        MemoryImage = new Bitmap(pnl.Width, pnl.Height);
        //Rectangle rect = new Rectangle(0, 0, pnl.Width, pnl.Height);
        pnl.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pnl.Width, pnl.Height));
    }

How can I print all the contents inside the panel?

kmandrew
  • 43
  • 6
  • It isn't obvious whether the textboxes are also inside the panel. Or it just doesn't fit the paper, [look here](https://stackoverflow.com/q/9982579/17034). – Hans Passant Apr 02 '20 at 16:18
  • Try this one: [How to use PrintDocument with a scrollable Panel?](https://stackoverflow.com/a/57257205/7444103) (it also handles RichTextBoxes). Of course, as Hans noted, the other Controls must be child of the Panel. Otherwise, print the Form's content. – Jimi Apr 02 '20 at 16:44
  • @HansPassant the textboxes isn't inside the panel, only the ID picture, the qr code, and the label inside of it. – kmandrew Apr 03 '20 at 00:21
  • Have you tried the method I linked? It draws the container's content from back to top, so it's probably what you need here. – Jimi Apr 03 '20 at 00:55
  • @Jimi I am still learning how the code works, it makes me confused since I am new to programming – kmandrew Apr 03 '20 at 01:07
  • It's a self-contained method, you just need to call it specifying the ContainerControl to draw to a Bitmap. As in the notes: `var bitmap = ScrollableControlToBitmap(this.panel1, true);`, where `this.panel1` is the Panel you want to print. Pass the actual instance of your Panel to the method (`this.pnlID`), then print the returned Bitmap as you're doing now. – Jimi Apr 03 '20 at 01:11
  • @Jimi thank you, it still quite confusing me though – kmandrew Apr 03 '20 at 01:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210841/discussion-between-kmandrew-and-jimi). – kmandrew Apr 03 '20 at 02:01

0 Answers0