Thread: Create a bitmat from just an hDC?

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    62

    Create a bitmat from just an hDC?

    I know it should be possible, I need to be able to create a bitmap from just an hDC. How would I go about doing this? The bitmap will be stored in memory and not saved anywhere. Basically I want my function to just return a handle to the bitmap.

    I want to do something similar to this http://www.codeproject.com/KB/vb-int...shotInVB6.aspx. But unfortunatly due to the way the program is setup, the only data coming in is the DC of the device that Im taking the screenshot of.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Is this what you need??

    Code:
    #include <windows.h>
    
    #define BITMAPSIZE  120
    
    LRESULT CALLBACK MyWndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
    
    DWORD* dwBitmapArray;
    BITMAPINFO bmInfo;
    HBITMAP theBitmapFromHDC;
    HDC theCompatibleDC;
    BYTE bRed = 0xff;
    BYTE bGreen = 0x00;
    BYTE bBlue = 0x00;
    
    char szClassName[ ] = "BMPfromHDC";
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
        WNDCLASS wc;
        HWND hwnd;
        MSG msg;
    
        ZeroMemory(&wc, sizeof(WNDCLASS));
        wc.hInstance = hInstance;
        wc.lpfnWndProc = MyWndProc;
        wc.lpszClassName = szClassName;
        wc.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
        RegisterClass(&wc);
    
        hwnd = CreateWindowEx (
            0,    
            szClassName,  
            "BMP from HDC Application",  
            WS_OVERLAPPEDWINDOW, 
            CW_USEDEFAULT,       
            CW_USEDEFAULT,       
            450,                 
            450,                 
            HWND_DESKTOP,        
            NULL,                
            hInstance,       
            NULL                 
            );
        ShowWindow (hwnd, nShowCmd);
        while(GetMessage(&msg, hwnd, 0,0))
            DispatchMessage(&msg);
        return 0;
    }
    
    LRESULT CALLBACK MyWndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
    {
        PAINTSTRUCT ps;
        int iIndex;
        switch(msg)
        {
            case WM_CREATE:
                dwBitmapArray = (DWORD*) malloc(BITMAPSIZE*BITMAPSIZE*sizeof(DWORD));
                for(iIndex = 0; iIndex < BITMAPSIZE * BITMAPSIZE; iIndex++)
                    dwBitmapArray[iIndex] = (bRed << 16) | (bGreen << 8) | (bBlue);
                ZeroMemory(&bmInfo, sizeof(BITMAPINFO));
                bmInfo.bmiHeader.biBitCount = 32;
                bmInfo.bmiHeader.biHeight = BITMAPSIZE;
                bmInfo.bmiHeader.biPlanes = 1;
                bmInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
                bmInfo.bmiHeader.biWidth = BITMAPSIZE;
                bmInfo.bmiHeader.biCompression = BI_RGB;
                theBitmapFromHDC = CreateCompatibleBitmap(GetDC(hwnd), BITMAPSIZE, BITMAPSIZE);
                theCompatibleDC = CreateCompatibleDC(GetDC(hwnd));
                SelectObject(theCompatibleDC, theBitmapFromHDC);
                SetDIBits(theCompatibleDC, theBitmapFromHDC, 0, BITMAPSIZE, dwBitmapArray, &bmInfo, 0);
                break;
            case WM_PAINT:
                BeginPaint(hwnd, &ps);
                BitBlt(ps.hdc, 0, 0, BITMAPSIZE, BITMAPSIZE, theCompatibleDC, 0, 0, SRCCOPY);
                EndPaint(hwnd, &ps);
                break;
            case WM_DESTROY:
                PostQuitMessage(0);
                exit(0);
                break;
            default:
                return DefWindowProc(hwnd, msg, wparam, lparam);
        }
        return lparam;
    }

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    You cant create a bitmap from an HDC, they are two different concepts. a bitmap is an image, an HDC is a handle to a device context, usually for the video display. Basically its a special purpose pointer to the display interface, but not to the frme buffer or anything in particular, its shoudl be treated as logically opaque.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    Quote Originally Posted by BobS0327 View Post
    Is this what you need??
    No, as the question has no sense at all..
    A HDC is just used to select a bitmap.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by abachler View Post
    You cant create a bitmap from an HDC, they are two different concepts. a bitmap is an image, an HDC is a handle to a device context, usually for the video display. Basically its a special purpose pointer to the display interface, but not to the frme buffer or anything in particular, its shoudl be treated as logically opaque.
    Couldn't you use the HDC to create a compatible HDC,
    create a compatible BMP,
    select the BMP into the created HDC (catching the original BMP),
    use bitblt to copy the image to your created BMP?

    Then selecting the orginal BMP frees your bitmap from the HDC, leaving only the clean up.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    Quote Originally Posted by novacain View Post
    Couldn't you use the HDC to create a compatible HDC,
    create a compatible BMP,
    select the BMP into the created HDC (catching the original BMP),
    use bitblt to copy the image to your created BMP?

    Then selecting the orginal BMP frees your bitmap from the HDC, leaving only the clean up.
    This was kind of what I was wondering. Because BitBlt only needs the HDC, a width, and a height. I was just wondering if there was any way to get a height without having the hwnd. I know it won't work always, but for my purposes it would be good enough.

    So from another POV would there be any way to determine a width and a height of some image on the HDC. Is there like a value that is always returned when you go out of bounds our something?

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    GetObject() with BITMAP struct will get the height and width (and other data) of the bitmap.

    This will require you to 'push' the BMP out of the original HDC. This is done by creating a temp BMP, compatible with the original HDC and then use SelectObject(), catching the HBITMAP returned.

    You can then call GetObject() on the HBITMAP, filling the BITMAP struct
    SelectObject() the HBITMAP back into the original HDC
    DeleteObject() the temp BMP you created.

    Note that the temp BMP is only used to 'push' the BMP of interest out of the HDC while you get data from it.

    Now create your compatible HDC
    compatible BMP using the size
    etc.....
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    Quote Originally Posted by novacain View Post
    GetObject() with BITMAP struct will get the height and width (and other data) of the bitmap.

    This will require you to 'push' the BMP out of the original HDC. This is done by creating a temp BMP, compatible with the original HDC and then use SelectObject(), catching the HBITMAP returned.

    You can then call GetObject() on the HBITMAP, filling the BITMAP struct
    SelectObject() the HBITMAP back into the original HDC
    DeleteObject() the temp BMP you created.

    Note that the temp BMP is only used to 'push' the BMP of interest out of the HDC while you get data from it.

    Now create your compatible HDC
    compatible BMP using the size
    etc.....
    Will that work on, say, an opengl window. Or a DX window? (I know, now Im just being lazy, I could test this for myself)

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well, I think if you actually ever read the MSDN docs you'd not even need to ask the question without performing some sort of test. But since I see this is only your fifth post and you already have described yourself as being too lazy to try something, I will just let you take this opportunity to not be lazy and to try it out on your own.

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    Quote Originally Posted by master5001 View Post
    Well, I think if you actually ever read the MSDN docs you'd not even need to ask the question without performing some sort of test. But since I see this is only your fifth post and you already have described yourself as being too lazy to try something, I will just let you take this opportunity to not be lazy and to try it out on your own.
    You know, if you aren't going to contribute to the thread, please don't just crap on it. I came here because I was inexperienced with windows programming and wanted to ask questions and learn how to be a better programmer, not to be insulted and have unhelpful comments thrown at me.

    I asked the question to see if what I was doing was correct for the situation. I have searched the MSDN and quite frankly find it unhelpful for my situation as it doesn't describe every function very well (Yes, it does a good job of describing most functions, but not all).

    Yes, I did use google to search for this exact subject, and no I haven't had much luck on finding it.

    By saying I was lazy I ment that I am a student and have classes but would have been happy to come back from them an see a helpful post about it. I wasn't saying that I wanted you guys to write my functions for me or do all me programming for me.

    Please contribute to a thread, don't crap on it. (BTW, Im not new to the internet, just this forum)
    Last edited by Cogman; 09-16-2008 at 04:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  2. How to create a file association program?
    By eShain in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2006, 12:15 PM
  3. Cannot create MDI Client Win
    By JaWiB in forum Windows Programming
    Replies: 1
    Last Post: 10-31-2005, 10:05 PM
  4. button in function
    By algi in forum Windows Programming
    Replies: 1
    Last Post: 03-21-2005, 11:12 PM
  5. GetPixel, HDC, and heartattacks
    By Epo in forum Game Programming
    Replies: 23
    Last Post: 12-28-2004, 11:35 AM