首页 > 代码库 > 将屏幕保存为图片 将当前MFC程序保存为图片 c++ vc
将屏幕保存为图片 将当前MFC程序保存为图片 c++ vc
将屏幕保存为图片,使用vs2008编译通过。
[cpp] view plaincopy
- #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。
第一个:
[cpp] view plaincopy
- 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);
- }
第二个:
[cpp] view plaincopy
- 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();
- }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。