   BMP write problem
  Hello,I am trying to make a class to save into bitmap the COLORREF of each pixel but the resulting picture cannot be viewed because its corrupted, damaged or too big as the error says.I am totally confused though as I cannot find a clear description of the header part. What I have is:struct BmpFileHeader{unsigned shortType;unsigned intSize;unsigned shortReserved1,    unsigned short  Reserved2;unsigned intOffBits;};struct BmpInfoHeader{unsigned intSize;unsigned intWidth;unsigned intHeight;unsigned shortPlanes;unsigned shortBitCount;unsigned intCompression;unsigned intSizeImage;unsigned intXPPM;unsigned intYPPM;unsigned intClrUsed;unsigned intClrImportant;};I created an RGB struct as well but since I want my BMP to be 24-bit I learnt that those extra bytes aren't needed.At first I left those variables empty then I tried to fill them up but I am not sure if I have included the correct information for each one of them...I write the data like that: for(int y = Height - 1; y >= 0; --y){// Iterate horizontallyfor(int x = 0; x < Width; ++x) {    R = (BYTE)(GetRValue(Data[(y * Width) + x]));G = (BYTE)(GetGValue(Data[(y * Width) + x]));B = (BYTE)(GetBValue(Data[(y * Width) + x]));    File.write((char*)&B, sizeof(char));   File.write((char*)&G, sizeof(char));File.write((char*)&R, sizeof(char)); }    }Do I need padding added somewhere? Are the bytes assigned in my header correct? and how should I fill them up?   
  Do you mean you don't write anything in the header?What are you expecting?You have to fill them just like the other bytes. Any I'm sure there are plenty of resources and info on the net about BMP format.http://en.wikipedia.org/wiki/BMP_file_formatYou have to set header of the file in exactly the same order, as the format description says.For example:File.write((char*)&BmpFileHeader.Type,sizeof(char));File.write((int*)&BmpFileHeader.Size,sizeof(int));File.write((char*)&BmpFileHeader.Reserved1,sizeof(char));File.write((char*)&BmpFileHeader.Reserved2,sizeof(char));and so on.I'm not sure, maybe the bytes are to be swapped in the ints.   ;  At first I wasn't filling it up, now I am filling it up with:m_FileHeader.Type =  0x4D42;//'MB';m_FileHeader.Size =   sizeof(BmpFileHeader);//+sizeof(BmpInfoHeader) +8+ ((m_nWidth+31)/32)*4*m_nHeight;m_FileHeader.Reserved1=0;m_FileHeader.Reserved2=0;m_FileHeader.OffBits = sizeof(BmpInfoHeader) + sizeof(BmpFileHeader);//+8;// Store bitmap's dimensions in the headers.m_InfoHeader.Size = sizeof(BmpInfoHeader);m_InfoHeader.Planes = 1;m_InfoHeader.BitCount = 32;m_InfoHeader.Width= m_nWidth;m_InfoHeader.Height= m_nHeight;m_InfoHeader.Compression = BI_RGB;//0;m_InfoHeader.Size = m_nWidth*abs(m_nHeight)*4;m_InfoHeader.XPPM = 2835;m_InfoHeader.YPPM = 2835;m_InfoHeader.ClrUsed = 0;m_InfoHeader.ClrImportant = 0;The commented out stuff are other possible things I have tried, but none works, after I fill them in I write them like that:File.write((char*)&m_FileHeader.Type, sizeof(ushort));File.write((char*)&m_FileHeader.Size, sizeof(uint));File.write((char*)&m_FileHeader.Reserved1, sizeof(ushort));File.write((char*)&m_FileHeader.Reserved2, sizeof(ushort));File.write((char*)&m_FileHeader.OffBits, sizeof(uint));;  "Do I need padding added somewhere?"That depends on if the image is a power of 2. You can write your header sections as Tipotas688 pointed out or pack the structure using a compiler specific directive. ;  if you point to an int, the cast (*int)if uchar, then cast (*uchar)And as I mentioned, probably some integers' bytes must be swapped (see link, or whatever)swapped_uint = (original_uint >> 8) | (original_uint << 8); //I hope it's rightBTW, after you fill your structs correctly, you can use one fwrite call:File.write(&BmpFileHeader.Type, sizeof(BmpFileHeader));//address the first member, to  //address the structFile.write(&BmpInfoHeader.Size, sizeof(BmpInfoHeader));  The bmp are always 200x200 so I guess I am fine.@szecs: I followed wiki as my only resource for what I have at the moment, but still it doesn't return with a valid picture that can be used :S   ;  Ad did you do the things I suggested?And: BMP stores B-G-R, not R-G-B   ;Quote:File.write(&BmpFileHeader.Type, sizeof(BmpFileHeader));//address the first member, to//address the structFile.write(&BmpInfoHeader.Size, sizeof(BmpInfoHeader))Edit: I thought you said something different so, shouldn't that be:File.write(&m_FileHeader, sizeof(BmpFileHeader));//address the first member, to//address the structFile.write(&m_InfoHeader, sizeof(BmpInfoHeader))I will do as you said now, I am abit uncertain which bytes need to be swapped atm but I ll check it out.   ;  I guess every two byte types need to be swapped (ints, uints etc).   ;  also you can't cast it as you said because File.write needs char*.Also if you notice I am writing in B-G-R order   