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...
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.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; }
If anyone can help me get more than 23 runs out of this I would be grateful![]()



LinkBack URL
About LinkBacks



