PDA

View Full Version : Creating .bmp files in vc++



06-10-2000, 07:11 PM
can anyone help me create .bmp files automatically i need to dump the pictures out into .bmp files with just the c++ code and not by taking screen shots

06-16-2000, 09:36 AM
i also need help with this any suggestions would be great

alanchinese
06-21-2000, 02:03 PM
yeah, but it seems nobody is answering such questions.
one method I know up to now is, made a .ppm file, and then use paint shop pro 5 to convert it to .bmp or .gif
but I haven't tried this one coz it is difficult for me to code the .ppm file as well.
can somebody help?

Siwko
06-22-2000, 07:14 AM
You're lucky I'm feeling nice today...

Instead of explaining... I'm just posting code...

--------------------------------------------
//////////////////////////////////////////////////////////////////////////////
// Screen Dump - Print to file

void COpenGLView::OnDump()
{
CFileDialog dlg(FALSE, "bmp", "Untitled", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, "Bitmap Files (*.bmp)|*.bmp|All Files (*.*)|*.*| |");

HDC dc;
BITMAPINFO info;
BITMAPFILEHEADER header;
HBITMAP bitmap;
GLubyte *bits;
PIXELFORMATDESCRIPTOR pfd;
HGLRC rc;
CRect rect;
CFile cfBitmap;
CDC m_dc;

int pf;
int Width = 400;
int Height = 300;

if(dlg.DoModal() == IDOK)
{
m_dc.CreateCompatibleDC(NULL);
dc = m_dc.GetSafeHdc();

memset(&info, 0, sizeof(info));
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biWidth = Width;
info.bmiHeader.biHeight = Height;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = 24;
info.bmiHeader.biCompression = BI_RGB;

bitmap = CreateDIBSection(dc, &info, DIB_RGB_COLORS, (void**)&bits, NULL, 0);

SelectObject(dc, bitmap);

memset(&pfd, 0, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cRedBits = 8;
pfd.cBlueBits = 8;
pfd.cGreenBits = 8;
pfd.cDepthBits = 16;

pf = ChoosePixelFormat(dc, &pfd);
SetPixelFormat(dc, pf, &pfd);

rc = wglCreateContext(dc);
wglMakeCurrent(dc, rc);

SetMapMode(dc, MM_TEXT);

glMatrixMode(GL_PROJECTION);
glViewport(0, 0, Width, Height);
gluPerspective(45.0f, (GLdouble)Width / (GLdouble)Height, 1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW);

SetupGLPipe();
SetupFontList(Height * 0.05);
SetupSceneList();
RenderPipe();

// Save here
header.bfType = 'MB';
header.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (info.bmiHeader.biWidth * info.bmiHeader.biBitCount + 7) / 8 * info.bmiHeader.biHeight;
header.bfReserved1 = 0;
header.bfReserved2 = 0;
header.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);

cfBitmap.Open(dlg.GetPathName(), CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
cfBitmap.Write(&header, sizeof(BITMAPFILEHEADER));
cfBitmap.Write(&info, sizeof(BITMAPINFOHEADER));
cfBitmap.Write(bits, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (info.bmiHeader.biWidth * info.bmiHeader.biBitCount + 7) / 8 * info.bmiHeader.biHeight);
cfBitmap.Close();

wglDeleteContext(rc);
DeleteObject(bitmap);
m_dc.DeleteDC();

wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
SetupFontList(15);
SetupSceneList();
Invalidate(TRUE);
}
}

-----------------------------------------

Now if you need explainations, let me know... I'll help. If you can decipher it on your own, there ya go.

Have a good one!

Siwko

12-28-2000, 08:41 AM
There is a bug in the code -
cfBitmap.Write(bits, sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (info.bmiHeader.biWidth * info.bmiHeader.biBitCount + 7) / 8 *
info.bmiHeader.biHeight);

Adding the sizeof (xx) is not correct,
it have no sense and it will lead non
correct memory acces (e.g. for 640x480
res will the code crash)

Just remove the sizeof from the sumation for
proper work ...

husakm@vscht.cz

12-29-2000, 06:45 PM
An extra question I have been wondering for long, please help:
How to save the display to a BMP file with larger size than the window? That will make the following file/printing with better resolution.