Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a bitmap

Status
Not open for further replies.

raochetan

Programmer
Aug 20, 2001
62
IN
I have created a dialog box, which will display a map reading the data from the file. A single dialog can display more than one map, with the user specifying which map is in the front and which map in the back. (like the drawings in MS Word, which can be put to front or back). Also the user can zoom in or zoom out.

For this i would like to create a draw bitmap store it in a member variable of the dialog class. If the user zoom in or zoom out, the bitmap has to be only stretched (using StretchBlt() ). Only if a map changes from foreground to background or vice-versa, the bitmap has to be redrawn.

Can anyone suggest how to do create a bitmap and store in variable.(a sample code if possible)

Thanks in advance.

raochetan
 
Take a look at the following code. If you have any problems with it just say.

CFile cFile;
unsigned char *data;
LPVOID lpBits;
char szX[3];
char szY[3];

data = (unsigned char*)malloc(DATA_SIZE);
if(data == NULL)
{
AfxMessageBox(_T("Could not allocate memory to display image"));
return TRUE;
}

// open the file and read the data and points
cFile.Open(m_szFilename, CFile::modeRead);
cFile.Read(data, DATA_SIZE);
for(int p=0; p < 4; p++)
{
cFile.Read(szX, 3);
szX[3] = '\0';

cFile.Read(szY, 3);
szY[3] = '\0';

cPoint[p].x = atoi(szX) + 6;
cPoint[p].y = atoi(szY) + 6;
}
cFile.Close();

char bmi[1500];
BITMAPINFO *bitInfo;
bitInfo = (BITMAPINFO*)bmi;

bitInfo->bmiHeader.biSize = 40;
bitInfo->bmiHeader.biWidth = 240;
bitInfo->bmiHeader.biHeight = 240;
bitInfo->bmiHeader.biPlanes = 1;
bitInfo->bmiHeader.biCompression = 0;
bitInfo->bmiHeader.biSizeImage = 0xe100;
bitInfo->bmiHeader.biXPelsPerMeter = 0;
bitInfo->bmiHeader.biYPelsPerMeter = 0;
bitInfo->bmiHeader.biClrUsed = 256;
bitInfo->bmiHeader.biClrImportant = 0;
bitInfo->bmiHeader.biBitCount = 8;

// set the colours (grey scale)
for (int i=0; i < 256; i++)
{
bitInfo->bmiColors.rgbBlue = i;
bitInfo->bmiColors.rgbGreen = i;
bitInfo->bmiColors.rgbRed = i;
bitInfo->bmiColors.rgbReserved = NULL;
}

CDC* pDC = GetDC();
m_hBitmap = CreateDIBSection(pDC->m_hDC, bitInfo, DIB_RGB_COLORS, &lpBits, NULL, NULL);
memcpy(lpBits, data, DATA_SIZE);

m_hBitmapDC.CreateCompatibleDC(pDC);
SelectObject(m_hBitmapDC, m_hBitmap);

InvalidateRect(NULL, TRUE);
free(data);
 
Thanks CraigD. I am able to display the graph. But i have a minor problem which is not related to this one. I am starting a new thread for it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top