CreateDIBSection bitmap does not display ok
I'm tring to display a Bitmap that I read from a file with my own format. Data is stored with 16 bitsxcolor . When I create a bitmap with CreateBitmap it displays ok if screen uses 16bitsxcolor but not in other cases.
So, I have tried to create a Device Independent Bitmap with CreateDIBSection. Image is created correctly but colors are not displayed ok. Anyone can help me?
This is the code I use to create the bitmap with CreateBitmap
im.bitmap->CreateBitmap(bm.l,bm.a,1,16,NULL);
im.bitmap->SetBitmapBits(bm.l*bm.a*2,bm.bits);
This is the code I use to create with CreateDIBSection
HDC dc= CreateCompatibleDC(NULL);
BITMAPINFO i;
ZeroMemory( &i.bmiHeader, sizeof(BITMAPINFOHEADER) );
i.bmiHeader.biWidth=bm.l; // Set size you need
i.bmiHeader.biHeight=-bm.a; // Set size you need
i.bmiHeader.biPlanes=1;
i.bmiHeader.biBitCount=16; // Can be 8, 16, 32 bpp or other number
i.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
VOID *pvBits;
HBITMAP hbmp= CreateDIBSection( dc, &i, DIB_RGB_COLORS, &pvBits, NULL, 0 );
SetDIBits(dc,hbmp,0,bm.a,bm.bits,&i,DIB_RGB_COLORS);
free(bm.bits);
im.bitmap->Attach(hbmp);
DeleteObject(dc);
[1287 byte] By [
JuanjoBP] at [2007-11-18 20:27:22]

# 5 Re: CreateDIBSection bitmap does not display ok
I wrote a test program, tested it in Win2K and Win98, everything seems OK.
#define W 100
#define H 100
void CBmpDlg::DrawTest(HBITMAP hBmp)
{
CClientDC dc(this);
HBITMAP hOldBmp;
HDC hDC;
hDC = CreateCompatibleDC(dc.m_hDC);
hOldBmp = (HBITMAP)SelectObject(hDC, hBmp);
BitBlt(dc.m_hDC, 0, 0, W, H, hDC, 0, 0, SRCCOPY);
SelectObject(hDC, hOldBmp);
DeleteDC(hDC);
}
void CBmpDlg::SetMyBits(WORD MyBits[], int nBitsLen, WORD wColor)
{
for (int ii = 0; ii < nBitsLen; ii += 1)
MyBits[ii] = wColor;
}
void CBmpDlg::OnButton1()
{
BITMAPINFO i;
HBITMAP hBmp;
WORD MyBits[W * H] = {0};
VOID *pvBits;
HDC hDC;
// create DIB
ZeroMemory(&i.bmiHeader, sizeof(BITMAPINFOHEADER));
i.bmiHeader.biWidth = W;
i.bmiHeader.biHeight = -H;
i.bmiHeader.biPlanes = 1;
i.bmiHeader.biBitCount = 16;
i.bmiHeader.biCompression = BI_RGB;
i.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
hDC = CreateCompatibleDC(NULL);
hBmp = CreateDIBSection(hDC, &i, DIB_RGB_COLORS, &pvBits, NULL, 0);
// test1: HBITMAP, Red
SetMyBits(MyBits, W * H, 0xF800);
SetDIBits(hDC, hBmp, 0, H, MyBits, &i, DIB_RGB_COLORS);
DrawTest(hBmp);
Sleep(1000);
// test2: HBITMAP, Green
SetMyBits(MyBits, W * H, 0x07C0);
SetDIBits(hDC, hBmp, 0, H, MyBits, &i, DIB_RGB_COLORS);
DrawTest(hBmp);
Sleep(1000);
// test3: CBitmap, Blue
CBitmap BmpInstance;
SetMyBits(MyBits, W * H, 0x003E);
SetDIBits(hDC, hBmp, 0, H, MyBits, &i, DIB_RGB_COLORS);
BmpInstance.Attach(hBmp);
DrawTest((HBITMAP)BmpInstance.m_hObject);
Sleep(1000);
//BmpInstance.Detach();
//DeleteObject(hBmp);
DeleteDC(hDC);
}
# 6 Re: CreateDIBSection bitmap does not display ok
I think the problem is with red color. I fill my image with all green intensity values, and same with blue, and the result is the expected, but if I do the same with red, the result is not what it should be.
I suggest you to modify your SetMyBits function with something like this.
void CBmpDlg::SetMyBits(WORD MyBits[], int nBitsLen, WORD wColor)
{
short red;
red=0x001f;
for (int ii = 0; ii < nBitsLen; ii += 1)
{
MyBits[ii] = red<<11;
red--;
if(red==0)
red=0x001f;
}
}