CreateCompatibleBitmap returns NULL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Raed Sawalha

    CreateCompatibleBitmap returns NULL

    I'm capturing the screen using the following code , but the problem i have
    that after 43 calls of the functions the NULL reference object is returned ,


    SIZE size;

    //Here we get the handle to the desktop device context.
    IntPtr hDC =
    PlatformInvokeU SER32.GetDC(Pla tformInvokeUSER 32.GetDesktopWi ndow());
    //Here we make a compatible device context in memory for screen device
    context.
    IntPtr hMemDC = PlatformInvokeG DI32.CreateComp atibleDC(hDC);
    //We pass SM_CXSCREEN constant to GetSystemMetric s to get the X coordinates
    of screen.
    size.cx =
    PlatformInvokeU SER32.GetSystem Metrics(Platfor mInvokeUSER32.S M_CXSCREEN);
    //We pass SM_CYSCREEN constant to GetSystemMetric s to get the Y coordinates
    of screen.
    size.cy =
    PlatformInvokeU SER32.GetSystem Metrics(Platfor mInvokeUSER32.S M_CYSCREEN);

    //We create a compatible bitmap of screen size and using screen device
    context.
    m_HBitmap = PlatformInvokeG DI32.CreateComp atibleBitmap(hD C, size.cx, size.cy);

    //Here m_HBitmap return be NULL(Zero) after 43 calls of the function...WHY?

    //As m_HBitmap is IntPtr we can not check it against null. For this purspose
    IntPtr.Zero is used.
    if (m_HBitmap!=Int Ptr.Zero)
    {
    //Here we select the compatible bitmap in memeory device context and keeps
    the refrence to Old bitmap.
    IntPtr hOld = (IntPtr) PlatformInvokeG DI32.SelectObje ct(hMemDC, m_HBitmap);
    //We copy the Bitmap to the memory device context.
    PlatformInvokeG DI32.BitBlt(hMe mDC, 0, 0,size.cx,size. cy, hDC, 0, 0,
    PlatformInvokeG DI32.SRCCOPY);
    //We select the old bitmap back to the memory device context.
    PlatformInvokeG DI32.SelectObje ct(hMemDC, hOld);
    //We delete the memory device context.
    PlatformInvokeG DI32.DeleteDC(h MemDC);
    //We release the screen device context.
    PlatformInvokeU SER32.ReleaseDC (PlatformInvoke USER32.GetDeskt opWindow(), hDC);
    //Image is created by Image bitmap handle and returned.
    return System.Drawing. Image.FromHbitm ap(m_HBitmap);
    }
    //If m_HBitmap is null retunrn null.
    return null;
Working...