截屏代码

  1 void CmyTestGraph::OnButton2()
  2 {
  3     // TODO: Add your control notification handler code here
  4     CWnd *pWnd = GetDlgItem(IDC_RICHEDIT1);
  5     RECT  myrect;
  6     pWnd->GetClientRect(&myrect);
  7     CDC *pDC;
  8     pDC = pWnd->GetDC();
  9 
 10     m_myview->SaveBMPFromDC("D:\\graph1.bmp", *pDC, myrect);
 11     pWnd->ReleaseDC(pDC);
 12 
 13 }
 14 
 15 
 16 void CTrView::SaveBMPFromWnd(CString filename, CWnd *pWnd)
 17 {
 18     if(pWnd ==NULL) return;
 19     if(!IsWindow(pWnd->m_hWnd)) return;
 20     RECT  myrect;
 21     pWnd->GetClientRect(&myrect);
 22     CDC *pDC;
 23     pDC = pWnd->GetDC();
 24 
 25     m_myview->SaveBMPFromDC(filename, *pDC, myrect);
 26     pWnd->ReleaseDC(pDC);
 27 }
 28 
 29 void CTrView::SaveBMPFromDC(CString filename, CDC &dc, RECT &myrect)
 30 {
 31 
 32 
 33     RECT TrViewRect;
 34     memcpy(&TrViewRect, &myrect, sizeof(RECT));
 35 
 36     int nWidth = TrViewRect.right - TrViewRect.left;
 37     int nHeight = TrViewRect.bottom - TrViewRect.top;
 38     int nX = TrViewRect.left;
 39     int nY = TrViewRect.top;
 40 
 41 
 42     HDC  hScrDC, hMemDC;
 43     hScrDC = dc.GetSafeHdc();
 44     hMemDC = CreateCompatibleDC(hScrDC);
 45     TRACE("view:SaveBMPFromDC:w=%d h=%d \n", nWidth, nHeight);
 46 
 47     HBITMAP    hBitmap, hOldBitmap;
 48     hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
 49     hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
 50 
 51     BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
 52 
 53     hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap);
 54 
 55     DeleteDC(hScrDC);
 56     DeleteDC(hMemDC);
 57 
 58     WriteBmpToFile(hBitmap, filename);
 59 
 60 
 61 }
 62 
 63 void CTrView::WriteBmpToFile(HBITMAP hBitmap, const char *pathname)
 64 {
 65     HDC hDC =::CreateDC("DISPLAY", NULL, NULL, NULL);
 66     int iBits = ::GetDeviceCaps(hDC, BITSPIXEL) * ::GetDeviceCaps(hDC, PLANES);//当前分辨率下每个像素所占字节数
 67     ::DeleteDC(hDC);
 68     WORD   wBitCount;   //位图中每个像素所占字节数
 69     if (iBits <= 1)
 70         wBitCount = 1;
 71     else if (iBits <= 4)
 72         wBitCount = 4;
 73     else if (iBits <= 8)
 74         wBitCount = 8;
 75     else if (iBits <= 24)
 76         wBitCount = 24;
 77     else
 78         wBitCount = iBits;
 79 
 80     DWORD   dwPaletteSize = 0;    //调色板大小, 位图中像素字节大小
 81     if (wBitCount <= 8)
 82         dwPaletteSize = (1 << wBitCount) *    sizeof(RGBQUAD);
 83 
 84 
 85     BITMAP  bm;        //位图属性结构
 86     ::GetObject(hBitmap, sizeof(bm), (LPSTR)&bm);
 87 
 88 
 89     BITMAPINFOHEADER   bi;       //位图信息头结构
 90     bi.biSize            = sizeof(BITMAPINFOHEADER);
 91     bi.biWidth           = bm.bmWidth;
 92     bi.biHeight          = bm.bmHeight;
 93     bi.biPlanes          = 1;
 94     bi.biBitCount        = wBitCount;
 95     bi.biCompression     = BI_RGB; //BI_RGB表示位图没有压缩
 96     bi.biSizeImage       = 0;
 97     bi.biXPelsPerMeter   = 0;
 98     bi.biYPelsPerMeter   = 0;
 99     bi.biClrUsed         = 0;
100     bi.biClrImportant    = 0;
101 
102     DWORD dwBmBitsSize = ((bm.bmWidth * wBitCount + 31) / 32) * 4 * bm.bmHeight;
103     HANDLE hDib  = ::GlobalAlloc(GHND, dwBmBitsSize + dwPaletteSize + sizeof(BITMAPINFOHEADER)); //为位图内容分配内存
104     LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
105     *lpbi = bi;
106 
107     HANDLE hPal = ::GetStockObject(DEFAULT_PALETTE);  // 处理调色板
108     HANDLE  hOldPal = NULL;
109     if (hPal)
110     {
111         hDC = ::GetDC(NULL);
112         hOldPal = SelectPalette(hDC, (HPALETTE)hPal, FALSE);
113         RealizePalette(hDC);
114     }
115     ::GetDIBits(hDC, hBitmap, 0, (UINT) bm.bmHeight, (LPSTR)lpbi + sizeof(BITMAPINFOHEADER) + dwPaletteSize, (BITMAPINFO *)lpbi, DIB_RGB_COLORS); // 获取该调色板下新的像素值
116     if (hOldPal)//恢复调色板
117     {
118         SelectPalette(hDC, (HPALETTE)hOldPal, TRUE);
119         RealizePalette(hDC);
120         ::ReleaseDC(NULL, hDC);
121     }
122 
123     BITMAPFILEHEADER   bmfHdr; //位图文件头结构
124     bmfHdr.bfType = 0x4D42;  // "BM"      // 设置位图文件头
125     DWORD dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize;
126     bmfHdr.bfSize = dwDIBSize;
127     bmfHdr.bfReserved1 = 0;
128     bmfHdr.bfReserved2 = 0;
129     bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize;
130 
131     HANDLE hFile = CreateFile(pathname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL); //创建位图文件
132     DWORD dwWritten;
133     WriteFile(hFile, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);    // 写入位图文件头
134     WriteFile(hFile, (LPSTR)lpbi, dwDIBSize, &dwWritten, NULL);// 写入位图文件其余内容
135 
136     GlobalUnlock(hDib);   //清除
137     GlobalFree(hDib);
138     CloseHandle(hFile);
139 
140 }

 

posted @ 2015-03-24 14:41  boyang987  阅读(297)  评论(0编辑  收藏  举报