title: What are the differences between a Windows bitmap and DIBSection?
body: <p>I'm loading a DIBSection from a file with the following:</p>
  
  <pre><code>HBITMAP bmpIn = (HBITMAP) LoadImage(NULL, _T("c:\\Temp\\Temp.bmp"), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE);
  </code></pre>
  
  <p>Empirically I've discovered the following differences between the loaded bitmap and the bitmaps that I've used in the past, but I can't find any documentation stating that there should be a difference.</p>
  
  <ul>
  <li>The lines are ordered in memory top down rather than bottom up. I've verified that the .bmp file itself is ordered bottom up.</li>
  <li>The row padding is to a multiple of 2 bytes rather than 4.</li>
  </ul>
  
  <p>I've also discovered a documented difference when you use <a href="http://msdn.microsoft.com/en-us/library/dd183494.aspx" rel="nofollow"><code>CreateDIBSection</code></a> to create a DIBSection from scratch.</p>
  
  <ul>
  <li>The DIBSECTION.dsHandle and BITMAP.bmBits values returned by <code>GetObject</code> will be NULL.</li>
  </ul>
  
  <p>Where's the documentation for the first two differences, and am I missing anything? This is with Windows 7 but I can't imagine it would be different for other versions of Windows.</p>
  
  <p><strong>Edit:</strong> Some additional details. Here's a hex dump of <code>temp.bmp</code>; it's a 7x7 image with a white stripe down the right side and blue values incrementing along the left (0x10,0x20,etc.). You can see that the bottom line (00,00,70) is first and that there's 3 bytes of padding.</p>
  
  <pre><code>00: 42 4d de 00 00 00 00 00 00 00 36 00 00 00 28 00
  10: 00 00 07 00 00 00 07 00 00 00 01 00 18 00 00 00
  20: 00 00 a8 00 00 00 00 00 00 00 00 00 00 00 00 00
  30: 00 00 00 00 00 00 70 00 00 00 00 00 00 00 00 00
  40: 00 00 00 00 00 00 00 00 ff ff ff 00 00 00 60 00
  50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  60: ff ff ff 00 00 00 50 00 00 00 00 00 00 00 00 00
  70: 00 00 00 00 00 00 00 00 ff ff ff 00 00 00 40 00
  80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  90: ff ff ff 00 00 00 30 00 00 00 00 00 00 00 00 00
  a0: 00 00 00 00 00 00 00 00 ff ff ff 00 00 00 20 00
  b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  c0: ff ff ff 00 00 00 10 00 00 00 00 00 00 00 00 00
  d0: 00 00 00 00 00 00 00 00 ff ff ff 00 00 00
  </code></pre>
  
  <p>Here's a sample program to read the .bmp file and write out the contents. I've removed error checking for brevity.</p>
  
  <pre><code>int _tmain(int argc, _TCHAR* argv[])
  {
     HBITMAP bmpIn = (HBITMAP) LoadImage(NULL, argv[1], IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE);
     FILE * out = _tfopen(argv[2], _T("wb"));
     DIBSECTION obj = {0};
     GetObject(bmpIn, sizeof(obj), &amp;obj);
     cout &lt;&lt; "dsBm.bmHeight = " &lt;&lt; obj.dsBm.bmHeight &lt;&lt; endl;
     cout &lt;&lt; "dsBmih.biHeight = " &lt;&lt; obj.dsBmih.biHeight &lt;&lt; endl;
     cout &lt;&lt; "sizeof(DIBSECTION) = " &lt;&lt; sizeof(DIBSECTION) &lt;&lt; endl;
     fwrite(&amp;obj, sizeof(DIBSECTION), 1, out);
     int stride = (((obj.dsBmih.biWidth * obj.dsBmih.biBitCount) + 15) / 16) * 2;
     int bytecount = abs(obj.dsBmih.biHeight) * stride;
     vector&lt;BYTE&gt; bits(bytecount);
     GetBitmapBits(bmpIn, bytecount, &amp;bits[0]);
     fwrite(&amp;bits[0], 1, bytecount, out);
     fclose(out);
     return 0;
  }
  </code></pre>
  
  <p>And here's the output from the above program along with a hex dump of the file produced:</p>
  
  <pre><code>dsBm.bmHeight = 7
  dsBmih.biHeight = 7
  sizeof(DIBSECTION) = 84
  00: 00 00 00 00 07 00 00 00 07 00 00 00 18 00 00 00
  10: 01 00 18 00 00 00 11 00 28 00 00 00 07 00 00 00
  20: 07 00 00 00 01 00 18 00 00 00 00 00 a8 00 00 00
  30: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  50: 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 00
  60: 00 00 00 00 00 00 ff ff ff 00 20 00 00 00 00 00
  70: 00 00 00 00 00 00 00 00 00 00 00 00 ff ff ff 00
  80: 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  90: 00 00 ff ff ff 00 40 00 00 00 00 00 00 00 00 00
  a0: 00 00 00 00 00 00 00 00 ff ff ff 00 50 00 00 00
  b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff ff
  c0: ff 00 60 00 00 00 00 00 00 00 00 00 00 00 00 00
  d0: 00 00 00 00 ff ff ff 00 70 00 00 00 00 00 00 00
  e0: 00 00 00 00 00 00 00 00 00 00 ff ff ff 00
  </code></pre>