Available in the LEADTOOLS Imaging toolkit. |
GetDIB example for C++ 4.0 and later
//This example takes a LEAD1 control (m_Lead1) with an image,
//converts it to a V5 DIB and writes out the DIB as a bitmap file
int CDIB2Dlg::MySaveFile(HGLOBAL hDIB, char * pszFileName)
{
int fp, nSize;
unsigned char *pDIB;
BITMAPFILEHEADER bfh;
PBITMAPINFOHEADER pbih;
int nDIBsize;
nDIBsize = GlobalSize(hDIB);
bfh.bfType = 0x4d42; //"BM";
bfh.bfSize = sizeof(BITMAPFILEHEADER) + nDIBsize;
bfh.bfReserved1 = 0;
bfh.bfReserved2 = 0;
pDIB = (unsigned char*)::GlobalLock(hDIB);
pbih = (PBITMAPINFOHEADER)pDIB;
// Compute the offset to the array of color indices.
bfh.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) +
pbih->biSize + pbih->biClrUsed * sizeof (RGBQUAD);
fp = _open(pszFileName, _O_RDWR | _O_CREAT | _O_BINARY, 0);//_S_IREAD | _S_IWRITE );
if (fp == 0)
return FALSE;
nSize = _write( fp, &bfh, sizeof(BITMAPFILEHEADER));
nSize = _write( fp, pDIB, nDIBsize);
_close(fp);
GlobalUnlock(hDIB);
return TRUE;
}
int CDIB2Dlg::ExampleConvertToDIB2(char * pszFileName)
{
int bRet;
HGLOBAL hDIB;
hDIB = (HGLOBAL)m_Lead1.GetDIB(DIB_BITMAPV5HEADER);
bRet = MySaveFile(hDIB, pszFileName);
GlobalFree(hDIB);
return bRet;
}