Screen of a window

  • Thread starter Christian Westerlund
  • Start date
C

Christian Westerlund

Hi!

Does anyone know how to take a "screendump" of a single window?
I have tried with some code I found on the Internet but I end up with a
picture of the screen with the size of the window. I only want the
window I specify with a handle to.

/Christian
 
M

Munir Husseini

Hi Christian,

try this:

[DllImport("User32.DLL")]

public static extern IntPtr GetActiveWindow ( );

[DllImport("gdi32.dll")]

private static extern bool BitBlt(IntPtr hdcDest,int nXDest,int nYDest,int
nWidth,int nHeight,IntPtr hdcSrc,int nXSrc,int nYSrc,System.Int32 dwRop);

[DllImport("User32.dll")]

public extern static System.IntPtr GetDC(System.IntPtr hWnd);

[DllImport("User32.dll")]

public extern static int ReleaseDC(System.IntPtr hWnd, System.IntPtr hDC);
//modified to include hWnd

private void Capture()

{

IntPtr hWnd = GetActiveWindow();

Form frm = (Form)Form.FromHandle(hWnd);

if(frm != null)

{

System.IntPtr srcDC = GetDC(frm.Handle);

Bitmap bm = new Bitmap(frm.Width,frm.Height);

Graphics g = Graphics.FromImage(bm);

System.IntPtr bmDC = g.GetHdc();

BitBlt(bmDC,0,0,bm.Width,bm.Height,srcDC,0,0,0x00CC0020 /*SRCCOPY*/);



ReleaseDC(frm.Handle, srcDC);

g.ReleaseHdc(bmDC);

g.Dispose();

bm.Save([FILENAME]);

}

}





Regards,

Munir Husseini

iCOMcept GmbH

www.icomcept.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top