在CDC上绘图,怎么保存为bmp格式

在CDC上绘图,如何保存为bmp格式
自己写的绘图软件,可以画线之类的,可是如何保存为bmp格式呢?

------解决方案--------------------
先把你的图画在内存DC m_pmemDC 上


HDC hScrDC, hMemDC;
int width, height,startX,startY;
startX = 0;
startY = 0;
width = m_icanvesEndX;
height = m_icanvesEndY;

//the pointer will save all pixel point 's color value
BYTE *lpBitmapBits = NULL;

hScrDC = m_pmemDC-> GetSafeHdc();

hMemDC = CreateCompatibleDC(hScrDC);

//initialise the struct BITMAPINFO for the bimap infomation,
//in order to use the function CreateDIBSection
// on wince os, each pixel stored by 24 bits(biBitCount=24)
//and no compressing(biCompression=0)
BITMAPINFO RGB16BitsBITMAPINFO;
ZeroMemory(&RGB16BitsBITMAPINFO, sizeof(BITMAPINFO));
RGB16BitsBITMAPINFO.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
RGB16BitsBITMAPINFO.bmiHeader.biWidth = width;
RGB16BitsBITMAPINFO.bmiHeader.biHeight = height;
RGB16BitsBITMAPINFO.bmiHeader.biPlanes = 1;
RGB16BitsBITMAPINFO.bmiHeader.biBitCount = 16;

//use the function CreateDIBSection and SelectObject
//in order to get the bimap pointer : lpBitmapBits
HBITMAP directBmp = CreateDIBSection(hMemDC, (BITMAPINFO*)&RGB16BitsBITMAPINFO,
DIB_RGB_COLORS, (void **)&lpBitmapBits, NULL, 0);
HGDIOBJ previousObject = SelectObject(hMemDC, directBmp);

// copy the screen dc to the memory dc
BitBlt(hMemDC, 0, 0, width, height, hScrDC, startX, startY, SRCCOPY);

//if you only want to get the every pixel color value,
//you can begin here and the following part of this function will be unuseful;
//the following part is in order to write file;

//bimap file header in order to write bmp file
BITMAPFILEHEADER bmBITMAPFILEHEADER;
ZeroMemory(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER));
bmBITMAPFILEHEADER.bfType = 0x4d42; //bmp
bmBITMAPFILEHEADER.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmBITMAPFILEHEADER.bfSize = bmBITMAPFILEHEADER.bfOffBits + ((width*height)*2); ///2=(16 / 8)

//write into file
FILE *mStream = NULL;
if((mStream = fopen(m_sfilename, "wb ")))
{
//write bitmap file header
fwrite(&bmBITMAPFILEHEADER, sizeof(BITMAPFILEHEADER), 1, mStream);
//write bitmap info
fwrite(&(RGB16BitsBITMAPINFO.bmiHeader), sizeof(BITMAPINFOHEADER), 1, mStream);
//write bitmap pixels data
fwrite(lpBitmapBits, 2*width*height, 1, mStream);
//close file
fclose(mStream);
}
else
{
return FALSE;
}
return TRUE;
------解决方案--------------------
void CTestDIBView::OnFileSaveAs()
{
// TODO: Add your command handler code here

CClientDC dc(this);
CDC memDC;
CRect rect;
GetClientRect(rect);

memDC.CreateCompatibleDC(&dc);
int Width = rect.Width();
int Height = rect.Height();
int iBits; //当前显示分辨率下每个像素所占字节数
WORD wBitCount; ////位图中每个像素所占字节数
DWORD dwPaletteSize=0, dwBmBitsSize;//定义调色板大小,位图中像素字节大小

if(m_OpenState != 1)
{
m_bitmap.CreateCompatibleBitmap(&dc, Width, Height);
}
CBitmap* pOld = memDC.SelectObject(&m_bitmap);
memDC.BitBlt(0, 0, Width, Height, &dc, 0, 0, SRCCOPY);
memDC.SelectObject(pOld);
BITMAP btm;
m_bitmap.GetBitmap(&btm);
DWORD dwBytesSize = btm.bmWidthBytes * btm.bmHeight;
iBits = GetDeviceCaps(memDC, BITSPIXEL);

if (iBits <= 1)