内存画图,该怎么解决

内存画图
Win32 能否直接在内存上画图,不显示在界面上,然后直接得到图片的buffer或者写到文件中?



HDC hDC = GetDC(m_handle);
HDC hNewDC = CreateCompatibleDC(hDC);

Gdiplus::Graphics graphics(hDC);
Gdiplus::Pen newPen(Gdiplus::Color( 255, 0, 0 ), 3); 
graphics.DrawLine(&newPen, 20, 10, 200, 100); 
graphics.DrawRectangle(&newPen, 10, 30, 200, 400);



网上搜索了从HDC写bmp文件的方法,但是得不到图片数据!!

BOOL WriteBmp(const TSTRING &strFile,const std::vector<BYTE> &vtData,const SIZE &sizeImg)   
{     
  
    BITMAPINFOHEADER bmInfoHeader = {0};  
    bmInfoHeader.biSize = sizeof(BITMAPINFOHEADER);  
    bmInfoHeader.biWidth = sizeImg.cx;  
    bmInfoHeader.biHeight = sizeImg.cy;  
    bmInfoHeader.biPlanes = 1;  
    bmInfoHeader.biBitCount = 24;  
  
    //Bimap file header in order to write bmp file  
    BITMAPFILEHEADER bmFileHeader = {0};  
    bmFileHeader.bfType = 0x4d42;  //bmp    
    bmFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);  
    bmFileHeader.bfSize = bmFileHeader.bfOffBits + ((bmInfoHeader.biWidth * bmInfoHeader.biHeight) * 3); ///3=(24 / 8)  
  
    HANDLE hFile = CreateFile(strFile.c_str(),GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);  
    if(hFile == INVALID_HANDLE_VALUE)  
    {  
        return FALSE;  
    }  
  
    DWORD dwWrite = 0;  
    WriteFile(hFile,&bmFileHeader,sizeof(BITMAPFILEHEADER),&dwWrite,NULL);  
    WriteFile(hFile,&bmInfoHeader, sizeof(BITMAPINFOHEADER),&dwWrite,NULL);  
    WriteFile(hFile,&vtData[0], vtData.size(),&dwWrite,NULL);  
  
  
    CloseHandle(hFile);  
  
    return TRUE;  
}   
  
  
BOOL WriteBmp(const TSTRING &strFile,HDC hdc)  
{  
    int iWidth = GetDeviceCaps(hdc,HORZRES);  
    int iHeight = GetDeviceCaps(hdc,VERTRES);  
    RECT rcDC = {0,0,iWidth,iHeight};  
  
    return WriteBmp(strFile,hdc,rcDC);    
}  
  
BOOL WriteBmp(const TSTRING &strFile,HDC hdc,const RECT &rcDC)  
{  
    BOOL bRes = FALSE;  
    BITMAPINFO bmpInfo = {0};  
    BYTE *pData = NULL;  
    SIZE sizeImg = {0};  
    HBITMAP hBmp = NULL;  
    std::vector<BYTE> vtData;  
    HGDIOBJ hOldObj = NULL;  
    HDC hdcMem = NULL;  
  
    //Initilaize the bitmap information   
    bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);  
    bmpInfo.bmiHeader.biWidth = rcDC.right - rcDC.left;  
    bmpInfo.bmiHeader.biHeight = rcDC.bottom - rcDC.top;  
    bmpInfo.bmiHeader.biPlanes = 1;  
    bmpInfo.bmiHeader.biBitCount = 24;  
  
    //Create the compatible DC to get the data  
    hdcMem = CreateCompatibleDC(hdc);  
    if(hdcMem == NULL)  
    {  
        goto EXIT;  
    }  
  
    //Get the data from the memory DC     
    hBmp = CreateDIBSection(hdcMem,&bmpInfo,DIB_RGB_COLORS,reinterpret_cast<VOID **>(&pData),NULL,0);  
    if(hBmp == NULL)  
    {  
        goto EXIT;  
    }