4

I'm trying to save, and then print a panel in c#. My only problem is that it only saves the visible areas and when I scroll down it prints that.

 Bitmap bmp = new Bitmap(this.panel.Width, this.panel.Height);

 this.panel.DrawToBitmap(bmp, new Rectangle(0, 0, this.panel.Width, this.panel.Height));

 bmp.Save("c:\\panel.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
pavium
  • 14,808
  • 4
  • 33
  • 50
Karl
  • 75
  • 3
  • 7

2 Answers2

12

Try following

    public void DrawControl(Control control,Bitmap bitmap)
    {
        control.DrawToBitmap(bitmap,control.Bounds);
        foreach (Control childControl in control.Controls)
        {
            DrawControl(childControl,bitmap);
        }
    }

    public void SaveBitmap()
    {
        Bitmap bmp = new Bitmap(this.panel1.Width, this.panel.Height);

        this.panel.DrawToBitmap(bmp, new Rectangle(0, 0, this.panel.Width, this.panel.Height));
        foreach (Control control in panel1.Controls)
        {
            DrawControl(control, bmp);
        }

        bmp.Save("d:\\panel.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
    }

Here is my result:

Form ScreenShot :

enter image description here

Saved bitmap :

enter image description here

As you can see there is TextBox wich is not visible on form but is present in saved bitmap

Stecya
  • 22,896
  • 10
  • 72
  • 102
  • This is also the solution if (like me) you are attempting to save the Panel before the form has actually been drawn on the screen. – Chris Rae Dec 06 '11 at 21:35
  • Your solution worked for me BUT controls in panels where drawn twice! So I fixed it with a IF( control is not a panel) before caling the child controls. – Christian Gold Feb 10 '17 at 14:48
1
Panel1.Dock = DockStyle.None // If Panel Dockstyle is in Fill mode     
Panel1.Width = 5000  // Original Size without scrollbar     
Panel1.Height = 5000 // Original Size without scrollbar      
Dim bmp As New Bitmap(Me.Panel1.Width, Me.Panel1.Height)     
Me.Panel1.DrawToBitmap(bmp, New Rectangle(0, 0, Me.Panel1.Width, Me.Panel1.Height))     
bmp.Save("C:\panel.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)      
Panel1.Dock = DockStyle.Fill

Note: Its working fine

Stecya
  • 22,896
  • 10
  • 72
  • 102