我现在想把CView中显示的内容截取成一个位图,应该怎么做啊?用pDC->getcurrentbitmap截取下来的是什么啊?麻烦哪位给我解答一下,不胜感激~~~~~

解决方案 »

  1.   

       参考一下下面这段将视图内容导出为bmp图像的代码:void CGraphSoftView::OnFileExport() 
    {
    // TODO: Add your command handler code here
    CClientDC dc(this);
    CDC  memDC;
    CRect rect;
    GetClientRect(rect);

    memDC.CreateCompatibleDC(&dc);
    CBitmap bm;
    int Width = rect.Width();
    int Height = rect.Height();
    bm.CreateCompatibleBitmap(&dc, Width, Height);
    CBitmap*  pOld = memDC.SelectObject(&bm);
    memDC.BitBlt(0, 0, Width, Height, &dc, 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_OVERWRITEPROMPT,_T("*.bmp|*.bmp|*.lld|*.lld|*.*|*.*|"));
    if (dlg.DoModal()==IDOK)
    {
    if(dlg.GetFileExt()=="bmp"){
    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();
    CString str;
    str.LoadString(IDS_STRING_SAVE_SUCCESS);
    AfxMessageBox(str);
    }
    }else if(dlg.GetFileExt()=="lld"){
    FILE *OutStream;
    OutStream = fopen(dlg.GetPathName(), "w" );
    GetDocument()->ExPort(OutStream);

    fclose(OutStream);
    }
    }
    GlobalFreePtr(lpData);
    }
      

  2.   

         参考一下下面这段将视图内容导出为bmp图像的代码:
    void CGraphSoftView::OnFileExport() 
    {
    // TODO: Add your command handler code here
    CClientDC dc(this);
    CDC  memDC;
    CRect rect;
    GetClientRect(rect);

    memDC.CreateCompatibleDC(&dc);
    CBitmap bm;
    int Width = rect.Width();
    int Height = rect.Height();
    bm.CreateCompatibleBitmap(&dc, Width, Height);
    CBitmap*  pOld = memDC.SelectObject(&bm);
    memDC.BitBlt(0, 0, Width, Height, &dc, 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_OVERWRITEPROMPT,_T("*.bmp|*.bmp|*.lld|*.lld|*.*|*.*|"));
    if (dlg.DoModal()==IDOK)
    {
    if(dlg.GetFileExt()=="bmp"){
    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();
    CString str;
    str.LoadString(IDS_STRING_SAVE_SUCCESS);
    AfxMessageBox(str);
    }
    }else if(dlg.GetFileExt()=="lld"){
    FILE *OutStream;
    OutStream = fopen(dlg.GetPathName(), "w" );
    GetDocument()->ExPort(OutStream);

    fclose(OutStream);
    }
    }
    GlobalFreePtr(lpData);
    }