bmp文件操作

HBITMAP CreateBitmapObjectFromDibFile (HDC hdc, PTSTR szFileName)
{
     BITMAPFILEHEADER * pbmfh ;
     BOOL               bSuccess ;
     DWORD              dwFileSize, dwHighSize, dwBytesRead ;
     HANDLE             hFile ;
     HBITMAP            hBitmap ;

          // Open the file: read access, prohibit write access

     hFile = CreateFile (szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
                         OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL) ;

     if (hFile == INVALID_HANDLE_VALUE)
          return NULL ;

          // Read in the whole file

     dwFileSize = GetFileSize (hFile, &dwHighSize) ;

     if (dwHighSize)
     {
          CloseHandle (hFile) ;
          return NULL ;
     }

     pbmfh = (LPBITMAPFILEHEADER)malloc (dwFileSize) ;

     if (!pbmfh)
     {
          CloseHandle (hFile) ;
          return NULL ;
     }

     bSuccess = ReadFile (hFile, pbmfh, dwFileSize, &dwBytesRead, NULL) ;
     CloseHandle (hFile) ;

          // Verify the file

     if (!bSuccess || (dwBytesRead != dwFileSize)        
                   || (pbmfh->bfType != * (WORD *) "BM")
                   || (pbmfh->bfSize != dwFileSize))
     {
          free (pbmfh) ;
          return NULL ;
     }

          // Create the DDB

     hBitmap = CreateDIBitmap (hdc,             
                               (BITMAPINFOHEADER *) (pbmfh + 1),
                               CBM_INIT,
                               (BYTE *) pbmfh + pbmfh->bfOffBits,
                               (BITMAPINFO *) (pbmfh + 1),
                               DIB_RGB_COLORS) ;
     free (pbmfh) ;

     return hBitmap ;
}

 

// DDBToDIB  - Creates a DIB from a DDB
// bitmap  - Device dependent bitmap
// dwCompression - Type of compression - see BITMAPINFOHEADER
// pPal   - Logical palette
HANDLE DDBToDIB(HBITMAP hBitmap, DWORD dwCompression, CPalette* pPal )
{
 BITMAP    bm;
 BITMAPINFOHEADER bi;
 LPBITMAPINFOHEADER  lpbi;
 DWORD    dwLen;
 HANDLE    hDIB;
 HANDLE    handle;
 HDC     hDC;
 HPALETTE   hPal;

 // The function has no arg for bitfields
 if( dwCompression == BI_BITFIELDS )
  return NULL;

 // If a palette has not been supplied use defaul palette
 hPal = (HPALETTE) pPal->GetSafeHandle();
 if (hPal==NULL)
  hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);

 // Get bitmap information
 ::GetObject(hBitmap, sizeof(bm),(LPSTR)&bm);

 // Initialize the bitmapinfoheader
 bi.biSize  = sizeof(BITMAPINFOHEADER);
 bi.biWidth  = bm.bmWidth;
 bi.biHeight   = bm.bmHeight;
 bi.biPlanes   = 1;
 bi.biBitCount  = bm.bmPlanes * bm.bmBitsPixel;
 bi.biCompression = dwCompression;
 bi.biSizeImage  = 0;
 bi.biXPelsPerMeter = 0;
 bi.biYPelsPerMeter = 0;
 bi.biClrUsed  = 0;
 bi.biClrImportant = 0;

 // Compute the size of the  infoheader and the color table
 int nColors = (1 << bi.biBitCount);
 if( nColors > 256 )
  nColors = 0;
 dwLen  = bi.biSize + nColors * sizeof(RGBQUAD);

 // We need a device context to get the DIB from
 hDC = GetDC(NULL);
 hPal = SelectPalette(hDC,hPal,FALSE);
 RealizePalette(hDC);

 // Allocate enough memory to hold bitmapinfoheader and color table
 hDIB = GlobalAlloc(GMEM_FIXED,dwLen);

 if (!hDIB){
  SelectPalette(hDC,hPal,FALSE);
  ReleaseDC(NULL,hDC);
  return NULL;
 }

 lpbi = (LPBITMAPINFOHEADER)hDIB;

 *lpbi = bi;

 // Call GetDIBits with a NULL lpBits param, so the device driver
 // will calculate the biSizeImage field
 GetDIBits(hDC, hBitmap, 0L, (DWORD)bi.biHeight,
   (LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);

 bi = *lpbi;

 // If the driver did not fill in the biSizeImage field, then compute it
 // Each scan line of the image is aligned on a DWORD (32bit) boundary
 if (bi.biSizeImage == 0){
  bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
      * bi.biHeight;

  // If a compression scheme is used the result may infact be larger
  // Increase the size to account for this.
  if (dwCompression != BI_RGB)
   bi.biSizeImage = (bi.biSizeImage * 3) / 2;
 }

 // Realloc the buffer so that it can hold all the bits
 dwLen += bi.biSizeImage;
 if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
  hDIB = handle;
 else{
  GlobalFree(hDIB);

  // Reselect the original palette
  SelectPalette(hDC,hPal,FALSE);
  ReleaseDC(NULL,hDC);
  return NULL;
 }

 // Get the bitmap bits
 lpbi = (LPBITMAPINFOHEADER)hDIB;

 // FINALLY get the DIB
 BOOL bGotBits = GetDIBits( hDC, hBitmap,
    0L,    // Start scan line
    (DWORD)bi.biHeight,  // # of scan lines
    (LPBYTE)lpbi    // address for bitmap bits
    + (bi.biSize + nColors * sizeof(RGBQUAD)),
    (LPBITMAPINFO)lpbi,  // address of bitmapinfo
    (DWORD)DIB_RGB_COLORS);  // Use RGB for color table

 if( !bGotBits )
 {
  GlobalFree(hDIB);
  
  SelectPalette(hDC,hPal,FALSE);
  ReleaseDC(NULL,hDC);
  return NULL;
 }

 SelectPalette(hDC,hPal,FALSE);
 ReleaseDC(NULL,hDC);
 return hDIB;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值