help for saving the bmp file 
Author Message
 help for saving the bmp file

Hi Everybody

If anybody can help me in dectecting the problem.
I am saving the window image into BMP format on the disk.
Code I am using is like this

I am not able to save the file. File saved is of only 1KB in size.

DWORD CDib::Save(CDC* memdc,CFile& file,HDIB hDib)
{

BITMAPFILEHEADER hdr;
DWORD dwDIBSize;

DIBSECTION ds;
  VERIFY(GetObject(sizeof(ds), &ds)==sizeof(ds));
char buf[sizeof(BITMAPINFOHEADER) + MAXPALCOLORS*sizeof(RGBQUAD)];
BITMAPINFOHEADER& bmih = *(BITMAPINFOHEADER*)buf;
RGBQUAD* colors = (RGBQUAD*)(&bmih+1);

memcpy(&bmih, &ds.dsBmih, sizeof(bmih));

if(hDib == NULL)
  return 0;

if (&bmih == NULL)
  return 0;

//fill the fields of file header
// fill the file type( first two will be "BM")

hdr.bfType = DIB_HEADER_MARKER;

//Calculating the size of DIB

// First find the size of header plus size of color table.
//the first DWORD in BITMAPINFOHEADER and BITMAPCOREHEADER contains
//the size of the structure

//Now Calculate the size of the Palette

if((bmih.biCompression = BI_RLE8) || (bmih.biCompression  == BI_RLE4))
{

  dwDIBSize +=bmih.biSizeImage;

Quote:
}

else
{
  DWORD dwBmBitsSize;   //Size of the Bitmap Bits only

  //It's not RLE,so size is width (DWORD aligned) * Height

  dwBmBitsSize = WIDTHBYTES((bmih.biWidth ) * ((DWORD)bmih.biBitCount)) *
bmih.biHeight;
  dwDIBSize +=dwBmBitsSize;

  bmih.biSizeImage = dwBmBitsSize;

Quote:
}

//Calculating the file size by adding the DIB size to
sizeof(BITMAPFILEHEADER)
hdr.bfSize = dwDIBSize + sizeof(BITMAPFILEHEADER);
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;

//now calculate the offset

hdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + bmih.biSize +
PaletteSize();

//Write the file header

file.Write ((LPSTR)&hdr,sizeof(BITMAPFILEHEADER));
DWORD dwBytesSaved = sizeof(BITMAPFILEHEADER);

// Write the DIB header

UINT nCount = sizeof(BITMAPINFO) +
(GetColorTable(colors,MAXPALCOLORS)*sizeof(RGBQUAD));

nCount = sizeof(BITMAPINFO) +(NumColors()-1)*sizeof(RGBQUAD);
dwBytesSaved += nCount;

file.Write (&bmih,nCount);

// Write the DIB bits

DWORD dwBytes = bmih.biBitCount * bmih.biWidth; //Width();
// Calculate the number of bytes per line

if (dwBytes%32 == 0)
  dwBytes /= 8;
else

dwBytes = dwBytes/8 + (32-dwBytes%32)/8 + (((32-dwBytes%32)%8 > 0) ? 1 :
0);
nCount = dwBytes * bmih.biHeight; //Height();
dwBytesSaved += nCount;

//file.WriteHuge (m_bm.bmBits ,nCount);

return dwBytesSaved;

Quote:
}

////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

UINT CDib::GetColorTable(RGBQUAD* colorTab, UINT nColors)
{
CWindowDC dcScreen(NULL);
CDC memdc;
memdc.CreateCompatibleDC(&dcScreen);
CBitmap* pOldBm = memdc.SelectObject(this);
nColors = GetDIBColorTable(memdc, 0, nColors, colorTab);
memdc.SelectObject(pOldBm);
return nColors;

Quote:
}

////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////

WORD CDib::PaletteSize()
{

RGBQUAD* colors = new RGBQUAD[MAXPALCOLORS];

       // MAXPALCOLORS defined as #define MAXPALCOLORS 256

return GetColorTable(colors,MAXPALCOLORS) * sizeof(RGBQUAD);

Quote:
}

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
/


Sun, 17 Feb 2002 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Help: Saving Screens drawn with OnDraw() as .bmp file (or other format)

2. URGENT : How to save bitmaps into bmp files.

3. Saving CBitmap to a bmp file

4. Saving a whole covered window to BMP file

5. Unable to Save window image to BMP file on disk

6. Saving BMP Files to Disk

7. How can I save sth to a bmp file

8. saving bmp file

9. Saving Bmp Files

10. How to save Clipboard to bmp file.

11. simple question:how to save CBitmap to bmp file

12. how to save a view as a bmp file

 

 
Powered by phpBB® Forum Software