程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 保存屏幕BMP

保存屏幕BMP

編輯:C++入門知識

[cpp] 
HBITMAP CopyScreenToBitmap(LPRECT lpRect) 

     //屏幕和內存設備描述表 
     HDC hSrcDC, hMemDC; 
     //位圖句柄 
     HBITMAP hBitmap, hOldBitmap; 
     //選定區域坐標 
     int nX, nY, nX2, nY2; 
     //位圖寬度和高度 
     int nWidth, nHeight; 
     //屏幕分辨率 
     int xScrn, yScrn; 
  
     //確保選定區域不為空矩形 
     if(IsRectEmpty(lpRect)) 
      return NULL; 
     //為屏幕創建設備描述表 
     hSrcDC = CreateDC("DISPLAY", NULL, NULL, NULL); 
     //為屏幕設備描述表創建兼容的內存設備描述表 
     hMemDC = CreateCompatibleDC(hSrcDC); 
     //獲得選定區域坐標 
     nX = lpRect->left; 
     nY = lpRect->top; 
     nX2 = lpRect->right; 
     nY2 = lpRect->bottom; 
     //獲得屏幕分辨率 
     xScrn = GetDeviceCaps(hSrcDC, HORZRES); 
     yScrn = GetDeviceCaps(hSrcDC, 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(hSrcDC, nWidth, nHeight); 
     //把新位圖選到內存設備描述表中 
     hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap); 
     //把屏幕設備描述表拷貝到內存設備描述表中 
     BitBlt(hMemDC, 0, 0, nWidth, nHeight, hSrcDC, nX, nY, SRCCOPY); 
     //得到屏幕位圖的句柄 
     hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap); 
     //清除 
     DeleteDC(hSrcDC); 
     DeleteDC(hMemDC); 
     //返回位置句柄 
     return hBitmap; 

HBITMAP CopyScreenToBitmap(int x1, int x2, int y1, int y2) 

     //屏幕和內存設備描述表 
     HDC hSrcDC, hMemDC; 
     //位圖句柄 
     HBITMAP hBitmap, hOldBitmap; 
     //選定區域坐標 
     int nX, nY, nX2, nY2; 
     //位圖寬度和高度 
     int nWidth, nHeight; 
     //屏幕分辨率 
     int xScrn, yScrn; 
     /*
     //確保選定區域不為空矩形
     if(IsRectEmpty(lpRect))
     return NULL;*/ 
     //為屏幕創建設備描述表 
     hSrcDC = CreateDC("DISPLAY", NULL, NULL, NULL); 
     //為屏幕設備描述表創建兼容的內存設備描述表 
     hMemDC = CreateCompatibleDC(hSrcDC); 
     //獲得選定區域坐標 
     nX = x1; 
     nY = y1; 
     nX2 = x2; 
     nY2 = y2; 
     //獲得屏幕分辨率 
     xScrn = GetDeviceCaps(hSrcDC, HORZRES); 
     yScrn = GetDeviceCaps(hSrcDC, 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(hSrcDC, nWidth, nHeight); 
     //把新位圖選到內存設備描述表中 
     hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap); 
     //把屏幕設備描述表拷貝到內存設備描述表中 
     BitBlt(hMemDC, 0, 0, nWidth, nHeight, hSrcDC, nX, nY, SRCCOPY); 
     //得到屏幕位圖的句柄 
     hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap); 
     //清除 
     DeleteDC(hSrcDC); 
     DeleteDC(hMemDC); 
     //返回位置句柄 
     return hBitmap; 

BOOL SaveCapturedBitmap(HBITMAP hBitmap, CString FileName) 

     if(hBitmap==NULL || FileName.IsEmpty()) 
     { 
      AfxMessageBox("參數錯誤"); 
      return false; 
     } 
  
     HDC hDC; 
     //當前分辨率下每象素所占字節數 
     int iBits; 
     //位圖中每象素所占字節數 
     WORD wBitCount; 
     //定義調色板大小, 位圖中像素字節大小 ,位圖文件大小 , 寫入文件字節數 
     DWORD dwPaletteSize=0, dwBmBitsSize=0, dwDIBSize=0, dwWritten=0; 
     //位圖屬性結構 
     BITMAP Bitmap;  
     //位圖文件頭結構 
     BITMAPFILEHEADER bmfHdr;  
     //位圖信息頭結構 
     BITMAPINFOHEADER bi;  
     //指向位圖信息頭結構  
     LPBITMAPINFOHEADER lpbi;  
     //定義文件,分配內存句柄,調色板句柄 
     HANDLE fh, hDib, hPal,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      wBitCount = 24; 
  
     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.biClrImportant = 0; 
     bi.biClrUsed = 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, (HPALETTE)hOldPal, TRUE); 
      RealizePalette(hDC);  
      ::ReleaseDC(NULL, hDC); 
     } 
  
     //創建位圖文件  
     fh = CreateFile(FileName, 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, dwDIBSize, &dwWritten, NULL); 
     //清除  
     GlobalUnlock(hDib); 
     GlobalFree(hDib); 
     CloseHandle(fh); 
  
     return TRUE; 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved