Screen capture of a Form

  • Thread starter Thread starter jcmag
  • Start date Start date
J

jcmag

I would like to take a "screen capture" of just a Form.

I've done the following:
internal void CaptureScreen()
{
Graphics g1 = CreateGraphics();
Image MyImage = new Bitmap(ClientRectangle.Width,
ClientRectangle.Height, g1);
Graphics g2 = Graphics.FromImage(MyImage);
IntPtr dc1 = g1.GetHdc();
IntPtr dc2 = g2.GetHdc();
BitBlt(dc2, 0, 0, ClientRectangle.Width,
ClientRectangle.Height, dc1, 0, 0, 13369376);
g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
MyImage.Save(@"Captured.jpg", ImageFormat.Jpeg);
}


[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
Int32 dwRop // raster operation code
);

Two questions:
1) CaptureScreen() works but in "Captured.jpg" I see my Form that is
not completely loaded (I see parts of my desktop behind some controls).
Is there a way to know if a Form is completely loaded/rendered ?
2) Then I will have to do this capture automatically and maybe without
showing the form to the user ; is it possible?

Thanks in advance
 
jcmag,

If you use .NET 2.0 try using DrawToBitmap method of the control class.

It will take care of all this PInvokes that you have in your code.

In .NET 2.0 Form fires Shown event after the form is fully painted first
time.
 
Back
Top