Thread: Writing Raw Bitmap File

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    Writing Raw Bitmap File

    I've been doing win32 for a little while now and I've just encountered a bit of a problem making a program to take a screenshot. Even after finding a convenient msdn article: http://msdn.microsoft.com/library/de...tmaps_7zfp.asp I was still unable to do this. Currently with the help of these MSDN functions, it writes a corrupted (black?) bitmap file. Several other attempts at creating the headers and getting it all written out resulted in empty or corrupted. The souce is attached, originally I was just screwing around with the effect repeated BitBlt's GetDC(GetDesktopWindow()) but now I want to get that into a bitmap file.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    void Screenshot(HDC hDC, HWND hwnd)
    {
        SIZE size; 
    	RECT rect;
    	GetClientRect(hwnd, &rect);
        size.cx = GetSystemMetrics(SM_CXSCREEN);
        size.cy = GetSystemMetrics(SM_CYSCREEN);
    	HDC hdcMemory = GetDC(GetDesktopWindow());
        HBITMAP hBitmap = CreateCompatibleBitmap(hdcMemory, size.cx, size.cy);
        if(hBitmap) {
    		BitBlt(hDC, 0, 0, size.cx, size.cy, hdcMemory, 0, 0, SRCCOPY);
    		//Save(hwnd, CreateInfoHeader(hwnd, hBitmap), hBitmap, hDC);
            DeleteDC(hdcMemory);        
            DeleteObject(hBitmap);
        }
        ReleaseDC(NULL, hDC);
    }
    You're saving an empty bitmap! You copy the screen image to your window but not the memory bitmap. Here is an edited, commented but untested version of your function:
    Code:
    void Screenshot(HDC hDC, HWND hwnd)
    {
    	RECT rect;
    	GetClientRect(hwnd, &rect);  /* Not currently used */
    
    	/* Get the size of the screen */
    	SIZE size; 
    	size.cx = GetSystemMetrics(SM_CXSCREEN);
    	size.cy = GetSystemMetrics(SM_CYSCREEN);
    
    	/* Get a device context for the screen */
    	HDC hdcScreen = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
    
    	/* Create a memory bitmap and put it in a memory DC so we can draw on it */
    	HDC hdcShot = CreateCompatibleDC(hdcScreen);
    	HBITMAP hBitmapShot = CreateCompatibleBitmap(hdcScreen, size.cx, size.cy);
    	HBITMAP hBitmapOld = (HBITMAP) SelectObject(hdcShot, hBitmapShot);
    
    	/* Copy the screen image to our memory bitmap */
    	BitBlt(hdcShot, 0, 0, size.cx, size.cy, hdcScreen, 0, 0, SRCCOPY);
    
    	/* You can copy the screen to your window (hDC) here if needed */
    	/* BitBlt(hDC, 0, 0, size.cx, size.cy, hdcScreen, 0, 0, SRCCOPY); */
    
    	/* Save our memory bitmap to file */
    	Save(hwnd, CreateInfoHeader(hwnd, hBitmapShot), hBitmapShot, hdcShot);
    
    	/* Cleanup memory bitmap and DC */
    	SelectObject(hdcShot, hBitmapOld);
    	DeleteObject(hBitmapShot);
    	DeleteDC(hdcShot);
    
    	/* Cleanup screen DC */
    	DeleteDC(hdcScreen);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Writing Bitmap Files
    By HappyDude in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2001, 05:48 PM