title: Saving .bmp file using hBitmap = CreateDIBSection() in C Win32
body: <p>I have the following code. I want to use this information to create one .bmp file that shows the data from memory which is a image copied from a framegrabber into main memory, can somebody please let me?
  Thank you</p>
  
  <pre><code>LPSTR CreateBMP( HWND hAppWnd, int nImageType )
  </code></pre>
  
  <p>{<br>
      void            * pWinGBits = NULL;
      int             i;
      Z_BITMAPINFO    zWinGHeader;    //  bitmapinfo for cerating the DIB</p>
  
  <pre><code>//  create DC for bitmap.
  hDCBits = CreateCompatibleDC( ghDCMain );
  
  switch ( nImageType )
  {
      case bayer_filter:
      zWinGHeader.bmiHeader.biSize            = sizeof( BITMAPINFOHEADER );
      zWinGHeader.bmiHeader.biPlanes          = 1;
      zWinGHeader.bmiHeader.biClrImportant    = 0;
      zWinGHeader.bmiHeader.biHeight          = -lYSize;
      zWinGHeader.bmiHeader.biWidth           = lXSize;
      zWinGHeader.bmiHeader.biBitCount        = 32;
      zWinGHeader.bmiHeader.biClrUsed         = 0;//3;
      zWinGHeader.bmiHeader.biCompression     = BI_BITFIELDS;
      zWinGHeader.bmiHeader.biSizeImage       = 0;
      zWinGHeader.bmiColors[0].rgbBlue        = 0x00;
      zWinGHeader.bmiColors[0].rgbGreen       = 0x00;
      zWinGHeader.bmiColors[0].rgbRed         = 0xFF;
      zWinGHeader.bmiColors[0].rgbReserved    = 0x00;
  
      zWinGHeader.bmiColors[1].rgbBlue        = 0x00;
      zWinGHeader.bmiColors[1].rgbGreen       = 0xFF;
      zWinGHeader.bmiColors[1].rgbRed         = 0x00;
      zWinGHeader.bmiColors[1].rgbReserved    = 0x00;
  
      zWinGHeader.bmiColors[2].rgbBlue        = 0xFF;
      zWinGHeader.bmiColors[2].rgbGreen       = 0x00;
      zWinGHeader.bmiColors[2].rgbRed         = 0x00;
      zWinGHeader.bmiColors[2].rgbReserved    = 0x00;
      break;
  
      case color32:
      zWinGHeader.bmiHeader.biSize            = sizeof( BITMAPINFOHEADER );
      zWinGHeader.bmiHeader.biPlanes          = 1;
      zWinGHeader.bmiHeader.biClrImportant    = 0;
      zWinGHeader.bmiHeader.biHeight          = -lYSize;
      zWinGHeader.bmiHeader.biWidth           = lXSize/4;
      zWinGHeader.bmiHeader.biBitCount        = 32;
      zWinGHeader.bmiHeader.biClrUsed         = 0;
      zWinGHeader.bmiHeader.biCompression     = BI_BITFIELDS;
      zWinGHeader.bmiHeader.biSizeImage       = 0;
      zWinGHeader.bmiColors[0].rgbBlue        = 0x00;
      zWinGHeader.bmiColors[0].rgbGreen       = 0x00;
      zWinGHeader.bmiColors[0].rgbRed         = 0xFF;
      zWinGHeader.bmiColors[0].rgbReserved    = 0x00;
  
      zWinGHeader.bmiColors[1].rgbBlue        = 0x00;
      zWinGHeader.bmiColors[1].rgbGreen       = 0xFF;
      zWinGHeader.bmiColors[1].rgbRed         = 0x00;
      zWinGHeader.bmiColors[1].rgbReserved    = 0x00;
  
      zWinGHeader.bmiColors[2].rgbBlue        = 0xFF;
      zWinGHeader.bmiColors[2].rgbGreen       = 0x00;
      zWinGHeader.bmiColors[2].rgbRed         = 0x00;
      zWinGHeader.bmiColors[2].rgbReserved    = 0x00;
      break;
  
      case color24:
      zWinGHeader.bmiHeader.biSize            = sizeof( BITMAPINFOHEADER );
      zWinGHeader.bmiHeader.biPlanes          = 1;
      zWinGHeader.bmiHeader.biClrImportant    = 0;
      zWinGHeader.bmiHeader.biHeight          = -lYSize;
      zWinGHeader.bmiHeader.biWidth           = lXSize/3;
      zWinGHeader.bmiHeader.biBitCount        = 24;
      zWinGHeader.bmiHeader.biClrUsed         = 0;
      zWinGHeader.bmiHeader.biCompression     = BI_RGB;
      zWinGHeader.bmiHeader.biSizeImage       = 0;
      break;
  
      case color3x16:
      zWinGHeader.bmiHeader.biSize            = sizeof( BITMAPINFOHEADER );
      zWinGHeader.bmiHeader.biPlanes          = 1;
      zWinGHeader.bmiHeader.biClrImportant    = 0;
      zWinGHeader.bmiHeader.biHeight          = -lYSize;
      zWinGHeader.bmiHeader.biWidth           = lXSize/6;
      zWinGHeader.bmiHeader.biBitCount        = 32;
      zWinGHeader.bmiHeader.biClrUsed         = 0;
      zWinGHeader.bmiHeader.biCompression     = BI_BITFIELDS;
      zWinGHeader.bmiHeader.biSizeImage       = 0;
      zWinGHeader.bmiColors[0].rgbBlue        = 0x00;
      zWinGHeader.bmiColors[0].rgbGreen       = 0x00;
      zWinGHeader.bmiColors[0].rgbRed         = 0xFF;
      zWinGHeader.bmiColors[0].rgbReserved    = 0x00;
  
      zWinGHeader.bmiColors[1].rgbBlue        = 0x00;
      zWinGHeader.bmiColors[1].rgbGreen       = 0xFF;
      zWinGHeader.bmiColors[1].rgbRed         = 0x00;
      zWinGHeader.bmiColors[1].rgbReserved    = 0x00;
  
      zWinGHeader.bmiColors[2].rgbBlue        = 0xFF;
      zWinGHeader.bmiColors[2].rgbGreen       = 0x00;
      zWinGHeader.bmiColors[2].rgbRed         = 0x00;
      zWinGHeader.bmiColors[2].rgbReserved    = 0x00;
      break;
  
      case bw1x10:
      //  create bitmap-infoheader.
      zWinGHeader.bmiHeader.biSize        = sizeof( BITMAPINFOHEADER );
      zWinGHeader.bmiHeader.biPlanes      = 1;
      zWinGHeader.bmiHeader.biBitCount    = 8;
      zWinGHeader.bmiHeader.biCompression = BI_RGB;
      zWinGHeader.bmiHeader.biSizeImage   = 0;
      zWinGHeader.bmiHeader.biClrUsed     = 256;
      zWinGHeader.bmiHeader.biClrImportant= 0;
      zWinGHeader.bmiHeader.biHeight      = -lYSize;
      zWinGHeader.bmiHeader.biWidth       = lXSize/2;
  
      //  create colortable fot bitmap (grayvalues).
      for (i = 0; i &lt; 256; i++) 
      {
          zWinGHeader.bmiColors[i].rgbGreen   = i;
          zWinGHeader.bmiColors[i].rgbBlue    = i;
          zWinGHeader.bmiColors[i].rgbRed     = i;
  
          zWinGHeader.bmiColors[i].rgbReserved = 0;
      }
      break;
  
      default:
      case bw8:
      //  create bitmap-infoheader.
      zWinGHeader.bmiHeader.biSize        = sizeof( BITMAPINFOHEADER );
      zWinGHeader.bmiHeader.biPlanes      = 1;
      zWinGHeader.bmiHeader.biBitCount    = 8;
      zWinGHeader.bmiHeader.biCompression = BI_RGB;
      zWinGHeader.bmiHeader.biSizeImage   = 0;
      zWinGHeader.bmiHeader.biClrUsed     = 256;
      zWinGHeader.bmiHeader.biClrImportant= 0;
      zWinGHeader.bmiHeader.biHeight      = -lYSize;
      zWinGHeader.bmiHeader.biWidth       = lXSize;
  
      //  create colortable fot bitmap (grayvalues).
      for (i = 0; i &lt; 256; i++) 
      {
          zWinGHeader.bmiColors[i].rgbGreen   = i;
          zWinGHeader.bmiColors[i].rgbBlue    = i;
          zWinGHeader.bmiColors[i].rgbRed     = i;
  
          zWinGHeader.bmiColors[i].rgbReserved = 0;
      }
      break;
  }
  
  //  cerate identity palette 
  hPal = CreateIdentityPalette( zWinGHeader.bmiColors );
  
  //  get new palette into DC and map into physical palette register.
  hOldPal = SelectPalette( ghDCMain, hPal, FALSE);
  RealizePalette( ghDCMain );
  
  //  cerate DIB-Section f黵 direct access of image-data.
  hBitmap = CreateDIBSection(
      hDCBits,                        //  handle of device context
      (BITMAPINFO *)&amp;zWinGHeader,     //  address of structure containing
                                      //  bitmap size, format and color data
      DIB_RGB_COLORS,                 //  color data type indicator: RGB values
                                      //  or palette indices
      &amp;pWinGBits,                     //  pointer to variable to receive a pointer
                                      //  to the bitmap's bit values
      NULL,                           //  optional handle to a file mapping object
      0                               //  offset to the bitmap bit values within
                                      //  the file mapping object
  );  
  //  get bitmap into DC .
  hOldBitmap = (HBITMAP)SelectObject( hDCBits, hBitmap );
  return pWinGBits;           //  return pointer to DIB 
  </code></pre>
  
  <p>}</p>