将Panel保存为JPEG,仅保存可见区域c#

时间:2011-05-14 11:37:07

标签: c# printing jpeg

我正在尝试保存,然后在c#中打印一个面板。我唯一的问题是它只保存可见区域,当我向下滚动时会打印出来。

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

2 个答案:

答案 0 :(得分:9)

尝试以下

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

这是我的结果:

表格ScreenShot

enter image description here

保存的位图

enter image description here

正如您所看到的那样,TextBox在表单上不可见但存在于保存的位图中

答案 1 :(得分: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

注意:工作正常