儲存當前螢幕到位元影像檔案CopyScreenToBitmap

來源:互聯網
上載者:User

//儲存螢幕到映像//用來儲存螢幕到影像檔。void CMainView::SaveScreen() {CRect rect;GetWindowRect(&rect);char BASED_CODE szFilter[] = "bmp Files (*.bmp)|*.bmp|";CString filename="";CString ext = "";CFileDialog dlg(false, "bmp",filename, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter);HBITMAP hMap =   CopyScreenToBitmap(rect); if(dlg.DoModal() == IDOK){filename = dlg.GetPathName();SaveBitmapToFile(hMap,filename.GetBuffer(filename.GetLength()+1)); }}int CMainView::SaveBitmapToFile(HBITMAP hBitmap, LPSTR lpFileName){       //lpFileName 為位元影像檔案名稱HDC      hDC;         //裝置描述表int      iBits;      //當前顯示解析度下每個像素所佔位元組數WORD     wBitCount;   //位元影像中每個像素所佔位元組數//定義調色盤大小, 位元影像中像素位元組大小 ,   位元影像檔案大小 , 寫入檔案位元組數DWORD            dwPaletteSize=0,dwBmBitsSize,dwDIBSize, dwWritten;BITMAP           Bitmap;        //位元影像屬性結構BITMAPFILEHEADER    bmfHdr;        //位元影像檔案頭結構BITMAPINFOHEADER    bi;            //位元影像資訊頭結構 LPBITMAPINFOHEADER lpbi;          //指向位元影像資訊頭結構HANDLE           fh, hDib, hPal;HPALETTE      hOldPal=NULL;//定義檔案,分配記憶體控制代碼,調色盤控制代碼//計算位元影像檔案每個像素所佔位元組數hDC = CreateDC("DISPLAY",NULL,NULL,NULL);iBits = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);DeleteDC(hDC);if (iBits <= 1)wBitCount = 1;else if (iBits <= 4)wBitCount = 4;else if (iBits <= 8)wBitCount = 8;else if (iBits <= 24)wBitCount = 24;elsewBitCount = 32;//計算調色盤大小if (wBitCount <= 8)dwPaletteSize=(1<<wBitCount)*sizeof(RGBQUAD);//設定位元影像資訊頭結構GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);bi.biSize             = sizeof(BITMAPINFOHEADER);bi.biWidth            = Bitmap.bmWidth;bi.biHeight           = Bitmap.bmHeight;bi.biPlanes           = 1;bi.biBitCount          = wBitCount;bi.biCompression       = BI_RGB;bi.biSizeImage         = 0;bi.biXPelsPerMeter      = 0;bi.biYPelsPerMeter      = 0;bi.biClrUsed          = 0;bi.biClrImportant       = 0;dwBmBitsSize = ((Bitmap.bmWidth*wBitCount+31)/32)*4*Bitmap.bmHeight;//為位元影像內容分配記憶體hDib   = GlobalAlloc(GHND,dwBmBitsSize+dwPaletteSize+sizeof(BITMAPINFOHEADER));lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);*lpbi = bi;// 處理調色盤   hPal = GetStockObject(DEFAULT_PALETTE);if (hPal){hDC = ::GetDC(NULL);hOldPal=SelectPalette(hDC,(HPALETTE)hPal,FALSE);RealizePalette(hDC);}// 擷取該調色盤下新的像素值GetDIBits(hDC,hBitmap,0,(UINT)Bitmap.bmHeight,(LPSTR)lpbi+sizeof(BITMAPINFOHEADER)+dwPaletteSize, (BITMAPINFO *)lpbi,DIB_RGB_COLORS);//恢複調色盤   if (hOldPal){SelectPalette(hDC, hOldPal, TRUE);RealizePalette(hDC);::ReleaseDC(NULL, hDC);}//建立位元影像檔案    fh=CreateFile(lpFileName, GENERIC_WRITE,0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);if (fh==INVALID_HANDLE_VALUE)return FALSE;// 設定位元影像檔案頭bmfHdr.bfType = 0x4D42;   // "BM"dwDIBSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+dwPaletteSize+dwBmBitsSize;  bmfHdr.bfSize = dwDIBSize;bmfHdr.bfReserved1 = 0;bmfHdr.bfReserved2 = 0;bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER)+(DWORD)sizeof(BITMAPINFOHEADER)+dwPaletteSize;// 寫入位元影像檔案頭WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);// 寫入位元影像檔案其餘內容WriteFile(fh, (LPSTR)lpbi, sizeof(BITMAPINFOHEADER)+dwPaletteSize+dwBmBitsSize , &dwWritten, NULL); //清除   GlobalUnlock(hDib);GlobalFree(hDib);CloseHandle(fh);return TRUE;}HBITMAP CMainView::CopyScreenToBitmap(LPRECT lpRect){HDC hScrDC, hMemDC;      // 螢幕和記憶體裝置描述表HBITMAP hBitmap,hOldBitmap;   // 位元影像控制代碼int        nX, nY, nX2, nY2;      // 選定地區座標int        nWidth, nHeight;      // 位元影像寬度和高度int        xScrn, yScrn;         // 螢幕解析度// 確保選定地區不為空白矩形if (IsRectEmpty(lpRect))return NULL;//為螢幕建立裝置描述表hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);//為螢幕裝置描述表建立相容的記憶體裝置描述表hMemDC = CreateCompatibleDC(hScrDC);// 獲得選定地區座標nX = lpRect->left;nY = lpRect->top;nX2 = lpRect->right;nY2 = lpRect->bottom;// 獲得螢幕解析度xScrn = GetDeviceCaps(hScrDC, HORZRES);yScrn = GetDeviceCaps(hScrDC, VERTRES);//確保選定地區是可見的if (nX < 0)nX = 0;if (nY < 0)nY = 0;if (nX2 > xScrn)nX2 = xScrn;if (nY2 > yScrn)nY2 = yScrn;nWidth = nX2 - nX;nHeight = nY2 - nY;// 建立一個與螢幕裝置描述表相容的位元影像hBitmap=CreateCompatibleBitmap(hScrDC,nWidth,nHeight);// 把新位元影像選到記憶體裝置描述表中hOldBitmap=(HBITMAP)SelectObject(hMemDC,hBitmap);// 把螢幕裝置描述表拷貝到記憶體裝置描述表中BitBlt(hMemDC,0,0, nWidth,nHeight,hScrDC, nX, nY, SRCCOPY);//得到螢幕位元影像的控制代碼hBitmap=(HBITMAP)SelectObject(hMemDC,hOldBitmap);//清除 DeleteDC(hScrDC);DeleteDC(hMemDC);// 返回位元影像控制代碼return hBitmap;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.