将屏幕保存为图片,使用vs2008编译通过。
-
#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);
-
-
RECT rect;
-
::GetClientRect(hwnd, &rect);
-
HDC hDCMem = ::CreateCompatibleDC(hDC);
-
-
HBITMAP hBitMap = ::CreateCompatibleBitmap(hDC, rect.right, rect.bottom);
-
HBITMAP hOldMap = (HBITMAP)::SelectObject(hDCMem, hBitMap);
-
-
::BitBlt(hDCMem, 0, 0, rect.right, rect.bottom, hDC, 0, 0, SRCCOPY);
-
-
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。
第一个:
-
void CMainFrame::OnGetMap()
-
{
-
-
HWND hwnd = this->GetSafeHwnd();
-
HDC hDC = ::GetWindowDC(hwnd);
-
-
-
RECT rect;
-
::GetWindowRect(hwnd, &rect);
-
HDC hDCMem = ::CreateCompatibleDC(hDC);
-
-
HBITMAP hBitMap = ::CreateCompatibleBitmap(hDC, rect.right-rect.left, rect.bottom-rect.top);
-
HBITMAP hOldMap = (HBITMAP)::SelectObject(hDCMem, hBitMap);
-
-
::BitBlt(hDCMem, 0, 0, rect.right-rect.left, rect.bottom-rect.top, hDC, 0, 0, SRCCOPY);
-
-
CImage image;
-
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();
-
-
}