Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8369851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:45:41+00:00 2026-06-09T13:45:41+00:00

I am currently writing a BMP screenshot function for an OpenGL framework and am

  • 0

I am currently writing a BMP screenshot function for an OpenGL framework and am running into an odd issue writing the data out. The data being brought in from glReadPixels along with the BMP headers are all correct, but it seems the ofstream.write operation is randomly inserting invalid values.

Excerpts from a correct BMP of the image (created by Paint) and that of the one made by my function follows, highlighting the incorrect bytes. The row that is correct will always be the first one.


Row 0x08C

3B 0D 4A 3A 0A 48 38 08 45 36
3B 0D 4A 3A 0D 0A 48 38 08 45
            ^^

Row 0x0DC (already off by one at this point)

3E 2F 07 3D 2E 0A 3F 31 0E 44
07 3E 2F 07 3D 2E 0D 0A 3F 31
                  ^^

Row 0x0E68 (very next row, and off by two)

35 13 48 3A 10 44 36 0A 3F 31
0E 44 35 13 48 3A 10 44 36 0D
                           ^^

So there seems to a pattern where the invalid value is always 0x0D and it is inserted in front of a 0x0A. I have no idea why this might be happening, as I have confirmed that the headers and data from glReadPixels are all correct. The code for the method follows.

bool captureScreen( const char* name, unsigned originX, unsigned originY, unsigned width, unsigned height )
{

    //...

    GLubyte* imageData = new GLubyte[ width * height * 3 ];
    glReadPixels( originX, originY, width, height, GL_BGR, GL_UNSIGNED_BYTE, imageData );

    //...

    captureAsBMP( imageData, width, height, path.c_str( ) );

    //...
}

bool captureAsBMP( GLubyte* data, GLuint width, GLuint height, const char* path )
{
    std::ofstream stream( path, std::ios_base::out );

    if( !stream.is_open( ) )
        return false;

    unsigned char BMPHeader[ 14 ] = { /* cut for brevity */ };
    unsigned char DIBHeader[ 40 ] = { /* cut for brevity */ };
    unsigned char padding[ 4 ] = { 0x00, 0x00, 0x00, 0x00 };

    unsigned int paddingLength = 4 - (( width * 3 ) % 4);
    paddingLength = ( paddingLength == 4 ? 0 : paddingLength );

    long fileSize = ( width * height * 3 ) + ( paddingLength * height ) + 54;
    long dataSize = fileSize - 54;

    memcpy( &BMPHeader[ 2 ], &fileSize, sizeof( long ) );
    memcpy( &DIBHeader[ 4 ], &width, sizeof( unsigned int ) );
    memcpy( &DIBHeader[ 8 ], &height, sizeof( unsigned int ) );
    memcpy( &DIBHeader[ 20 ], &dataSize, sizeof( long ) );

    stream.write( reinterpret_cast< char* >( BMPHeader ), 14 );
    stream.write( reinterpret_cast< char* >( DIBHeader ), 40 );

    unsigned pos = 0;

    // Write out one row at a time
    for( int i = 0; i < height; i++ )
    {
        stream.write( reinterpret_cast< char* >( &data[ pos ] ), ( width * 3 ) );

        // Is there padding that needs to be added?
        if( paddingLength != 0 )
            stream.write( reinterpret_cast< char* >( padding ), paddingLength );

        // Update where we are in data
        pos += width * 3;
    }

    stream.close( );

    return true;
}

Also of note, the image in question is a 800×600 and so this error is occurring during the first stream write of the rows, prior to padding and pos incrementing.

Finally, how it should appear: https://i.stack.imgur.com/APfPJ.jpg

And how it does: https://i.stack.imgur.com/AHaRS.jpg (re-saved in Paint as Imgur complained of it being corrupted of course)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:45:43+00:00Added an answer on June 9, 2026 at 1:45 pm

    You need to open your files in binary mode by adding the std::ios::binary flag to avoid CRLF conversion. On Windows, newlines are represented as the two bytes 0x0D 0x0A (carriage return + linefeed, or CR LF)—the C and C++ runtimes automatically translate those into just plain linefeeds on input, and they automatically translate plain linefeeds '\n' into CRLF pairs on output, when files are opened in text mode, which is the default.

    To suppress these newline conversions, you need to tell the runtime not to translate. With C++’s iostreams, this is done with the std::ios::binary flag; with C’s FILE* streams, this is done with the "b" mode flag, e.g. "rb" for reading or "wb" for writing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently writing an application that's meant to scale with data brought in from
I'm currently writing a function what would create a zip file, which will be
I am currently writing a simple IOS app that saves tasks into a table.
I'm currently writing a billing application using EF 5 Code First, and I'm running
I'm currently writing a monad wrapping OpenGL called GL and I want to be
I am currently writing a system that stores meta data for around 140,000 ish
Im currently writing an iphone app that requires the downloading of data that needs
Our company is currently writing a GUI automation testing tool for compact framework applications.
I am currently writing a script where I want to take the data and
I am currently writing a small framework that will be used internally by other

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.