Karl
Karl

Reputation: 75

Saving Panel as JPEG, only saving visible areas c#

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);

Upvotes: 4

Views: 7906

Answers (2)

R Muruganandhan
R Muruganandhan

Reputation: 21

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

Upvotes: 1

Stecya
Stecya

Reputation: 23266

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

Upvotes: 12

Related Questions