2

I'm trying to print panel contents in C# windows application on button click but, I'm getting blank document have attached some screenshots and code.

enter image description here

enter image description here

My Code:

   private void BtnPrint_Click(object sender, EventArgs e)
    {
        Bitmap bitmap;
        //Add a Panel control.
        Panel panel = new Panel();
        this.Controls.Add(panel);


        Graphics grp = panel.CreateGraphics();
        Size formSize = this.ClientSize;
        bitmap = new Bitmap(formSize.Width, formSize.Height, grp);
        grp = Graphics.FromImage(bitmap);


        Point panelLocation = PointToScreen(panel.Location);
        grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize);

        //Show the Print Preview Dialog.
        printPreviewDialog1.Document = printDocument1;
        printPreviewDialog1.PrintPreviewControl.Zoom = 1;
        printPreviewDialog1.ShowDialog();
    }
RBT
  • 24,161
  • 21
  • 159
  • 240
  • What is `printDocument1`? The code creates a `Panel` and a `Graphics` and a `Bitmap`, but they have no connection whatsoever to `printDocument1`, so why should anything show up there? – Corak Feb 25 '20 at 07:29
  • have you tried to save the bitmap as a jpeg file? to see whether you captured the right content at least. Divide the problem – Mong Zhu Feb 25 '20 at 07:35
  • 1
    Assuming you have subscribed to the `printDocument1.PrintPage` event, what are you passing to the event handler? Your `Bitmap bitmap` is a local object, the handler will never receive it: use a Field object. + `Graphics grp` should not be derived from a panel and set to a Bitmap: derive it directly from the Bitmap object. + The Panel you're adding is empty & off z-order + You don't need `Graphics.CopyFromScreen()` but `Control.DrawToBitmap()`. You can take the code from here: [How to use PrintDocument with a scrollable Panel?](https://stackoverflow.com/a/57257205/7444103), it can draw anything – Jimi Feb 25 '20 at 07:56

0 Answers0