Results 1 to 6 of 6

Thread: Creating .bmp files in vc++

  1. #1
    Guest

    Creating .bmp files in vc++

    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

  2. #2
    Guest

    Re: Creating .bmp files in vc++

    i also need help with this any suggestions would be great

  3. #3
    Junior Member Newbie
    Join Date
    May 2000
    Location
    Davis,CA,USA
    Posts
    20

    Re: Creating .bmp files in vc++

    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?
    From Alan.

  4. #4
    Senior Member Regular Contributor
    Join Date
    May 2000
    Location
    Batavia, NY, 14020
    Posts
    234

    Re: Creating .bmp files in vc++

    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
    - I am not opensource! -

  5. #5
    Guest

    Re: Creating .bmp files in vc++

    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

  6. #6
    Guest

    Re: Creating .bmp files in vc++

    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.

Similar Threads

  1. Replies: 2
    Last Post: 10-20-2007, 02:15 PM
  2. Creating viewer for .3ds-files
    By hulapupp in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 01-14-2005, 02:33 AM
  3. Creating OpenGL data object files
    By LDB in forum OpenGL: Linux
    Replies: 1
    Last Post: 12-06-2004, 12:16 AM
  4. Creating a ChaseCamera in OGL & Creating multiple cameras
    By Nuno Miguel Silva in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 08-03-2004, 12:45 AM
  5. Creating a loading function for ASE files
    By cmos in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 06-08-2001, 10:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean