将面板另存为JPEG,仅保存可见区域c#
我正在尝试保存,然后用c#打印一个面板。我唯一的问题是,它只保存可见区域,当我向下滚动时,它会打印出来将面板另存为JPEG,仅保存可见区域c#,c#,printing,jpeg,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.Im
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);
试着跟随
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);
}
以下是我的结果:
表单屏幕截图:
保存的位图:
正如您所看到的,有一个文本框,它在表单上不可见,但在保存的位图中存在
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
注意:工作正常如果(像我一样)您试图在窗体实际绘制到屏幕上之前保存面板,这也是解决方案。您的解决方案对我有效,但在绘制两次的面板中控制!因此,在校准子控件之前,我用IF(控件不是面板)修复了它。