我用单文档窗口画了一个图形,彩色(黑色和黄色)的,带滚动条,想将画好的图形保存为BMP格式,可是实验了好几次都失败了,请问各位高手有什么好的办法,希望附上代码,不胜感激!!!

解决方案 »

  1.   

    就是直接把dc上的东西存盘啊
    网上有函数
    http://wenku.baidu.com/view/9875c4d049649b6648d747f5.html
      

  2.   


    void CDialogSaveAsPic::SaveAsBmp(CString filename)
    {
    //定义图形大小
    int iWidth = 678;
    int iHeight = 290;
        int iPixel  = 16;
    //图形格式参数
        LPBITMAPINFO lpbmih = new BITMAPINFO;
        lpbmih->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
        lpbmih->bmiHeader.biWidth = iWidth;
        lpbmih->bmiHeader.biHeight = iHeight;
        lpbmih->bmiHeader.biPlanes = 1;
        lpbmih->bmiHeader.biBitCount = iPixel;
        lpbmih->bmiHeader.biCompression = BI_RGB;
        lpbmih->bmiHeader.biSizeImage = 0;
        lpbmih->bmiHeader.biXPelsPerMeter = 0;
        lpbmih->bmiHeader.biYPelsPerMeter = 0;
        lpbmih->bmiHeader.biClrUsed = 0;
        lpbmih->bmiHeader.biClrImportant = 0;    //创建位图数据
        HDC hdc,hdcMem;
        HBITMAP hBitMap = NULL;
        CBitmap *pBitMap = NULL;
        CDC *pMemDC = NULL;
        BYTE *pBits;    hdc = CreateIC(TEXT("DISPLAY"),NULL,NULL,NULL);
        hdcMem = CreateCompatibleDC(hdc);
        hBitMap = CreateDIBSection(hdcMem,lpbmih,DIB_PAL_COLORS,(void **)&pBits,NULL,0);
        pBitMap = new CBitmap;
        pBitMap->Attach(hBitMap);
        pMemDC = new CDC;
        pMemDC->Attach(hdcMem);
        pMemDC->SelectObject(pBitMap);
         //
    // CRect rcClient(0,0,iWidth,iHeight);
    pMemDC->SetBkMode(TRANSPARENT);
        //添加自绘图形 
    DrawCurve(pMemDC);
        //保存到文件并创建位图结构
        BITMAPFILEHEADER bmfh;
        ZeroMemory(&bmfh,sizeof(BITMAPFILEHEADER));
        *((char *)&bmfh.bfType) = 'B';
        *(((char *)&bmfh.bfType) + 1) = 'M';
        bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
        bmfh.bfSize = bmfh.bfOffBits + (iWidth * iHeight) * iPixel / 8;    TCHAR szBMPFileName[128];
        int iBMPBytes = iWidth * iHeight * iPixel / 8;
        strcpy(szBMPFileName,filename);
        CFile file;
        if(file.Open(szBMPFileName,CFile::modeWrite | CFile::modeCreate))
        {
            file.Write(&bmfh,sizeof(BITMAPFILEHEADER));
            file.Write(&(lpbmih->bmiHeader),sizeof(BITMAPINFOHEADER));
            file.Write(pBits,iBMPBytes);
            file.Close();
        }    pMemDC->DeleteDC();
        delete pMemDC; pMemDC  = NULL;
        delete pBitMap; pBitMap = NULL;
        delete lpbmih;  lpbmih  = NULL;
    }void CDialogSaveAsPic::OnButtonSaveAsPic() 
    {
    // TODO: Add your control notification handler code here
    CFileDialog dlg(false,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
    "位图文件(*.bmp)|*.bmp|",NULL);
    if (dlg.DoModal()!= IDOK) return;
    CString filename = dlg.GetFileName() + ".bmp";
    SaveAsBmp(filename);
    }这是我以前写的代码,希望能帮到你
      

  3.   


    谢谢你,不过你给的是读取图片后显示再保存,这个我会的,可是我的情况是在文档窗口画出图形在保存,BMP的信息如何设置?
      

  4.   


    我的情况是在文档窗口画出图形之后再保存,保存为BMP文件之前要设置BMP的文图信息头,但是这时如何获得BMP的信息?
      

  5.   


    你说的意思大概我懂,但是如何获得DC中有关BMP的参数我没找到方法,双缓存也试过了。
      

  6.   

    #include "WINDOWSX.H" CClientDC SHDC(this);//取得客户区内存DC
     CPaintDC   dc(this);
     CDC memDC;
     CRect rect;
     GetClientRect(rect); memDC.CreateCompatibleDC(&SHDC);
     CBitmap bm;
     int Width = rect.Width();//
     int Height = rect.Height();//
     bm.CreateCompatibleBitmap(&SHDC, Width, Height);
     CBitmap*  pOld = memDC.SelectObject(&bm);
     memDC.BitBlt(0, 0, Width, Height, &SHDC, 0, 0, SRCCOPY);
     memDC.SelectObject(pOld);
     BITMAP  btm;
     bm.GetBitmap(&btm);
     DWORD  size = btm.bmWidthBytes * btm.bmHeight;
     LPSTR lpData =(LPSTR)GlobalAllocPtr(GPTR, size);
     BITMAPFILEHEADER   bfh;
    /////////////////////////////////////////////
     BITMAPINFOHEADER  bih;
     bih.biBitCount = btm.bmBitsPixel;
     bih.biClrImportant = 0;
     bih.biClrUsed = 0;
     bih.biCompression = 0;
     bih.biHeight = btm.bmHeight;
     bih.biPlanes = 1;
     bih.biSize = sizeof(BITMAPINFOHEADER);
     bih.biSizeImage = size;
     bih.biWidth = btm.bmWidth;
     bih.biXPelsPerMeter = 0;
     bih.biYPelsPerMeter = 0;
     GetDIBits(dc,bm,0,bih.biHeight,lpData,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
     bfh.bfReserved1 = bfh.bfReserved2 = 0;
     bfh.bfType = ((WORD)('M'<< 8)|'B');
     bfh.bfSize = 54 + size;
     bfh.bfOffBits = 54; CFileDialog dlg(false,_T("BMP"),_T("*.bmp"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,_T("*.bmp|*.bmp|*.*|*.*|"));
     if (dlg.DoModal()==IDOK)
     {
      CFile  bf;
      CString ss=dlg.GetPathName();
      if(bf.Open(ss, CFile::modeCreate | CFile::modeWrite))
      {
       bf.WriteHuge(&bfh, sizeof(BITMAPFILEHEADER));
       bf.WriteHuge(&bih, sizeof(BITMAPINFOHEADER));
       bf.WriteHuge(lpData, size);
       bf.Close();
      }
      GlobalFreePtr(lpData);
     }
      

  7.   


    //定义图形大小
    int iWidth = 678;
    int iHeight = 290;
    int iPixel  = 16;这个是自己定义的,而非获取得到的