Click to See Complete Forum and Search --> : Newbie: How do I create a DIB from an array ?


May 17th, 1999, 05:55 PM
Greetings

I have a 256x256 array containing RGB values.Each value represents one out of 256 possible shades of gray.
The problem is that i don't know how to use this array to create a DIB.
I read about CreateDIBSection() function in help but being a newbie I cannot understand how to use it.

Could anyone help me?

Thanks in advance

George

Andre
May 21st, 1999, 04:45 PM
It's very simple:



struct
{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[256];
}BMInfo;


ZeroMemory(&BMInfo, sizeof(BITMAPINFOHEADER));
BMInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);


BMInfo.bmiHeader.biWidth = 256; //X size, must be byte aligned
BMInfo.bmiHeader.biHeight = 256;//Y size


BMInfo.bmiHeader.biPlanes = 1;//always 1
BMInfo.bmiHeader.biCompression = BI_RGB;


BMInfo.bmiHeader.biBitCount = 24;//Bits per Pixel(in our sample RGB)
BMInfo.bmiHeader.biClrUsed = 0;//ignored for True Color


BYTE* pVideoBuffer = 0;//prepare pointer for DIB
HBITMAP hBitmap = ::CreateDIBSection(NULL, (BITMAPINFO*)BMInfo,
DIB_RGB_COLORS, &pVideoBuffer, NULL, 0);

if(!hBitmap)
{//error create DIB Section
}

//now you have a valid pointer to buffer, copy you picture here
//or fill it directly.
//for example copy:
memcpy(pVideoBuffer, pYouSourceBuffer, SizeX * SizeY *
BMInfo.bmiHeader.biBitCount/8);

//then you may select, bitmap in DC.
MemDC.CreateCompatibleDC(NULL);

HBITMAP hOldBitmap = ::SelectObject(MemDC.GetSafeHdc(), hBitmap);
//then draw on it DC, make BitBlt, etc.
........................................
........................................
//In this code removed error check, be carefull !




Andrey.