在ONDRAW函数中用SETPIXEL函数画了一幅每行128个点*96行的图象,现在我想把它存储为BMP文件不知道怎么实现?!

解决方案 »

  1.   

    CString fileName = "new.bmp"
    CDC* pDC = GetDC(); 
    CDC  dc;
    RECT rect;
    CBitmap bmp;

    dc.CreateCompatibleDC(pDC);
    GetClientRect(&rect);
    bmp.CreateCompatibleBitmap(pDC, rect.right, rect.bottom);
    CBitmap* pOldBmp = dc.SelectObject(&bmp);
    dc.BitBlt(0, 0, rect.right, rect.bottom, pDC,0,0,SRCCOPY);
    dc.SelectObject(pOldBmp);

    BITMAP bitmap;
    bmp.GetBitmap(&bitmap);
    DWORD size = bitmap.bmWidthBytes*bitmap.bmHeight;
    LPSTR lpData = (LPSTR)malloc(size);

    BITMAPINFOHEADER bih;
    bih.biBitCount = bitmap.bmBitsPixel;
    bih.biClrImportant = 0;
    bih.biClrUsed = 0;
    bih.biCompression = 0;
    bih.biHeight = bitmap.bmHeight;
    bih.biPlanes = 1;
    bih.biSize = sizeof(BITMAPINFOHEADER);
    bih.biSizeImage = size;
    bih.biWidth = bitmap.bmWidth;
    bih.biXPelsPerMeter = 0;
    bih.biYPelsPerMeter = 0;
    GetDIBits(*pDC,bmp,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);

    BITMAPFILEHEADER bfh;
    bfh.bfReserved1 = bfh.bfReserved2 = 0;
    bfh.bfType = ((WORD)('M'<< 8)|'B');
    bfh.bfSize = 54+size;
    bfh.bfOffBits = 54;
    CFile file;
    if(file.Open(fileName,CFile::modeCreate|CFile::modeWrite))
    {
    file.WriteHuge(&bfh,sizeof(BITMAPFILEHEADER));
    file.WriteHuge(&bih,sizeof(BITMAPINFOHEADER));
    file.WriteHuge(lpData,size);
    file.Close();
    }
    free(lpData);
      

  2.   

    谢谢 lpq1981() ,但是你的是把整个客户区都画进去了,我只是要画有图象的块,比如(100,100),(500,600)为顶点的矩形区域。能不能给点注释,好让我修改一下!
      

  3.   

    我改了一下好象并没有用,minotaurus(弥诺陶络斯) (能否讲的详细点!