whats wrong in this code
i want to create sub dib 24 bit from a large dib 24 bit.
bool CDIB::WriteE(CString &filename,int top ,int left,
int height,int width)
{
///FILE* fp = fopen(filename, "wb" );
FILE* fp = fopen(filename, "wb");
if( fp == NULL )
{
AfxMessageBox("cannot open file");
fclose( fp );
return false;
}
double dTotalFileSize = 14 + 40 + height*width;//dTotalPixelBytes;
// write the file header
BITMAPFILEHEADER bFile;
bFile.bfType =19778;
bFile.bfSize = (DWORD) dTotalFileSize;
bFile.bfReserved1 = 0;
bFile.bfReserved2 = 0;
bFile.bfOffBits = (DWORD)(14+40);
fwrite( (char*) &bFile , sizeof(BITMAPFILEHEADER) , 1 , fp );
AfxMessageBox("after header write");
BITMAPINFOHEADER bmInfo;
bmInfo.biSize = 40;
bmInfo.biWidth = width;
bmInfo.biHeight = height;
bmInfo.biPlanes = 1;
bmInfo.biBitCount = 24;
bmInfo.biCompression = BI_RGB;
bmInfo.biSizeImage = width*height;
bmInfo.biClrUsed =0;
bmInfo.biClrImportant =0;
bmInfo.biXPelsPerMeter =0;
bmInfo.biYPelsPerMeter =0;
fwrite( (char*) &bmInfo , sizeof(BITMAPINFOHEADER) , 1 , fp );
//AfxMessageBox("after info write");
/*/RGBQUAD palPalEntry[256];
for ( int i = 0; i < 256; i++) {
palPalEntry[i].rgbBlue = (BYTE)i;
palPalEntry[i].rgbGreen = (BYTE)i;
palPalEntry[i].rgbRed = (BYTE)i;
palPalEntry[i].rgbReserved = (BYTE)0;
}
fseek(fp, 54L, SEEK_SET);
fwrite(palPalEntry, 256 * sizeof(RGBQUAD),1, fp);
AfxMessageBox("after pallete write");*/
unsigned char col[3];
unsigned char *ptr;
COLORREF colornew;
int w,h;
int i,j;
CString str;
str.Format("top=%d left=%d width=%d height=%d",top,left,width,height);
AfxMessageBox(str);
unsigned char *sPtr,*dPtr=NULL;
//sPtr=GetLinePtr(left)+3*top;
for( i=0;i < height; i++,left++)
{
sPtr=GetLinePtr(left)+top;//getline ptr is a pointer to the //one row of bytes
//sPtr = m_pBits + left*bytes+3*top;
for(j=0 ; j < width; j++,sPtr++)
{
//sPtr = m_pBits + left*bytes+3*top;
//c = (*sPtr + (*(sPtr+1)) + (*(sPtr+2)))/3; //(r+g+b)/3
//AfxMessageBox(" before write ");
fwrite( (char*)&sPtr, sizeof(unsigned char), 1, fp );
}
}
AfxMessageBox("at end");
fclose(fp);
return true;
}

