Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving Panel as JPEG, only saving visible areas c#

Tags:

c#

printing

jpeg

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);
like image 943
Karl Avatar asked Dec 13 '22 13:12

Karl


1 Answers

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

like image 67
Stecya Avatar answered Dec 24 '22 23:12

Stecya