// ********************************************************************** // // ** This file was downloaded from Jeff Heaton's Source Code Examples ** // // ** www.heat-on.com/source ** // // ********************************************************************** // // Copyright 1999 by Jeff Heaton // ********************************************************************** // // Permission is granted for royaltyfree use in compiled form. No // additional licensing is required. This code may not be distributed // in source form without written premission by Jeff Heaton. Generally // I will grant it. Just ask first. // // Basically I do not care how you distribute this code in compiled form. // But if your going to publish my source code somewhere I'd like to hear // about it first. // ********************************************************************** // // SaveBitmap V1.0 // Written 1/14/1999 by Jeff Heaton // // Write bitmap handles as bitmap files(.bmp). // // Windows provides many routines for working with bitmaps. However there // is no easy way to save one as a .bmp file. The SaveBitmap routine can be // used to save bitmap handles to disk. This example uses the ErrorHandler // example. #include // Function Name: ErrorHandler // Parameters: // msg - Error message to display // Returns: // Nothing. // Purpose: // This function is used to report errors to the user. // Author // Jeff Heaton void ErrorHandler(char *msg) { LPVOID lpMsgBuf;// The GetLastError error message char *complete;// Used to build complete error message FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL ); complete = (char*)LocalAlloc( LMEM_FIXED,LocalSize(lpMsgBuf) + 2 + lstrlen(msg) ); lstrcpy(complete,msg); lstrcat(complete,"\r\n"); lstrcat(complete,(char*)lpMsgBuf); MessageBox( NULL, (const char *)complete, "Error", MB_OK|MB_ICONINFORMATION ); LocalFree( lpMsgBuf ); LocalFree( complete ); } // Function Name: SaveBitmap // Parameters: // name - Filename to save the bitmap to // bitmap - Bitmap handle to save // Returns: // TRUE if successful. // Purpose: // This is used to save a HBITMAP handle as a Windows .bmp file. // Author // Jeff Heaton BOOL SaveBitmap(char *name,HBITMAP bitmap) { HDC hdcScreen; // Device context of the screen HWND hwndScreen; // Desktop window handle int i; // Counts the number of scanlines BYTE *bits; // Pointer to the bitmap's bits BITMAPINFOHEADER *info; // Info structure about this bitmap HANDLE fp; // A file handle BITMAPFILEHEADER finfo; // File info header long ctsize,imgsize,pixels;// Various sizes needed to build the file DWORD dw; // Doubleword return code char *ptr; // Pointer used to write the opening BM hwndScreen=GetDesktopWindow(); hdcScreen = GetDC( hwndScreen ); info = (BITMAPINFOHEADER*)GlobalAlloc(GMEM_FIXED,sizeof(BITMAPFILEHEADER)+(1024*sizeof(RGBQUAD))); info->biSize = sizeof(BITMAPINFOHEADER); info->biBitCount = 0; i = GetDIBits( hdcScreen, // handle of device context bitmap, // handle of bitmap 0, // first scan line to set in destination bitmap 0, // number of scan lines to copy NULL, // address of array for bitmap bits (BITMAPINFO*)info, // address of structure with bitmap data DIB_RGB_COLORS // RGB or palette index ); if( (info->biBitCount!=4) && (info->biBitCount!=8) && (info->biBitCount!=24) ) info->biBitCount=24; info->biCompression=BI_RGB; pixels=info->biWidth*info->biHeight; switch(info->biBitCount) { case 4:ctsize=16;imgsize=pixels/2;break; case 8:ctsize=256;imgsize=pixels;break; case 16:ctsize=0;imgsize=pixels*2;break; case 24:ctsize=0;imgsize=pixels*3;break; case 32:ctsize=0;imgsize=pixels*4;break; default: ReleaseDC( hwndScreen,hdcScreen ); return FALSE; } bits = (unsigned char*)GlobalAlloc(GMEM_FIXED,imgsize); i = GetDIBits( hdcScreen, // handle of device context bitmap, // handle of bitmap 0, // first scan line to set in destination bitmap info->biHeight, // number of scan lines to copy bits, // address of array for bitmap bits (BITMAPINFO*)info, // address of structure with bitmap data DIB_RGB_COLORS // RGB or palette index ); ptr = (char *)&finfo.bfType; ptr[0]='B'; ptr[1]='M'; finfo.bfOffBits=sizeof(BITMAPFILEHEADER)+ sizeof(BITMAPINFOHEADER)+ (sizeof(RGBQUAD)*ctsize); finfo.bfSize=finfo.bfOffBits+imgsize; finfo.bfReserved1=0; finfo.bfReserved2=0; fp = CreateFile(name,// pointer to name of the file GENERIC_WRITE, // access (read-write) mode 0, // share mode NULL, // pointer to security attributes CREATE_ALWAYS,// how to create FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,// file attributes NULL // handle to file with attributes to copy ); WriteFile(fp,&finfo,sizeof(BITMAPFILEHEADER),&dw,NULL); WriteFile(fp,info,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*ctsize,&dw,NULL); WriteFile(fp,bits,imgsize,&dw,NULL); CloseHandle(fp); GlobalFree(info); GlobalFree(bits); ReleaseDC( hwndScreen,hdcScreen ); return TRUE; }