Thread: No more that 23???

  1. #1
    Registered User
    Join Date
    Jun 2008

    No more that 23???

    Ok I ran into a problem and Im pulling my hair out trying to get around it. Im writing a small screen capture program that saves the bitmaps to disk, here is a simplified source of what im trying to do...

    #include <windows.h>
    #include <stdio.h>
    //Copied from
    void SaveBitmap(char *szFilename,HBITMAP hBitmap)
          HDC        hdc=NULL;
          FILE*      fp=NULL;
          LPVOID     pBuf=NULL;
          BITMAPINFO bmpInfo;
          BITMAPFILEHEADER  bmpFileHeader; 
                if((pBuf = malloc(bmpInfo.bmiHeader.biSizeImage))==NULL)
                      MessageBox( NULL, "Unable to Allocate Bitmap Memory", "Error", MB_OK|MB_ICONERROR);
                GetDIBits(hdc,hBitmap,0,bmpInfo.bmiHeader.biHeight,pBuf, &bmpInfo, DIB_RGB_COLORS);       
                if((fp = fopen(szFilename,"wb"))==NULL)
                      MessageBox( NULL, "Unable to Create Bitmap File", "Error", MB_OK|MB_ICONERROR);
                if(hdc)     ReleaseDC(NULL,hdc); 
                if(pBuf)    free(pBuf); 
                if(fp)      fclose(fp);
    int WinMain(HINSTANCE h, HINSTANCE hp, LPSTR c, INT s){
    	int i;
    	for(i=1; i<=30; i++){
                    //Capture the screen
    		HDC dc = GetDC(NULL);
    		HDC cdc = CreateCompatibleDC(dc);
    		HBITMAP screen = CreateCompatibleBitmap(dc, 1280, 1024);
    		HBITMAP dummy = SelectObject(cdc, screen);
    		BitBlt(cdc, 0, 0, 1280, 1024, dc, 0, 0, SRCCOPY);
    		screen = SelectObject(cdc, dummy);
    		char TEMPFILE[255];
    		sprintf(TEMPFILE, "c:/bmp&#37;i.bmp", i);
                    //Save the HBITMAP to disk
    		SaveBitmap(TEMPFILE, screen);
    		ReleaseDC(NULL, dc);
    	return 0;
    This code works fine and captures the full screen up to 23 times, after that it only creates 1kb bitmap files with a 0x0 resolution. Just to clarify, the first 23 runs through the 'for' loop work perfect but after that it just loses it and saves empty files and shows no error.

    If anyone can help me get more than 23 runs out of this I would be grateful

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Can you post the entire code?
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Or better yet attach the text version of it to your post. Placing it all in code blocks would result in a very long post and it is not needed.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Sorry if I didnt make myself clear - I rewrote what I was trying to do into this simple example to try and get it working so what you see above is the full source of that project at the moment. It compiles and runs ok in pellesC except for the problem mentioned in my last post...

  5. #5
    train spotter
    Join Date
    Aug 2001
    near a computer
    Is the while loop the same in the actuall app?

    If the loop executes more than once you will lose the GDI objects and memory each loop as the clean up is only called once (after the while exits).
    "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
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    The code that you appear to have copied form here is complete crap.
    Calling the function even once will result in leaking and using up all available memory or file handles and leave your app in an essentially useless state.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to test if port (23) is open on a device
    By Mikey in forum C Programming
    Replies: 7
    Last Post: 05-16-2007, 01:03 PM
  2. VB to C++ problem
    By Mike_01 in forum C++ Programming
    Replies: 12
    Last Post: 04-05-2004, 04:15 AM
  3. A 23 on that c/c++ quiz
    By Terrance in forum A Brief History of
    Replies: 1
    Last Post: 11-19-2002, 08:14 PM
  4. 5158 handles, 300 threads, and 23 processes
    By Invincible in forum A Brief History of
    Replies: 15
    Last Post: 06-06-2002, 04:24 PM
  5. How to display 23 bytes long double answer in C?
    By CLIE_ZETA in forum C Programming
    Replies: 3
    Last Post: 11-18-2001, 11:11 AM

Tags for this Thread