/*在StdAfx.h中静态调用diplus.lib
#ifndef ULONG_PTR
#define ULONG_PTR unsigned long*
#include "GdiPlus.h"
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")
#endif
在类的头文件中定义,以下成员变量,用来初始化GDI+的使用和结束使用。
或在APP中添加以下成员变量
GdiplusStartupInput m_gdiplusStartupInput;
ULONG_PTR m_gdiplusToken;
然后在OnCreate()函数中加入初始化GDI+的函数:或在APP的InitInstance中
GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
在OnDestroy()函数中加入结束GDI+使用的函数:
GdiplusShutdown(m_gdiplusToken);
*/
void Cmfc对话框Dlg::OnBnClickedButton3()
{
CImage mmage;
HWND hWnd = ::GetDesktopWindow();//获得屏幕的HWND.
HDC hScreenDC = ::GetDC(hWnd); //获得屏幕的HDC.
HDC MemDC = ::CreateCompatibleDC(hScreenDC);
RECT rect;
::GetWindowRect(hWnd,&rect);
HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC,rect.right,rect.bottom);
HGDIOBJ hOldBMP = ::SelectObject(MemDC,hBitmap);
::BitBlt(MemDC,0,0,rect.right,rect.bottom,hScreenDC,rect.left,rect.top,SRCCOPY);
hBitmap=(HBITMAP)::SelectObject(MemDC,hOldBMP);
mmage.Attach(hBitmap);
IStream* pStmImage = NULL;
HGLOBAL hMemBmp = GlobalAlloc(GMEM_MOVEABLE,0);//可移动的缓冲区
if (hMemBmp == NULL) return;
CreateStreamOnHGlobal(hMemBmp, FALSE, &pStmImage);//将内存区B作为流的起始
if (pStmImage == NULL)
{
GlobalFree(hMemBmp);
MessageBox(L"为空");
return ;
}
mmage.Save(pStmImage,ImageFormatJPEG);
BYTE* pbyBmp = (BYTE *)GlobalLock(hMemBmp);//得到缓冲区的起始地址
CFile mfile(L"234.jpg",CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
GlobalUnlock(hMemBmp);
mfile.Write(pbyBmp,GlobalSize(hMemBmp));
mfile.Close();
pStmImage->Release();
GlobalFree(hMemBmp);
if(mmage)
mmage.Destroy();
::DeleteObject(MemDC);
::ReleaseDC(hWnd,hScreenDC);
}
//
int C简单抓图Dlg::GetImageCLSID(const WCHAR* format, CLSID* pCLSID)
{
UINT num = 0;
UINT size = 0;
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return FALSE; // 编码信息不可用
//分配内存
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return FALSE; // 分配失败
//获得系统中可用的编码方式的所有信息
GetImageEncoders(num, size, pImageCodecInfo);
//在可用编码信息中查找format格式是否被支持
for(UINT i = 0; i < num; ++i)
{ //MimeType:编码方式的具体描述
if(wcscmp(pImageCodecInfo[i].MimeType, format) == 0 )
{
*pCLSID = pImageCodecInfo[i].Clsid;
free(pImageCodecInfo);
return TRUE;
}
}
free(pImageCodecInfo);
return FALSE;
return 0;
}
WCHAR* C简单抓图Dlg::ToWChar(char* str)
{
static WCHAR buffer[1024];
wcsset(buffer,0);
MultiByteToWideChar(CP_ACP,0,str,strlen(str),buffer,1024);
return buffer;
}