如何将DDB位图转化为DIB位图? 我想要将一个显示设备兼容的DDB位图保存成文件,请问如何转换成DIB位图后保存? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 好办,BCGControlBAR上面使用的代码,相信没什么问题,这个是保存DIB文件//************************************************************************************static BOOL WriteDIB( LPCTSTR szFile, HANDLE hDIB){ BITMAPFILEHEADER hdr; LPBITMAPINFOHEADER lpbi; if (!hDIB) return FALSE; CFile file; if( !file.Open (szFile, CFile::modeWrite | CFile::modeCreate)) { return FALSE; } lpbi = (LPBITMAPINFOHEADER) hDIB; int nColors = 1 << lpbi->biBitCount; if (nColors > 256 || lpbi->biBitCount == 32) nColors = 0; // Fill in the fields of the file header hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM" hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr ); hdr.bfReserved1 = 0; hdr.bfReserved2 = 0; hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize + nColors * sizeof(RGBQUAD)); // Write the file header file.Write( &hdr, sizeof(hdr) ); // Write the DIB header and the bits file.Write( lpbi, GlobalSize(hDIB) ); return TRUE;}这个是DDB转换成DIB//********************************************************************************static HANDLE DDBToDIB (HBITMAP bitmap, DWORD dwCompression) { 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; hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE); // Get bitmap information ::GetObject(bitmap, sizeof(bm),(LPSTR)&bm); // Initialize the bitmapinfoheader bi.biSize = sizeof(BITMAPINFOHEADER); bi.biWidth = bm.bmWidth; bi.biHeight = bm.bmHeight; bi.biPlanes = 1; bi.biBitCount = (WORD) (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 || bi.biBitCount == 32) nColors = 0; dwLen = bi.biSize + nColors * sizeof(RGBQUAD); // We need a device context to get the DIB from hDC = ::CreateCompatibleDC (NULL); if (hDC == NULL) { return FALSE; } HBITMAP bmp = ::CreateBitmap (1, 1, 1, bi.biBitCount, NULL); if (bmp == NULL) { ::DeleteDC(hDC); return NULL; } HBITMAP hOldBitmap = (HBITMAP)::SelectObject (hDC, bmp); 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); if (hOldBitmap != NULL) { ::SelectObject (hDC, hOldBitmap); } ::DeleteObject (bmp); ::DeleteDC(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, bitmap, 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; handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE); if (handle != NULL) hDIB = handle; else{ GlobalFree(hDIB); // Reselect the original palette SelectPalette(hDC,hPal,FALSE); if (hOldBitmap != NULL) { ::SelectObject (hDC, hOldBitmap); } ::DeleteObject (bmp); ::DeleteDC(hDC); return NULL; } // Get the bitmap bits lpbi = (LPBITMAPINFOHEADER)hDIB; // FINALLY get the DIB BOOL bGotBits = GetDIBits( hDC, bitmap, 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); if (hOldBitmap != NULL) { ::SelectObject (hDC, hOldBitmap); } ::DeleteObject (bmp); ::DeleteDC(hDC); return NULL; } // Convert color table to the standard 3-d colors: DWORD* pColorTable = (DWORD*)(((LPBYTE)lpbi) + (UINT) lpbi->biSize); for (int iColor = 0; iColor < nColors; iColor ++) { pColorTable[iColor] = CBCGPToolBarImages::MapFromSysColor (pColorTable[iColor]); } SelectPalette(hDC,hPal,FALSE); if (hOldBitmap != NULL) { ::SelectObject (hDC, hOldBitmap); } ::DeleteObject (bmp); ::DeleteDC(hDC); return hDIB;} 关于画笔的设置 VC++项目开发全程实例 关于ActiveX控件如何导出自动化对象 资深开发人员寻兼职 学习<<com技术内幕>>的问题. 如何将一个给定区域rect中用GDI画出来的图像转成bmp保存起来? 程序员初学者求助!!!自认为是高手的请进来呀! 低手请教高手:为什么这个程序不能捕捉WM_PAINT 类似网址解析的功能 怎样才能用键盘全局钩钩住写字板(怪问题奉上50分) 动态创建一个LISTBOX窗口,如何对其进行子类化,使其能够自绘制出彩色文本,请高手指教 为什么在VC中用float a = 4.1会出现warning?
//************************************************************************************
static BOOL WriteDIB( LPCTSTR szFile, HANDLE hDIB)
{
BITMAPFILEHEADER hdr;
LPBITMAPINFOHEADER lpbi; if (!hDIB)
return FALSE; CFile file;
if( !file.Open (szFile, CFile::modeWrite | CFile::modeCreate))
{
return FALSE;
} lpbi = (LPBITMAPINFOHEADER) hDIB; int nColors = 1 << lpbi->biBitCount;
if (nColors > 256 || lpbi->biBitCount == 32)
nColors = 0; // Fill in the fields of the file header
hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize +
nColors * sizeof(RGBQUAD)); // Write the file header
file.Write( &hdr, sizeof(hdr) ); // Write the DIB header and the bits
file.Write( lpbi, GlobalSize(hDIB) ); return TRUE;
}这个是DDB转换成DIB
//********************************************************************************
static HANDLE DDBToDIB (HBITMAP bitmap, DWORD dwCompression)
{
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; hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE); // Get bitmap information
::GetObject(bitmap, sizeof(bm),(LPSTR)&bm); // Initialize the bitmapinfoheader
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = (WORD) (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 || bi.biBitCount == 32)
nColors = 0;
dwLen = bi.biSize + nColors * sizeof(RGBQUAD); // We need a device context to get the DIB from
hDC = ::CreateCompatibleDC (NULL);
if (hDC == NULL)
{
return FALSE;
} HBITMAP bmp = ::CreateBitmap (1, 1, 1, bi.biBitCount, NULL);
if (bmp == NULL)
{
::DeleteDC(hDC);
return NULL;
} HBITMAP hOldBitmap = (HBITMAP)::SelectObject (hDC, bmp); 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); if (hOldBitmap != NULL)
{
::SelectObject (hDC, hOldBitmap);
} ::DeleteObject (bmp);
::DeleteDC(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, bitmap, 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;
handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE);
if (handle != NULL)
hDIB = handle;
else{
GlobalFree(hDIB); // Reselect the original palette
SelectPalette(hDC,hPal,FALSE);
if (hOldBitmap != NULL)
{
::SelectObject (hDC, hOldBitmap);
}
::DeleteObject (bmp);
::DeleteDC(hDC);
return NULL;
} // Get the bitmap bits
lpbi = (LPBITMAPINFOHEADER)hDIB; // FINALLY get the DIB
BOOL bGotBits = GetDIBits( hDC, bitmap,
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);
if (hOldBitmap != NULL)
{
::SelectObject (hDC, hOldBitmap);
}
::DeleteObject (bmp);
::DeleteDC(hDC);
return NULL;
} // Convert color table to the standard 3-d colors:
DWORD* pColorTable = (DWORD*)(((LPBYTE)lpbi) + (UINT) lpbi->biSize);
for (int iColor = 0; iColor < nColors; iColor ++)
{
pColorTable[iColor] = CBCGPToolBarImages::MapFromSysColor (pColorTable[iColor]);
} SelectPalette(hDC,hPal,FALSE); if (hOldBitmap != NULL)
{
::SelectObject (hDC, hOldBitmap);
} ::DeleteObject (bmp);
::DeleteDC(hDC); return hDIB;
}