|
DWORD CDib::CreateDib1bits(int aWidth,int aHeight,HDC dc)//创建一个DIB文件
{
DWORD Size;
Free();
m_BMFH.bfReserved1=0;
m_BMFH.bfReserved2=0;
m_BMFH.bfOffBits=sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) *2;
m_BMFH.bfSize=sizeof(BITMAPFILEHEADER);//整个文件大小 后面会赋值
m_BMFH.bfType=DIB_HEADER_MARKER;
// 分配DIB信息头和调色板的内存
m_lpBMI = (LPBITMAPINFO)GlobalAllocPtr(GHND, m_BMFH.bfOffBits-sizeof(BITMAPFILEHEADER));
if (m_lpBMI == 0)
return 0;
// 设置信息头中的信息
m_lpBMI->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
m_lpBMI->bmiHeader.biWidth = aWidth;
m_lpBMI->bmiHeader.biHeight = aHeight;
m_lpBMI->bmiHeader.biPlanes = 1;
m_lpBMI->bmiHeader.biBitCount =1;
m_lpBMI->bmiHeader.biCompression =BI_RGB;
m_lpBMI->bmiHeader.biSizeImage = BytesPerLine()*aHeight;
m_lpBMI->bmiHeader.biXPelsPerMeter = 0;
m_lpBMI->bmiHeader.biYPelsPerMeter = 0;
m_lpBMI->bmiHeader.biClrUsed =2;
m_lpBMI->bmiHeader.biClrImportant=0;
m_lpBMI->bmiColors[0].rgbRed=255;
m_lpBMI->bmiColors[0].rgbGreen=255;
m_lpBMI->bmiColors[0].rgbBlue=255;
m_lpBMI->bmiColors[0].rgbReserved= 0;
m_lpBMI->bmiColors[1].rgbRed=0;
m_lpBMI->bmiColors[1].rgbGreen=0;
m_lpBMI->bmiColors[1].rgbBlue=0;
m_lpBMI->bmiColors[1].rgbReserved= 0;
// 计算图象数据内存的大小
DWORD BitsSize=ComputeMetrics();
CreatePalette();
Size=sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) *2+BitsSize;
m_BMFH.bfSize=Size;
MemDC=::CreateCompatibleDC(dc);
hBitmap=::CreateDIBSection(dc,m_lpBMI,DIB_RGB_COLORS,(VOID**)&m_pBits,NULL,0);
if(hBitmap!=0)
{
::SelectObject(MemDC,hBitmap);
}
else
return 0;
return Size;
}
void CDib::Free()
{
// Make sure all member data that might have been allocated is freed.
if (hBitmap)
{
DeleteObject(hBitmap) ;
hBitmap=NULL;
}
if (MemDC)
{
DeleteDC(MemDC) ;
MemDC=NULL;
}
if (m_pBits)
{
GlobalFreePtr(m_pBits);
m_pBits = NULL;
}
if (m_lpBMI)
{
GlobalFreePtr(m_lpBMI);
m_lpBMI = NULL;
}
if (m_pPalette)
{
m_pPalette->DeleteObject();
delete m_pPalette;
m_pPalette = NULL;
}
}
这个是创建一个位图的原本代码 然后呢 我不断重复调用这个代码 就会产生内存泄漏 |
|