bool CCStaticDrawDlg::SaveBitmapFile(CString strFileName, CBitmap &bmp,CDC *pdc)
{
BITMAP bm;
bmp.GetBitmap (&bm);
int nWidth = bm.bmWidth;
int nHeight = bm.bmHeight;
int nLineBits = bm.bmWidthBytes;
if ((nLineBits % 8) != 0)
nLineBits += 8 - nLineBits%8;
int nBitCounts = nLineBits * bm.bmHeight ;
int nBits = 24;//bm.bmBitsPixel;
int nBitmapInfoSize = 0;
if (nBits <= 8)
{
int nColors = 1 << nBits;
int nPalUnitSize = sizeof(RGBQUAD);
nBitmapInfoSize = sizeof(BITMAPINFOHEADER) + nPalUnitSize * nColors;
}
else
nBitmapInfoSize = sizeof(BITMAPINFOHEADER);
BITMAPINFO* pbmpinfo = NULL;
pbmpinfo =(BITMAPINFO *)(new BYTE[nBitmapInfoSize]);
ZeroMemory((void *)pbmpinfo,nBitmapInfoSize);
BITMAPINFOHEADER* pInfoHead = (BITMAPINFOHEADER *)pbmpinfo;
ZeroMemory((void *)pInfoHead,sizeof(BITMAPINFOHEADER));
pInfoHead->biSize = sizeof(BITMAPINFOHEADER);
pInfoHead->biBitCount = nBits;
pInfoHead->biPlanes = 1;
pInfoHead->biSizeImage = nBitCounts;
pInfoHead->biCompression = BI_RGB;
pInfoHead->biWidth = nWidth;
pInfoHead->biHeight = nHeight;
CDC dc;
dc.CreateDC("DISPLAY",NULL,NULL,NULL);
int nLx = dc.GetDeviceCaps (LOGPIXELSX);
int nLy = dc.GetDeviceCaps (LOGPIXELSY);
double dbInchPerMeter = 39.375;
int nPMx = (int)((double)nLx * dbInchPerMeter);
int nPMy = (int)((double)nLy * dbInchPerMeter);
pInfoHead->biXPelsPerMeter = nPMx;//3780;
pInfoHead->biYPelsPerMeter = nPMy;//3780;
if (nBits <= 8)
{
int nColors = 1 << nBits;
GetDIBColorTable(dc.m_hDC,0,nColors,pbmpinfo->bmiColors);
}
BYTE* pBits = NULL;
// HGLOBAL hGlobal = ::GlobalAlloc(GMEM_FIXED, nBitCounts);
// pBits = (BYTE *)::GlobalLock(hGlobal);
pBits=new byte[nBitCounts];
ZeroMemory((void *)pBits,nBitCounts);
GetDIBits(pdc->m_hDC,(HBITMAP)bmp,0,nHeight,pBits,pbmpinfo,DIB_RGB_COLORS);
BITMAPFILEHEADER bmfHeader;
int nFileHeadSize = sizeof(BITMAPFILEHEADER);
ZeroMemory((void *)&bmfHeader,sizeof(BITMAPFILEHEADER));
bmfHeader.bfType = 19778;//'BM' ((WORD) ('M' << 8) | 'B');
bmfHeader.bfSize = nFileHeadSize + nBitmapInfoSize + nBitCounts;
bmfHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + nBitmapInfoSize;
CFile file;
if(!file.Open (strFileName,CFile::modeCreate | CFile::modeWrite))
{
AfxMessageBox("Write File Error!");
return FALSE;
}
file.Write (&bmfHeader,sizeof(BITMAPFILEHEADER));
file.Write (pbmpinfo,nBitmapInfoSize);
file.WriteHuge(pBits, nBitCounts);
file.Close ();
delete pBits;
// ::GlobalUnlock (hGlobal);
// ::GlobalFree (hGlobal);
delete[] pbmpinfo;
return TRUE;
}