Here's some code I wrote a long time ago in C# for a screen capture application. It uses the Win32 function GetWindowRect to get the bounds of the window you want to capture, create a bitmap with that size and then use the Win32 function PrintWindow to ask the window to print itself to that bitmap:
RECT lRectangle = new RECT();
if (!GetWindowRect(lWindow.HWnd, ref lRectangle))
{
MessageBox.Show(this, "An error occured while measuring the selected window.", Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
fCapturedImage = new Bitmap(lRectangle.Right - lRectangle.Left, lRectangle.Bottom - lRectangle.Top, PixelFormat.Format32bppArgb);
using (Graphics lGraphics = Graphics.FromImage(fCapturedImage))
{
HDC lHdc = lGraphics.GetHdc();
PrintWindow(lWindow.HWnd, lHdc, 0);
lGraphics.ReleaseHdc(lHdc);
}