View Code 
  1 static long __stdcall SaveBmp( HBITMAP hBitmap, char *FileName) {
  2 
  3     HDC                    hDC;
  4     long                nBits        = 0;
  5     short                numBits        = 0;
  6     unsigned long        szPal        = 0,
  7                         szBits        = 0,
  8                         szDIB        = 0,
  9                         nAccess        = 0
 10     BITMAP                objBitmap;
 11     BITMAPFILEHEADER    bmpHdr;  
 12     BITMAPINFOHEADER    bi;  
 13     LPBITMAPINFOHEADER    lpbi;  
 14     HANDLE                hBmp        = NULL,
 15                         hDib        = NULL,
 16                         hPal        = NULL,
 17                         hOldPal        = NULL; 
 18  
 19     hDC        = CreateDCA("DISPLAY", NULL, NULL, NULL);
 20     nBits    = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES); 
 21     DeleteDC( hDC);
 22 
 23     // calculate bit
 24          if( nBits <= 1)  numBits = 1
 25     else if( nBits <= 4)  numBits = 4
 26     else if( nBits <= 8)  numBits = 8
 27     else     numBits = 24
 28  
 29     memset( &bi, 0sizeof(BITMAPINFOHEADER));
 30     GetObject(hBitmap, sizeof(objBitmap), (LPSTR)&objBitmap);
 31     bi.biSize            = sizeof(BITMAPINFOHEADER);
 32     bi.biWidth            = objBitmap.bmWidth;
 33     bi.biHeight            = objBitmap.bmHeight;
 34     bi.biPlanes            = 1;
 35     bi.biBitCount        = numBits;
 36     bi.biCompression    = BI_RGB;
 37  
 38     szBits = ((objBitmap.bmWidth * numBits + 31/ 32* 4 * objBitmap.bmHeight;
 39  
 40     hDib    = GlobalAlloc(GHND,szBits + szPal + sizeof(BITMAPINFOHEADER)); 
 41     lpbi    = (LPBITMAPINFOHEADER)GlobalLock(hDib); 
 42     *lpbi    = bi;
 43     
 44     hPal    = GetStockObject(DEFAULT_PALETTE); 
 45     if( hPal) { 
 46         hDC        = GetDC(NULL); 
 47         hOldPal = SelectPalette(hDC, (HPALETTE)hPal, FALSE); 
 48         RealizePalette(hDC); 
 49     }
 50 
 51     GetDIBits(
 52         hDC,
 53         hBitmap,
 54         0,
 55         (unsigned long)objBitmap.bmHeight,
 56         (LPSTR)lpbi + sizeof(BITMAPINFOHEADER) + szPal,
 57         (BITMAPINFO *)lpbi,
 58          DIB_RGB_COLORS
 59         ); 
 60 
 61     if( hOldPal) { 
 62         SelectPalette( hDC, (HPALETTE)hOldPal, TRUE); 
 63         RealizePalette( hDC); 
 64         ReleaseDC( NULL, hDC); 
 65     }
 66 
 67     hBmp    = CreateFile(
 68                 FileName,
 69                 GENERIC_WRITE,
 70                 0,
 71                 NULL,
 72                 CREATE_ALWAYS,
 73                 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
 74                 NULL);
 75     if( hBmp == INVALID_HANDLE_VALUE)
 76     return 1;
 77 
 78     bmpHdr.bfType    = 0x4D42// "BM" 
 79     szDIB            = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
 80                         szPal +    szBits;  
 81     bmpHdr.bfSize        = szDIB; 
 82     bmpHdr.bfReserved1    = 0
 83     bmpHdr.bfReserved2    = 0
 84     bmpHdr.bfOffBits    = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + szPal; 
 85 
 86     WriteFile(hBmp, (void *)&bmpHdr, sizeof(BITMAPFILEHEADER), &nAccess, NULL); 
 87     WriteFile(hBmp, (void *)lpbi, szDIB, &nAccess, NULL); 
 88 
 89     GlobalUnlock(hDib); 
 90     GlobalFree(hDib); 
 91     CloseHandle(hBmp);
 92 
 93     return 0;
 94 }
 95 
 96 #define ASC_SAVEBMP                "c:\\11.bmp"
 97 
 98 static long __stdcall get_pixel_nums( POINT point, long type) {
 99 
100     /*
101     *    get a image, then read nums in it
102     *             top (mBom)
103     *    left (mTop)    .    right (mTop)
104     *            bottom (mBom)
105     */
106     HDC                sdc,
107                     mdc;
108     HBITMAP            hmap,
109                     hmemmap;
110     RECT            rect;
111     long            nx            = 0,
112                     ny            = 0,
113                     mTop        = 30,
114                     mBom        = 10;
115 
116 
117     sdc = CreateDCA( "DISPLAY"000);
118     mdc = CreateCompatibleDC(sdc);
119 
120     nx = GetDeviceCaps( sdc, HORZRES);
121     ny = GetDeviceCaps( sdc, VERTRES);
122 
123     // first get mouse position
124     if( point.x < 0 || point.y < 0)
125         GetCursorPos(    &point);
126 
127     rect.left        = point.x - mTop < 0  ? 0  : point.x - mTop;
128     rect.right        = point.x + mTop > nx ? nx : point.x + mTop;
129     rect.top        = point.y - mBom < 0  ? 0  : point.y - mBom;
130     rect.bottom        = point.y + mBom > ny ? ny : point.y + mBom;
131 
132     hmap    = CreateCompatibleBitmap(
133                 sdc,
134                 rect.right - rect.left,
135                 rect.bottom - rect.top
136                 );
137     hmemmap    = (HBITMAP)SelectObject( mdc, hmap);
138     BitBlt( mdc,
139             0,
140             0,
141             rect.right - rect.left,
142             rect.bottom - rect.top,
143             sdc,
144             rect.left,
145             rect.top,
146             SRCCOPY);
147     hmap    = (HBITMAP)SelectObject( mdc, hmemmap);
148 
149     DeleteDC(sdc);
150     DeleteDC(mdc);
151 
152     if0 < type) {
153         // save the bitmap as .bmp file
154         SaveBmp( hmap, ASC_SAVEBMP);
155     }
156 
157     return 0;
158 
posted on 2011-08-09 14:02  Goahead  阅读(303)  评论(0)    收藏  举报