將屏幕保存為圖片 將當(dāng)前MFC程序保存為圖片 c++ vc 收藏
將屏幕保存為圖片,使用vs2008編譯通過(guò)。
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);//創(chuàng)建兼容DC
HBITMAP hBitMap = ::CreateCompatibleBitmap(hDC, rect.right, rect.bottom);//創(chuàng)建兼容位圖
HBITMAP hOldMap = (HBITMAP)::SelectObject(hDCMem, hBitMap);//將位圖選入DC,并保存返回值
::BitBlt(hDCMem, 0, 0, rect.right, rect.bottom, hDC, 0, 0, SRCCOPY);//將屏幕DC的圖象復(fù)制到內(nèi)存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);//創(chuàng)建兼容DC
HBITMAP hBitMap = ::CreateCompatibleBitmap(hDC, rect.right, rect.bottom);//創(chuàng)建兼容位圖
HBITMAP hOldMap = (HBITMAP)::SelectObject(hDCMem, hBitMap);//將位圖選入DC,并保存返回值
::BitBlt(hDCMem, 0, 0, rect.right, rect.bottom, hDC, 0, 0, SRCCOPY);//將屏幕DC的圖象復(fù)制到內(nèi)存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;
}
將當(dāng)前MFC程序(這里是單文檔程序)保存為圖片,使用vs2005。第一個(gè)使用CImage類保存圖片,第二個(gè)使用BITMAPINFO。
第一個(gè):
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);//創(chuàng)建兼容DC
HBITMAP hBitMap = ::CreateCompatibleBitmap(hDC, rect.right-rect.left, rect.bottom-rect.top);//創(chuàng)建兼容位圖
HBITMAP hOldMap = (HBITMAP)::SelectObject(hDCMem, hBitMap);//將位圖選入DC,并保存返回值
::BitBlt(hDCMem, 0, 0, rect.right-rect.left, rect.bottom-rect.top, hDC, 0, 0, SRCCOPY);//將屏幕DC的圖象復(fù)制到內(nèi)存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);//創(chuàng)建兼容DC
HBITMAP hBitMap = ::CreateCompatibleBitmap(hDC, rect.right-rect.left, rect.bottom-rect.top);//創(chuàng)建兼容位圖
HBITMAP hOldMap = (HBITMAP)::SelectObject(hDCMem, hBitMap);//將位圖選入DC,并保存返回值
::BitBlt(hDCMem, 0, 0, rect.right-rect.left, rect.bottom-rect.top, hDC, 0, 0, SRCCOPY);//將屏幕DC的圖象復(fù)制到內(nèi)存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);
}
第二個(gè):
view plaincopy to clipboardprint?
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);
//計(jì)算調(diào)色板大小
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;
//獲取位圖的實(shí)際數(shù)據(jù)
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;
//向文件中寫入位圖數(shù)據(jù)
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();
}