title: How to create a bmp file from byte[] in C#
body: <p>I have a byte[] array received in TCP Client.The array contains a 24-bit RGB Bitmap file.How to create that bitmap file with given Width ,Height and data?</p>
  
  <p>In C++ I use this</p>
  
  <pre><code>int WriteBitmapFile(const char *filename, int width, int height, unsigned char *imageData)
  {
  FILE             *filePtr;        // file pointer
  BITMAPFILEHEADER bitmapFileHeader;    // bitmap file header
  BITMAPINFOHEADER bitmapInfoHeader;    // bitmap info header
  DWORD                 imageIdx;    // used for swapping RGB-&gt;BGR
  unsigned char     tempRGB;            // used for swapping
  
  // open file for writing binary mode
  filePtr = fopen(filename, "wb");
  if (!filePtr)
      return 0;
  
  // define the bitmap file header
  bitmapFileHeader.bfSize = sizeof(BITMAPFILEHEADER);
  bitmapFileHeader.bfType = 0x4D42;
  bitmapFileHeader.bfReserved1 = 0;
  bitmapFileHeader.bfReserved2 = 0;
  bitmapFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
  
  // define the bitmap information header
  bitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
  bitmapInfoHeader.biPlanes = 1;
  bitmapInfoHeader.biBitCount = 32;                        // 24-bit
  bitmapInfoHeader.biCompression = BI_RGB;                // no compression
  bitmapInfoHeader.biSizeImage = width * abs(height) * 4;    // width * height * (RGB bytes)
  bitmapInfoHeader.biXPelsPerMeter = 0;
  bitmapInfoHeader.biYPelsPerMeter = 0;
  bitmapInfoHeader.biClrUsed = 0;
  bitmapInfoHeader.biClrImportant = 0;
  bitmapInfoHeader.biWidth = width;                        // bitmap width
  bitmapInfoHeader.biHeight = height;                    // bitmap height
  
  // switch the image data from RGB to BGR
  for(imageIdx = 0; imageIdx &lt; bitmapInfoHeader.biSizeImage; imageIdx+=4)
  {
      tempRGB = imageData[imageIdx];
      imageData[imageIdx] = imageData[imageIdx + 2];
      imageData[imageIdx + 2] = tempRGB;
  }
  
  // write the bitmap file header
  fwrite(&amp;bitmapFileHeader, 1, sizeof(BITMAPFILEHEADER), filePtr);
  
  // write the bitmap info header
  fwrite(&amp;bitmapInfoHeader, 1, sizeof(BITMAPINFOHEADER), filePtr);
  
  // write the image data
  fwrite(imageData, 1, bitmapInfoHeader.biSizeImage, filePtr);
  
  // close our file
  fclose(filePtr);
  
  // Success
  return 1;
  }
  </code></pre>
  
  <p>How could I do that in C#?</p>