bitmapmfccdccbitmap

Save a CBitmap to .bmp file


I'm working with a CBitmap object using a CDC. When I'm finished with it I want to save it to a file. I have seen something about a process with that code LINK:

CFile file;
if(file.Open(szFileName, CFile::modeCreate | CFile::modeWrite))
{ 
    file.Write(&bh, sizeof(BITMAPFILEHEADER));
    file.Write(&(bi.bmiHeader), sizeof(BITMAPINFOHEADER));
    file.Write(lpBitmapBits, 3 * nWidth * nHeight);
    file.Close();
}

My doubt is about BITMAPFILEHEADER and BITMAPINFOHEADER. How can I fill them with a given CBitmap or CDC?


Solution

  • You could try this:

    CImage image;
    
    image.Attach(CDCvar.GetCurrentBitmap());
    image.Save(_T(".\\test.bmp"), Gdiplus::ImageFormatBMP);