將螢幕儲存為圖片,使用vs2008編譯通過。
view plaincopy to clipboardprint?
- #include "stdafx.h"
- #include <windows.h>
- #include <atlimage.h>
-
- int __stdcall WinMain(
- HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nShowCmd)
- {
- HWND hwnd = ::GetDesktopWindow();
- HDC hDC = ::GetDC(hwnd);//擷取螢幕DC
-
- RECT rect;
- ::GetClientRect(hwnd, &rect);//擷取螢幕大小
- HDC hDCMem = ::CreateCompatibleDC(hDC);//建立相容DC
-
- HBITMAP hBitMap = ::CreateCompatibleBitmap(hDC, rect.right, rect.bottom);//建立相容位元影像
- HBITMAP hOldMap = (HBITMAP)::SelectObject(hDCMem, hBitMap);//將位元影像選入DC,並儲存傳回值
-
- ::BitBlt(hDCMem, 0, 0, rect.right, rect.bottom, hDC, 0, 0, SRCCOPY);//將螢幕DC的圖象複製到記憶體DC中
-
- CImage image;
- image.Attach(hBitMap);
- image.Save(_T("c:\\B.jpg"));//如果檔案尾碼為.bmp,則儲存為為bmp格式
- image.Detach();
-
- ::SelectObject(hDCMem, hOldMap);//選入上次的傳回值
-
- //釋放
- ::DeleteObject(hBitMap);
- ::DeleteDC(hDCMem);
- ::DeleteDC(hDC);
-
- return 0;
- }
#include "stdafx.h" #include <windows.h> #include <atlimage.h> int __stdcall WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { HWND hwnd = ::GetDesktopWindow(); HDC hDC = ::GetDC(hwnd);//擷取螢幕DC RECT rect; ::GetClientRect(hwnd, &rect);//擷取螢幕大小 HDC hDCMem = ::CreateCompatibleDC(hDC);//建立相容DC HBITMAP hBitMap = ::CreateCompatibleBitmap(hDC, rect.right, rect.bottom);//建立相容位元影像 HBITMAP hOldMap = (HBITMAP)::SelectObject(hDCMem, hBitMap);//將位元影像選入DC,並儲存傳回值 ::BitBlt(hDCMem, 0, 0, rect.right, rect.bottom, hDC, 0, 0, SRCCOPY);//將螢幕DC的圖象複製到記憶體DC中 CImage image; image.Attach(hBitMap); image.Save(_T("c:\\B.jpg"));//如果檔案尾碼為.bmp,則儲存為為bmp格式 image.Detach(); ::SelectObject(hDCMem, hOldMap);//選入上次的傳回值 //釋放 ::DeleteObject(hBitMap); ::DeleteDC(hDCMem); ::DeleteDC(hDC); return 0; }
將當前MFC程式(這裡是單文檔程式)儲存為圖片,使用vs2005。第一個使用CImage類儲存圖片,第二個使用BITMAPINFO。
將螢幕儲存為圖片 將當前MFC程式儲存為圖片 c++ vc
收藏
將螢幕儲存為圖片,使用vs2008編譯通過。
view plaincopy to clipboardprint?
- #include "stdafx.h"
- #include <windows.h>
- #include <atlimage.h>
-
- int __stdcall WinMain(
- HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nShowCmd)
- {
- HWND hwnd = ::GetDesktopWindow();
- HDC hDC = ::GetDC(hwnd);//擷取螢幕DC
-
- RECT rect;
- ::GetClientRect(hwnd, &rect);//擷取螢幕大小
- HDC hDCMem = ::CreateCompatibleDC(hDC);//建立相容DC
-
- HBITMAP hBitMap = ::CreateCompatibleBitmap(hDC, rect.right, rect.bottom);//建立相容位元影像
- HBITMAP hOldMap = (HBITMAP)::SelectObject(hDCMem, hBitMap);//將位元影像選入DC,並儲存傳回值
-
- ::BitBlt(hDCMem, 0, 0, rect.right, rect.bottom, hDC, 0, 0, SRCCOPY);//將螢幕DC的圖象複製到記憶體DC中
-
- CImage image;
- image.Attach(hBitMap);
- image.Save(_T("c:\\B.jpg"));//如果檔案尾碼為.bmp,則儲存為為bmp格式
- image.Detach();
-
- ::SelectObject(hDCMem, hOldMap);//選入上次的傳回值
-
- //釋放
- ::DeleteObject(hBitMap);
- ::DeleteDC(hDCMem);
- ::DeleteDC(hDC);
-
- return 0;
- }
#include "stdafx.h" #include <windows.h> #include <atlimage.h> int __stdcall WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { HWND hwnd = ::GetDesktopWindow(); HDC hDC = ::GetDC(hwnd);//擷取螢幕DC RECT rect; ::GetClientRect(hwnd, &rect);//擷取螢幕大小 HDC hDCMem = ::CreateCompatibleDC(hDC);//建立相容DC HBITMAP hBitMap = ::CreateCompatibleBitmap(hDC, rect.right, rect.bottom);//建立相容位元影像 HBITMAP hOldMap = (HBITMAP)::SelectObject(hDCMem, hBitMap);//將位元影像選入DC,並儲存傳回值 ::BitBlt(hDCMem, 0, 0, rect.right, rect.bottom, hDC, 0, 0, SRCCOPY);//將螢幕DC的圖象複製到記憶體DC中 CImage image; image.Attach(hBitMap); image.Save(_T("c:\\B.jpg"));//如果檔案尾碼為.bmp,則儲存為為bmp格式 image.Detach(); ::SelectObject(hDCMem, hOldMap);//選入上次的傳回值 //釋放 ::DeleteObject(hBitMap); ::DeleteDC(hDCMem); ::DeleteDC(hDC); return 0; }
將當前MFC程式(這裡是單文檔程式)儲存為圖片,使用vs2005。第一個使用CImage類儲存圖片,第二個使用BITMAPINFO。
第一個:
view plaincopy to clipboardprint?
- void CMainFrame::OnGetMap()
- {
-
- HWND hwnd = this->GetSafeHwnd();
- HDC hDC = ::GetWindowDC(hwnd);//擷取DC
-
-
- RECT rect;
- ::GetWindowRect(hwnd, &rect);//擷取螢幕大小
- HDC hDCMem = ::CreateCompatibleDC(hDC);//建立相容DC
-
- HBITMAP hBitMap = ::CreateCompatibleBitmap(hDC, rect.right-rect.left, rect.bottom-rect.top);//建立相容位元影像
- HBITMAP hOldMap = (HBITMAP)::SelectObject(hDCMem, hBitMap);//將位元影像選入DC,並儲存傳回值
-
- ::BitBlt(hDCMem, 0, 0, rect.right-rect.left, rect.bottom-rect.top, hDC, 0, 0, SRCCOPY);//將螢幕DC的圖象複製到記憶體DC中
-
- CImage image;//需要#include <atlimage.h>
- image.Attach(hBitMap);
- image.Save(_T("c:\\B.jpg"));//如果檔案尾碼為.bmp,則儲存為為bmp格式
- image.Detach();
-
- ::SelectObject(hDCMem, hOldMap);//選入上次的傳回值
-
- //釋放
- ::DeleteObject(hBitMap);
- ::DeleteDC(hDCMem);
- ::DeleteDC(hDC);
- }
void CMainFrame::OnGetMap() { HWND hwnd = this->GetSafeHwnd(); HDC hDC = ::GetWindowDC(hwnd);//擷取DC RECT rect; ::GetWindowRect(hwnd, &rect);//擷取螢幕大小 HDC hDCMem = ::CreateCompatibleDC(hDC);//建立相容DC HBITMAP hBitMap = ::CreateCompatibleBitmap(hDC, rect.right-rect.left, rect.bottom-rect.top);//建立相容位元影像 HBITMAP hOldMap = (HBITMAP)::SelectObject(hDCMem, hBitMap);//將位元影像選入DC,並儲存傳回值 ::BitBlt(hDCMem, 0, 0, rect.right-rect.left, rect.bottom-rect.top, hDC, 0, 0, SRCCOPY);//將螢幕DC的圖象複製到記憶體DC中 CImage image;//需要#include <atlimage.h> image.Attach(hBitMap); image.Save(_T("c:\\B.jpg"));//如果檔案尾碼為.bmp,則儲存為為bmp格式 image.Detach(); ::SelectObject(hDCMem, hOldMap);//選入上次的傳回值 //釋放 ::DeleteObject(hBitMap); ::DeleteDC(hDCMem); ::DeleteDC(hDC); }
第二個:
- void CMainFrame::OnGetMap()
- {
-
- CDC* pDC = GetWindowDC();
- CBitmap bitmap;
- CDC memDC ;
- CRect rect;
- GetWindowRect(rect);
- memDC.CreateCompatibleDC(pDC);
-
- bitmap.CreateCompatibleBitmap(pDC,rect.Width(),rect.Height());
- memDC.SelectObject(&bitmap);
- memDC.BitBlt(0,0,rect.Width(),rect.Height(),pDC,0,0,SRCCOPY);
-
- CFileDialog fDlg(FALSE,_T("bmp"),NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,_T("位元影像檔案|*.bmp"),this);
- if (fDlg.DoModal()==IDOK)
- {
-
- CString bmpfile = fDlg.GetPathName();
-
- CFile file(bmpfile,CFile::modeCreate|CFile::modeWrite);
-
- BITMAP bInfo;
- bitmap.GetBitmap(&bInfo);
-
- //計算調色盤大小
- int panelsize = 0;
- if (bInfo.bmBitsPixel<24) //非真彩色
- {
- panelsize = pow((double)2,bInfo.bmBitsPixel)*sizeof(RGBQUAD);
- }
-
- //定義位元影像資訊
- BITMAPINFO* bMapInfo = (BITMAPINFO*)LocalAlloc(LPTR,sizeof(BITMAPINFO)+panelsize);
- bMapInfo->bmiHeader.biBitCount = bInfo.bmBitsPixel;
- bMapInfo->bmiHeader.biClrImportant = 0;
- bMapInfo->bmiHeader.biCompression = 0;
- bMapInfo->bmiHeader.biHeight = bInfo.bmHeight;
- bMapInfo->bmiHeader.biPlanes = bInfo.bmPlanes;
- bMapInfo->bmiHeader.biSize = sizeof(BITMAPINFO);
- bMapInfo->bmiHeader.biSizeImage = bInfo.bmHeight*bInfo.bmWidthBytes;
- bMapInfo->bmiHeader.biWidth = bInfo.bmWidth;
- bMapInfo->bmiHeader.biXPelsPerMeter = 0;
- bMapInfo->bmiHeader.biYPelsPerMeter = 0;
-
- //擷取位元影像的實際資料
- char* pData = new char[bMapInfo->bmiHeader.biSizeImage];
- int len = GetDIBits(pDC->m_hDC,bitmap,0,bInfo.bmHeight,pData,bMapInfo,DIB_RGB_COLORS);
-
- BITMAPFILEHEADER bFileHeader;
- bFileHeader.bfType = 0x4D42;
- bFileHeader.bfReserved1 = 0;
- bFileHeader.bfReserved2 = 0;
- bFileHeader.bfSize = sizeof(BITMAPFILEHEADER);
- bFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+panelsize;
-
- //向檔案中寫入位元影像資料
- file.Write(&bFileHeader,sizeof(BITMAPFILEHEADER));
- file.Write(&bMapInfo->bmiHeader,sizeof(BITMAPINFOHEADER));
- file.Write(pData,bMapInfo->bmiHeader.biSizeImage+panelsize);
- file.Close();
- delete pData;
- LocalFree(bMapInfo);
- }
- bitmap.DeleteObject();
- memDC.DeleteDC();
-
- }