首先,你可以不必把DIB转换成DDB就可以让它显示在设备上。但是那样的话,你都会看见会程序运行得有多慢了,而且你也不
能使用一些有关DDB操作的函数功能了,诸如BitBlt()....
以下是从DIB创建一个DDB的基本步骤:
1.从DIB颜色表信息中的信息可以创建一个逻辑调色板,如果设备支持的话,你只需要做这一步。为了创建一个调色板,就需要
分配给逻辑调色板结构。初始化palversion和palnumentries,从DIB中的颜色表把颜色数拷贝过来。然后我们就可以使用createpalett
e()函数来使用我们初始化的逻辑调色板了。
2.把逻辑调色板选到设备中,然后实现它。
3.创建DDB,可以使用createdibbitmap()函数。
4.最后不要忘记释放掉分给逻辑调色板结构的内存。
虽然这样就可以创建一个逻辑调色板了,但它没有返回信息给所调用的程序代码。如果DIB描述的是一个256色的位图的话,而
且设备也只支持256色,那么DDB可能就不能正确的显示在设备上。那是因为系统使用的颜色数与位图的颜色数不相同,因此,
你要修改一下,在我们画位图之前,就让它返回逻辑调色板,然后我们选择和实现它到设备的上下文。
//以下是程序代码
HBITMAP CProcess::DIBtoDDB(HANDLE hdib )
{
LPBITMAPINFOHEADER lpbi;
HBITMAP hbm;
CPalette Pal;
CPalette* pOldPal;
CClientDC dc(NULL);
if (hdib == NULL)
{
return NULL;
}
lpbi = (LPBITMAPINFOHEADER)hdib;
int nColors = lpbi->biClrUsed ? lpbi->biClrUsed : 1 << lpbi->biBitCount; //颜色表项的个数
BITMAPINFO &bmInfo = *(LPBITMAPINFO)hdib;
LPVOID lpDIBits;
if(bmInfo.bmiHeader.biBitCount > 8 )
{
lpDIBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors
+ bmInfo.bmiHeader.biClrUsed)
+ ((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
}
else{
lpDIBits = (LPVOID)(bmInfo.bmiColors + nColors);
}
// create and select a logical palette if needed
if( nColors <= 256 && dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
{
UINT nSize=sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
LOGPALETTE *plp=(LOGPALETTE *) new BYTE[nSize];
plp->palVersion = 0x300;
plp->palNumEntries = nColors;
for( int i=0; i<nColors;i++)
{
plp->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed;
plp->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen;
plp->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue;
plp->palPalEntry[i].peFlags = 0;
}
Pal.CreatePalette(plp);
delete[] plp; //要及时处理
// select and realize the palette
pOldPal = dc.SelectPalette(&Pal, false);
dc.RealizePalette();
}
hbm = CreateDIBitmap(dc.GetSafeHdc(), //设备上下文的句柄
(LPBITMAPINFOHEADER)lpbi, //位图信息头指针
(long)CBM_INIT, //初始化标志
lpDIBits, //初始化数据指针
(LPBITMAPINFO)lpbi, //位图信息指针
DIB_RGB_COLORS); //颜色数据的使用方式
if (Pal.GetSafeHandle())
{
dc.SelectPalette(pOldPal,false);
}
return hbm;
} --------------------------
DDB转为DIB
把DDB转换成DIB
设备相关位图(DDB)显示方式是尽可能与显示设备驱动程序相匹配,这样,DDB不可能与其他显示设备兼容;而设备无关位图(DIB)能与所有显示设备兼容,但是,其缺点在于显示速度比较慢。
我们需要把DDB转换为DIB的一种情况是,需要将位图保存到一个文件中。下面是其实现的代码。
// DDBToDIB - Creates a DIB from a DDB
// bitmap - Device dependent bitmap(DDB)
// pPal - Logical palette
/*
HANDLE DDBToDIB(CBitmap& bitmap, DWORD dwCompression, CPalette* pPal)
{
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen; //DIB长度
HANDLE hDIB;
HANDLE handle;
HDC hDC;
HPALETTE hPal;
ASSERT(bitmap.GetSafeHandle());
// 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
bitmap.GetObject(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; //使用hDIB申请的内存
*lpbi = bi;
// Call GetDIBits with a NULL lpBits param, so the device driver
// will calculate the biSizeImage field
GetDIBits(hDC, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
//GetDIBits函数用来计算所需的内存空间的大小和得到DIB参数
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)bitmap.GetSafeHandle(),
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;
}
2006-8-21
今天又写了一个DDB转DIB的函数,它是一个象素一个象素点的读取的,相对较慢!而且只支持24位的!
HANDLE CProcess::DDBtoDIB(CBitmap &bitmap, CDC *pDC)
{
LPBITMAPINFO lpbmi;
DWORD dwHeader;
DWORD dwLen;
HANDLE hDIB;
LPSTR pDIB;
CDC memDC;
dwHeader = m_image->GetHeaderSize();
dwLen = dwHeader + m_image->GetImageSize();//获取文件大小
memDC.CreateCompatibleDC(pDC);
memDC.SelectObject(bitmap);
pDIB = (LPSTR)new char[dwLen];//该内存在SaveLayer()函数中释放
if (pDIB == NULL)//如果内存申请失败
{
return NULL;
}
lpbmi = (LPBITMAPINFO)pDIB; //使用pDIB申请的内存
int width = m_image->GetWidth();
int height = m_image->GetHeight();
if (m_image->m_lpbmi->bmiHeader.biBitCount < 24)
{
m_image->ComputePaletteSize(24);
if (m_image->m_lpbmi != NULL)
{
m_image->m_lpbmi = NULL;
delete m_image->m_lpbmi;
}
m_image->m_lpbmi = (LPBITMAPINFO)new char[sizeof(BITMAPINFOHEADER)];
if (NULL == m_image->m_lpbmi)
{
AfxMessageBox("m_lpbmi内存申请失败");
}
m_image->m_lpbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
m_image->m_lpbmi->bmiHeader.biWidth = width;
m_image->m_lpbmi->bmiHeader.biHeight = height;
m_image->m_lpbmi->bmiHeader.biPlanes = 1;
m_image->m_lpbmi->bmiHeader.biBitCount = 24;
m_image->m_lpbmi->bmiHeader.biCompression = BI_RGB;
m_image->m_lpbmi->bmiHeader.biSizeImage = 0;
m_image->m_lpbmi->bmiHeader.biXPelsPerMeter = 0;
m_image->m_lpbmi->bmiHeader.biXPelsPerMeter = 0;
m_image->m_lpbmi->bmiHeader.biClrUsed = 0;
m_image->m_lpbmi->bmiHeader.biClrImportant = 0;
m_image->ComputeImage();//重新计算图像大小
m_image->m_lpbmi->bmiHeader.biSizeImage = m_image->GetImageSize();
}
memcpy(lpbmi, m_image->m_lpbmi, dwHeader);//获取信息头
int x = 0 ,y = 0;
DWORD i = 0;
COLORREF color;
for (y = height - 1; y >= 0; y--)
{
for (x = 0; x < width; x++)
{
i = (height - 1 - y)*width + x;
color = memDC.GetPixel(x, y);
pDIB[dwHeader + i*3] = (BYTE)((color&0x00ff0000)>>16);
pDIB[dwHeader + i*3 +1] = (BYTE)((color&0x0000ff00)>>8);
pDIB[dwHeader + i*3 +2] = (BYTE)(color&0x000000ff);
}
}
hDIB = (HANDLE)pDIB;
return hDIB;
}