title: Creating a bitmap from raw headers and image data
body: <p>I am extremely new to manipulating bitmap data in C++, and I have a problem.  I'm trying to follow <a href="http://en.wikipedia.org/wiki/BMP_file_format#Example_1" rel="nofollow">this</a> example from wikipedia.  Here is the code I'm using:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;Windows.h&gt;
  
  using namespace std;
  
  int main()
  {
  
      //fileheader
      BITMAPFILEHEADER* bf = new BITMAPFILEHEADER;
      bf-&gt;bfType = 66;
      bf-&gt;bfSize = 70;
      bf-&gt;bfOffBits = 54;
  
      //infoheader
      BITMAPINFOHEADER* bi = new BITMAPINFOHEADER;
      bi-&gt;biSize = 40;
      bi-&gt;biWidth = 2;
      bi-&gt;biHeight = 2;
      bi-&gt;biPlanes = 1;
      bi-&gt;biBitCount = 24;
      bi-&gt;biCompression = 0;
      bi-&gt;biSizeImage = 16;
      bi-&gt;biXPelsPerMeter = 2835;
      bi-&gt;biYPelsPerMeter = 2835;
      bi-&gt;biClrUsed = 0;
      bi-&gt;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 &lt;&lt; hbmp;
      outFile.close();
  
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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). </p>
  
  <p><strong>EDIT</strong>:  I've been told that setting bftype to 66 is wrong.  What is the correct value?</p>
  
  <p>Also, I've created a .bmp file of what the output should be.  Here is the hex data for that bmp:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>and here is the data for my .bmp I'm outputting:</p>
  
  <pre><code>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
  </code></pre>