把HBITMAP 保存图片文件.

  1 //VC下把HBITMAP保存为bmp图片   
  2 BOOL  CtestBitMapDlg::SaveBmp(HBITMAP  hBitmap,   CString  FileName)         
  3 {         
  4     HDC     hDC;         
  5     //当前分辨率下每象素所占字节数         
  6     int     iBits;         
  7     //位图中每象素所占字节数         
  8     WORD     wBitCount;         
  9     //定义调色板大小,     位图中像素字节大小     ,位图文件大小     ,     写入文件字节数             
 10     DWORD     dwPaletteSize=0,   dwBmBitsSize=0,   dwDIBSize=0,   dwWritten=0;             
 11     //位图属性结构             
 12     BITMAP     Bitmap;                 
 13     //位图文件头结构         
 14     BITMAPFILEHEADER     bmfHdr;                 
 15     //位图信息头结构             
 16     BITMAPINFOHEADER     bi;                 
 17     //指向位图信息头结构                 
 18     LPBITMAPINFOHEADER     lpbi;                 
 19     //定义文件,分配内存句柄,调色板句柄             
 20     HANDLE     fh,   hDib,   hPal,hOldPal=NULL;   
 21 
 22     int aa = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 288 * 352 * 24 / 8;
 23 
 24     //计算位图文件每个像素所占字节数             
 25     hDC  = CreateDC("DISPLAY",   NULL,   NULL,   NULL);         
 26     iBits  = GetDeviceCaps(hDC,   BITSPIXEL)     *     GetDeviceCaps(hDC,   PLANES);             
 27     DeleteDC(hDC);             
 28     if(iBits <=  1)                                                   
 29         wBitCount = 1;             
 30     else  if(iBits <=  4)                               
 31         wBitCount  = 4;             
 32     else if(iBits <=  8)                               
 33         wBitCount  = 8;             
 34     else                                                                                                                               
 35         wBitCount  = 24;             
 36 
 37     GetObject(hBitmap,   sizeof(Bitmap),   (LPSTR)&Bitmap);         
 38     bi.biSize= sizeof(BITMAPINFOHEADER);         
 39     bi.biWidth = Bitmap.bmWidth;         
 40     bi.biHeight =  Bitmap.bmHeight;         
 41     bi.biPlanes =  1;         
 42     bi.biBitCount = wBitCount;         
 43     bi.biCompression= BI_RGB;         
 44     bi.biSizeImage = Bitmap.bmWidth * Bitmap.bmHeight * 3;         
 45     bi.biXPelsPerMeter = 0;         
 46     bi.biYPelsPerMeter = 0;         
 47     bi.biClrImportant = 0;         
 48     bi.biClrUsed =  0;         
 49 
 50     dwBmBitsSize  = ((Bitmap.bmWidth *wBitCount+31) / 32)*4* Bitmap.bmHeight;         
 51 
 52     //为位图内容分配内存             
 53     hDib  = GlobalAlloc(GHND,dwBmBitsSize+dwPaletteSize+sizeof(BITMAPINFOHEADER));             
 54     lpbi  = (LPBITMAPINFOHEADER)GlobalLock(hDib);             
 55     *lpbi  = bi;             
 56 
 57     //     处理调色板                 
 58     hPal  = GetStockObject(DEFAULT_PALETTE);             
 59     if (hPal)             
 60     {             
 61         hDC  = ::GetDC(NULL);             
 62         hOldPal = ::SelectPalette(hDC,(HPALETTE)hPal, FALSE);             
 63         RealizePalette(hDC);             
 64     }         
 65 
 66     //     获取该调色板下新的像素值             
 67     GetDIBits(hDC,hBitmap, 0,(UINT)Bitmap.bmHeight,  
 68         (LPSTR)lpbi+ sizeof(BITMAPINFOHEADER)+dwPaletteSize,   
 69         (BITMAPINFO *)lpbi, DIB_RGB_COLORS);             
 70 
 71     //恢复调色板                 
 72     if (hOldPal)             
 73     {             
 74         ::SelectPalette(hDC,   (HPALETTE)hOldPal,   TRUE);             
 75         RealizePalette(hDC);             
 76         ::ReleaseDC(NULL,   hDC);             
 77     }             
 78 
 79     //创建位图文件                 
 80     fh  = CreateFile(FileName,   GENERIC_WRITE,0,   NULL,   CREATE_ALWAYS,           
 81         FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,   NULL);             
 82 
 83     if (fh     ==  INVALID_HANDLE_VALUE)         return     FALSE;             
 84 
 85     //     设置位图文件头             
 86     bmfHdr.bfType  = 0x4D42;     //     "BM"             
 87     dwDIBSize  = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+dwPaletteSize+dwBmBitsSize;                 
 88     bmfHdr.bfSize  = dwDIBSize;             
 89     bmfHdr.bfReserved1  = 0;             
 90     bmfHdr.bfReserved2  = 0;             
 91     bmfHdr.bfOffBits  = (DWORD)sizeof(BITMAPFILEHEADER)+(DWORD)sizeof(BITMAPINFOHEADER)+dwPaletteSize;             
 92     //     写入位图文件头             
 93     WriteFile(fh,   (LPSTR)&bmfHdr,   sizeof(BITMAPFILEHEADER),   &dwWritten,   NULL);             
 94     //     写入位图文件其余内容             
 95     WriteFile(fh,   (LPSTR)lpbi,   dwDIBSize - 14,   &dwWritten,   NULL);             
 96     //清除                 
 97     GlobalUnlock(hDib);             
 98     GlobalFree(hDib);             
 99     CloseHandle(fh);             
100 
101     return     TRUE;         
102 }  

 

posted on 2016-11-16 15:39  Sam.Richard  阅读(3060)  评论(0编辑  收藏  举报

导航