title: Creating a BMP file (bitmap) in C
body: <p>I'm trying to make a bitmap in C, just from code. I'm currently trying to make a very easy .bmp image, with a height of 1px and a width of 4 pixels, with all white pixels. I have read the format description and tried to apply it. This resulted in the following code:</p>
  
  <pre><code>char bitmap[1000];
  
  void BMPmake()
  {
      // -- FILE HEADER -- //
  
      // bitmap signature
      bitmap[0] = 'B';
      bitmap[1] = 'M';
  
      // file size
      bitmap[2] = 66; // 40 + 14 + 12
      bitmap[3] = 0;
      bitmap[4] = 0;
      bitmap[5] = 0;
  
      // reserved field (in hex. 00 00 00 00)
      for(int i = 6; i &lt; 10; i++) bitmap[i] = 0;
  
      // offset of pixel data inside the image
      for(int i = 10; i &lt; 14; i++) bitmap[i] = 0;
  
      // -- BITMAP HEADER -- //
  
      // header size
      bitmap[14] = 40;
      for(int i = 15; i &lt; 18; i++) bitmap[i] = 0;
  
      // width of the image
      bitmap[18] = 4;
      for(int i = 19; i &lt; 22; i++) bitmap[i] = 0;
  
      // height of the image
      bitmap[22] = 1;
      for(int i = 23; i &lt; 26; i++) bitmap[i] = 0;
  
      // reserved field
      bitmap[26] = 1;
      bitmap[27] = 0;
  
      // number of bits per pixel
      bitmap[28] = 24; // 3 byte
      bitmap[29] = 0;
  
      // compression method (no compression here)
      for(int i = 30; i &lt; 34; i++) bitmap[i] = 0;
  
      // size of pixel data
      bitmap[34] = 12; // 12 bits =&gt; 4 pixels
      bitmap[35] = 0;
      bitmap[36] = 0;
      bitmap[37] = 0;
  
      // horizontal resolution of the image - pixels per meter (2835)
      bitmap[38] = 0;
      bitmap[39] = 0;
      bitmap[40] = 0b00110000;
      bitmap[41] = 0b10110001;
  
      // vertical resolution of the image - pixels per meter (2835)
      bitmap[42] = 0;
      bitmap[43] = 0;
      bitmap[44] = 0b00110000;
      bitmap[45] = 0b10110001;
  
      // color pallette information
      for(int i = 46; i &lt; 50; i++) bitmap[i] = 0;
  
      // number of important colors
      for(int i = 50; i &lt; 54; i++) bitmap[i] = 0;
  
      // -- PIXEL DATA -- //
      for(int i = 54; i &lt; 66; i++) bitmap[i] = 0;
  }
  
  void BMPwrite()
  {
      FILE *file;
      file = fopen("bitmap.bmp", "w+");
      for(int i = 0; i &lt; 66; i++)
      {
          fputc(bitmap[i], file);
      }
      fclose(file);
  }
  </code></pre>
  
  <p>When I try to open this image, it says that the image is damaged. Am I missing something here?</p>
  
  <p>I also noticed that the encoding of the .bmp integers is little endian. I thought that this mean that I have to reverse the order of the bytes. For example, 256 in four bytes is: 00000000 00000000 00000001 00000000, and I think in little endian this would be: 00000000 00000001 00000000 00000000</p>
  
  <p>Can anyone give me a hand here? Am I using a right approach? Any help would be appreciated!</p>
  
  <p>Thanks in advance!</p>