Capturing window image in windows server 2008

Posted by Sergey Osypchuk on Stack Overflow See other posts from Stack Overflow or by Sergey Osypchuk
Published on 2010-04-08T15:48:07Z Indexed on 2010/04/08 15:53 UTC
Read the original article Hit count: 326

Filed under:

I am capturing output of windows program using following function:

public static Bitmap Get(IntPtr hWnd, int X1, int Y1, int width, int height)
{
    WINDOWINFO winInfo = new WINDOWINFO();
    bool ret = GetWindowInfo(hWnd, ref winInfo);
    if (!ret)
    {
        return null;
    }

    int curheight = height;
    if (curheight <= 0 || curheight > winInfo.rcWindow.Height)
        curheight = winInfo.rcWindow.Height;

    int curwidth = width;
    if (curwidth <= 0 || curwidth > winInfo.rcWindow.Width)
        curwidth = winInfo.rcWindow.Width;

    if (curheight == 0 || curwidth == 0) return null;

    Graphics frmGraphics = Graphics.FromHwnd(hWnd);
    IntPtr hDC = GetWindowDC(hWnd); //gets the entire window
    //IntPtr hDC = frmGraphics.GetHdc(); -- gets the client area, no menu bars, etc..

    System.Drawing.Bitmap tmpBitmap = new System.Drawing.Bitmap(curwidth, curheight, frmGraphics);
    Graphics bmGraphics = Graphics.FromImage(tmpBitmap);
    IntPtr bmHdc = bmGraphics.GetHdc();

    BitBlt(bmHdc, 0, 0, curwidth, curheight, hDC, X1, Y1, TernaryRasterOperations.SRCCOPY);

    bmGraphics.ReleaseHdc(bmHdc);
    ReleaseDC(hWnd, hDC);

    return tmpBitmap;
}

On Development environment everything is excellent, but on windows server 2008 I have following issues: 1) When there is other window in front my - it is getting captured as well 2) When there is no user connected to RDC - image is black

On other hand, I am able to render webpage images using IE. How I can change behaviour of windows rendering process to get proper results?

© Stack Overflow or respective owner

Related posts about windows-api