CopyFromScreen返回表单背后的图像

我有一个严重的问题在这里。 Iam编程一个工具,用户可以在其中为开关devise控制柜。 该小组是用面板和PictureBox,这是工作很好,看起来不错。

现在我想做一个导出function,将devise的柜子导出为pdf文件。

到目前为止,这个function很好,只在我的电脑上运行! 我使用CopyFromScreen得到一个面板的屏幕截图,其中显示了隔间,将其保存到一个文件中,并将其放到一个pdf文件中(我也尝试使用DrawToBitmap来获取面板的图片,但是这样做并不正确正在绘制一些比其他的Picturebox)。 在我的计算机上,它正确捕获了面板,并在pdf中显示了正确的图片,但是在其他任何一台计算机上,它都会显示表格背后的内容。 这是相当混乱的,因为我不知道为什么它应该这样做,为什么它在我的系统上工作。 到目前为止,我尝试了一些事情来强制窗口顶部,但没有任何工作,每个人都得到的桌面或窗口后面的图片。

码:

void takeScreenshot() { this.TopMost = true; this.BringToFront(); this.Focus(); Application.DoEvents(); System.Drawing.Rectangle bounds = Mainpanel.Bounds; bounds.Width = bounds.Width - 6; bounds.Height = bounds.Height - 4; using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height)) { using (Graphics g = Graphics.FromImage(bitmap)) { g.CopyFromScreen(Mainpanel.PointToScreen(new Point()).X + 3, Mainpanel.PointToScreen(new Point()).Y + 2, 0, 0, bounds.Size); } bitmap.Save(Application.StartupPath + "\\data\\programdata\\temppic.bmp"); } this.TopMost = false; } 

Iam其实挺绝望的,没有出口的程序是没用的。

有没有人有一个想法如何解决这个问题?

乔纳森

使用这个代码。

 void takeScreenshot() { Application.DoEvents(); System.Drawing.Rectangle bounds = Mainpanel.Bounds; bounds.Width = bounds.Width - 6; bounds.Height = bounds.Height - 4; using (Bitmap bitmap = new Bitmap(Mainpanel.Width, Mainpanel.Height)) { Mainpanel.DrawToBitmap(bitmap, new Rectangle(3, 2, bounds.Width, bounds.Height)); bitmap.Save(Application.StartupPath + "\\data\\programdata\\temppic.bmp"); } }