将表单捕获到图像

我有一个带控件的表单,我需要将此表单捕获到图像中。 请帮我。 谢谢。

//Control cntrl; previously declared and populated Bitmap bmp = new Bitmap(cntrl.Width,cntrl.Height); cntrl.DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size)); 

这对我来说(差不多)。 我注意到很多时候图像只是空白。 当我在网上搜索时,似乎很多人都遇到了webBrowser控件的这个问题。 事实certificate,有时需要将初始图像清除为此控件的纯色。 我真的不知道为什么,但一旦初始化,我会得到更加一致的结果。 我还没有得到一张空白图像。

请注意我附加的代码,用于将初始图像清除为全黑。

  try { string fn = Path.Combine(Path.GetTempPath(), "Error_screen.png"); Bitmap bmp = new Bitmap(internalBrowser.Width, internalBrowser.Height); // In order to use DrawToBitmap, the image must have an INITIAL image. // Not sure why. Perhaps it uses this initial image as a mask??? Dunno. using (Graphics G = Graphics.FromImage(bmp)) { G.Clear(Color.Black); } internalBrowser.DrawToBitmap(bmp, new Rectangle(0, 0, internalBrowser.Width, internalBrowser.Height)); bmp.Save(fn, ImageFormat.Png); } catch { // handle exceptions here. return ""; } 

一个非常有趣的旁注(与我见过的其他解决方案相比):这适用于我的控件,这是我从未展示过的表格! 我的表单作为无头网页浏览器存在,但这种实际forms从未见过光明的一天。