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

我正在尝试保存,然后在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); 

试试以下

  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

在此处输入图像描述

保存的位图

在此处输入图像描述

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

 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 

注意 :它工作正常