Click to See Complete Forum and Search --> : Covert DIB to DDB


cyberninja
June 11th, 2003, 01:31 AM
I need to display 32 bit DIB on a PC with 8-bit color depth. I have have already created a DIB handle (HBITMAP) and the raw data of the bitmap. After I changed DIB to DDB by SetDIBits and used BitBlt or StretchBlt. However, the final displayed bitmap has been reversed in both horizontally and vertically. According to the StretchBlt API, I changed one height/width to negative, but it does not display mirror image. Does anybody know why or give me some suggestion? I attached some code here.
Thanks.


// get DC. hStaticSecurityID is hwnd, hdcStaticSecurityID
// is HDC, and hStaticSecurityID is HBITMAP, which is
// a 32 bit DIB
HDC hdcStaticSecurityID = GetDC(hStaticSecurityID);
if (NULL != hdcStaticSecurityID)
{

PBITMAPINFO pBmpInfo = CreateBitmapInfoStruct(
hStaticSecurityID,
m_hProvisioningSecurityID
);

if(NULL==pBmpInfo)
{
MessageBox(NULL,_T("CreateBitmapInfoStruct failed"), _T("!"),MB_OK);
}

// get dib bits:
HBITMAP hBitmap = NULL;
HDC hMemDC = NULL;

hBitmap = CreateCompatibleBitmap(
hdcStaticSecurityID,
pBmpInfo->bmiHeader.biWidth,
pBmpInfo->bmiHeader.biHeight
);

if (NULL == hBitmap)
{
MessageBox(NULL, _T("1"), _T("Error"), MB_OK);
}

hMemDC = CreateCompatibleDC(hdcStaticSecurityID);

if (NULL == hMemDC)
{
MessageBox(NULL, _T("2"), _T("Error"), MB_OK);
}


HGDIOBJ hOldBitmap = SelectObject(
hMemDC,
hBitmap
);

if (hOldBitmap == NULL)
{
MessageBox(NULL, _T("4"), _T("Error"), MB_OK);
}

// g_pBitmapData is a buffer with the raw data
// of hBitmap
int iScanline = SetDIBits(
hdcStaticSecurityID,
hBitmap,
0,
pBmpInfo->bmiHeader.biHeight,
g_pBitmapData,
pBmpInfo,
DIB_RGB_COLORS
);
if (0 == iScanline)
{
MessageBox(NULL, _T("5. SetDIBits failed"), _T("Error"), MB_OK);
}


//if (!BitBlt(
// hdcStaticSecurityID,
// 0,
// 0,
// pBmpInfo->bmiHeader.biWidth,
// pBmpInfo->bmiHeader.biHeight,
// hMemDC,
// 0,
// 0,
// SRCCOPY
// ))
//{
// MessageBox(NULL, _T("6 BitBlt failed"), _T("Error"), MB_OK);
//}

// ERROR! the following code display the bitmap
// reversely in both
// X and Y coordinations
if (!StretchBlt(
hdcStaticSecurityID,
0,
0,
pBmpInfo->bmiHeader.biWidth,
pBmpInfo->bmiHeader.biHeight,
hMemDC,
0,
0,
pBmpInfo->bmiHeader.biWidth,
pBmpInfo->bmiHeader.biHeight,
SRCCOPY
))
{
MessageBox(NULL, _T("6 StretchBlt failed"), _T("Error"), MB_OK);
}

DeleteObject(SelectObject(hMemDC, hOldBitmap));
DeleteObject(hBitmap);
DeleteDC(hMemDC);

ReleaseDC(hStaticSecurityID, hdcStaticSecurityID);

if (NULL != pBmpInfo)
{
LocalFree(pBmpInfo);
pBmpInfo = NULL;
}

}