C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-15-2008, 09:09 AM   #1
Registered User
 
Join Date: Jun 2008
Posts: 2
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...

Code:
#include <windows.h>
#include <stdio.h>

//Copied from http://www.geocities.com/krishnapg/bitmap.html
void SaveBitmap(char *szFilename,HBITMAP hBitmap)
{
      HDC        hdc=NULL;
      FILE*      fp=NULL;
      LPVOID     pBuf=NULL;
      BITMAPINFO bmpInfo;
      BITMAPFILEHEADER  bmpFileHeader; 
      do{ 
            hdc=GetDC(NULL);
            ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
            bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
            GetDIBits(hdc,hBitmap,0,0,NULL,&bmpInfo,DIB_RGB_COLORS); 
            if(bmpInfo.bmiHeader.biSizeImage<=0)
				bmpInfo.bmiHeader.biSizeImage=bmpInfo.bmiHeader.biWidth*abs(bmpInfo.bmiHeader.biHeight)*
				(bmpInfo.bmiHeader.biBitCount+7)/8;

            if((pBuf = malloc(bmpInfo.bmiHeader.biSizeImage))==NULL)
            {
                  MessageBox( NULL, "Unable to Allocate Bitmap Memory", "Error", MB_OK|MB_ICONERROR);
                  break;
            }            
            bmpInfo.bmiHeader.biCompression=BI_RGB;
            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);
                  break;
            } 

            bmpFileHeader.bfReserved1=0;
            bmpFileHeader.bfReserved2=0;
            bmpFileHeader.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+bmpInfo.bmiHeader.biSizeImage;
            bmpFileHeader.bfType='MB';
            bmpFileHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); 
            fwrite(&bmpFileHeader,sizeof(BITMAPFILEHEADER),1,fp);
            fwrite(&bmpInfo.bmiHeader,sizeof(BITMAPINFOHEADER),1,fp);
            fwrite(pBuf,bmpInfo.bmiHeader.biSizeImage,1,fp); 

      }while(FALSE); 
            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%i.bmp", i);
		
                //Save the HBITMAP to disk
		SaveBitmap(TEMPFILE, screen);
	
		DeleteObject(screen);
		DeleteDC(cdc);
		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
bubba_169 is offline   Reply With Quote
Old 06-15-2008, 10:00 AM   #2
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 2,929
Can you post the entire code?
__________________
He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet
abachler is offline   Reply With Quote
Old 06-15-2008, 10:32 AM   #3
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,472
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.
Bubba is offline   Reply With Quote
Old 06-15-2008, 10:49 AM   #4
Registered User
 
Join Date: Jun 2008
Posts: 2
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...
bubba_169 is offline   Reply With Quote
Old 06-20-2008, 09:11 PM   #5
train spotter
 
Join Date: Aug 2001
Location: near a computer
Posts: 3,359
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
novacain is offline   Reply With Quote
Old 06-22-2008, 01:25 PM   #6
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,476
The code that you appear to have copied form here http://www.geocities.com/krishnapg/bitmap.html 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
iMalc is offline   Reply With Quote
Reply

Tags
bitmap, capture, save

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:05 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22