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 9225225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:31:28+00:00 2026-06-18T04:31:28+00:00

I am extremely new to manipulating bitmap data in C++, and I have a

  • 0

I am extremely new to manipulating bitmap data in C++, and I have a problem. I’m trying to follow this example from wikipedia. Here is the code I’m using:

#include <iostream>
#include <fstream>
#include <Windows.h>

using namespace std;

int main()
{

    //fileheader
    BITMAPFILEHEADER* bf = new BITMAPFILEHEADER;
    bf->bfType = 66;
    bf->bfSize = 70;
    bf->bfOffBits = 54;

    //infoheader
    BITMAPINFOHEADER* bi = new BITMAPINFOHEADER;
    bi->biSize = 40;
    bi->biWidth = 2;
    bi->biHeight = 2;
    bi->biPlanes = 1;
    bi->biBitCount = 24;
    bi->biCompression = 0;
    bi->biSizeImage = 16;
    bi->biXPelsPerMeter = 2835;
    bi->biYPelsPerMeter = 2835;
    bi->biClrUsed = 0;
    bi->biClrImportant = 0;

    //image data
    unsigned char* thedata = new unsigned char;
    thedata[0] = 0;
    thedata[1] = 0;
    thedata[2] = 255;

    thedata[3] = 255;
    thedata[4] = 255;
    thedata[5] = 255;

    thedata[6] = 0;
    thedata[7] = 0;

    thedata[8] = 255;
    thedata[9] = 0;
    thedata[10] = 0;

    thedata[11] = 0;
    thedata[12] = 255;
    thedata[13] = 0;

    thedata[14] = 0;
    thedata[15] = 0;

    //dc
    HDC dc = GetDC(NULL);

    //bitmap info
    BITMAPINFO* bmi = (BITMAPINFO*)bi;

    //handle to bitmap
    HBITMAP hbmp = CreateDIBitmap(dc, bi, CBM_INIT, thedata, bmi, DIB_RGB_COLORS);

    //output to bmp....?
    ofstream outFile;
    outFile.open("outtestbmp.bmp");
    outFile << hbmp;
    outFile.close();

}

I’ve been searching Google for the past couple days trying to figure out how to get this done but I still can’t seem to make it work.

This complies without errors, but the outtestbmp.bmp file in the end is unopenable. Are there any huge mistakes I’m making (probably dozens) that are preventing this from working? (I have high suspicions that using ofstream to output my bmp data is wrong).

EDIT: I’ve been told that setting bftype to 66 is wrong. What is the correct value?

Also, I’ve created a .bmp file of what the output should be. Here is the hex data for that bmp:

42 4D 46 00 00 00 00 00 00 00 36 00 00 00 28 00 00 00 02 00 00 00 02 00 00 00 01 00 18        
00 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF FF  
FF FF 00 00 FF 00 00 00 FF 00 00 00

and here is the data for my .bmp I’m outputting:

42 00 46 00 00 00 CD CD CD CD 36 00 00 00 28 00 00 00 02 00 00 00 02 00 00 00 01 00 18 
00 00 00 00 00 10 00 00 00 13 0B 00 00 13 0B 00 00 00 00 00 00 00 00 00 00 00 00 FF FF 
FF FF 00 00 FF 00 00 00 FF 00 00 00
  • 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-18T04:31:29+00:00Added an answer on June 18, 2026 at 4:31 am
    bf->bfType == 66;
    

    This is wrong on two counts. Firstly it’s the wrong value (the magic value are the bytes ‘BM’ or 0x4d42 on a little-endian machine), and secondly it’s not an assignment.

    Then:

    unsigned char* thedata = new unsigned char;
    

    Only allocating 1 char?

    And this:

    outFile << hbmp;
    

    Doesn’t do what you think it does. You’re just writing out a handle. You didn’t need to create a bitmap, you just need to write out the data structures you’ve initialised (once you’ve made them correct).

    And why are you new’ing everything? There’s no need for this stuff to be dynamically allocated (and you’re not delete’ing it either).

    I would generally do it something like this (some code removed for brevity):

    BITMAPFILEHEADER bf;
    bf.bfType = 0x4d42;
    bf.bfSize = 70;
    bf.bfOffBits = 54;
    
    //infoheader
    BITMAPINFOHEADER bi;
    bi.biSize = 40;
    bi.biWidth = 2;
    etc...
    
    //image data
    unsigned char* thedata = new unsigned char[16]; // Should really calculate this properly
    thedata[0] = 0;
    ....
    
    //output to bmp....?
    ofstream outFile;
    outFile.open("outtestbmp.bmp");
    outFile.write((char *)&bf,sizeof(bf));
    outFile.write((char *)&bi,sizeof(bi));
    outFile.write((char *)thedata, 16);
    outFile.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm extremely new to JS and have this code that I'm trying to tweak.
I'm extremely new to cocos2D development and I have hit a problem with my
I'm extremely new to Lex and the complete requirement of this problem is as
I'm extremely new to Flex and I'm stuck at this early point. I have
I'm Extremely new to this and I've been trying to get the title of
I am extremely new to javascript and I have worked on this for hours....
I'm extremely new to ios. I'm trying to learn appDelegate method to pass data
I am extremely new to Java (just started this weekend) and have a background
I am extremely new to json and have been working on this issue for
I'm extremely new to jQuery. I'm trying to have it so that the text

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.