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

4

我正在尝试在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个回答

12

尝试以下方法

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

这是我的结果:

表单截图 :

enter image description here

保存的位图 :

enter image description here

如您所见,表格中有一个文本框在表单上不可见,但在保存的位图中存在。


如果(像我一样)您试图在屏幕上绘制表单之前保存面板,则这也是解决方案。 - Chris Rae
你的解决方案对我有用,但是面板中的控件被绘制了两次!因此,在调用子控件之前,我使用了一个IF(控件不是面板)来修复它。 - Christian Gold

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

注意:它正常工作


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接