[00013]-[2015-08-27]-[01]-[Windows 程序设计 ---GDI+ 截图---> BMP OR JPG OR PNG ...]

int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0;
UINT size = 0;
ImageCodecInfo* pImageCodecInfo = NULL;
Gdiplus::GetImageEncodersSize(&num, &size);
if(size == 0)
{
return -1;
}

pImageCodecInfo = (ImageCodecInfo*)malloc(size);
if(pImageCodecInfo==NULL)
{
return -1;
}

Gdiplus::GetImageEncoders(num, size, pImageCodecInfo);

for(UINT i = 0; i < num; i++)
{
if(wcscmp(pImageCodecInfo[i].MimeType, format) == 0)
{
*pClsid = pImageCodecInfo[i].Clsid;
free(pImageCodecInfo);
return i;
}
}

free(pImageCodecInfo);
return -1;
}

BOOL SaveHDCToFile(HDC hDC, LPRECT lpRect)
{
BOOL bRet = FALSE;
int nWidth = lpRect->right - lpRect->left;
int nHeight = lpRect->bottom - lpRect->top;

HDC memDC = CreateCompatibleDC(hDC);
HBITMAP hBmp = CreateCompatibleBitmap(hDC, nWidth, nHeight);
SelectObject(memDC, hBmp);
BitBlt(memDC, 0, 0, nWidth, nHeight,
hDC, 0, 0, SRCCOPY);

CLSID bmpClsid;
// //L"image/bmp" L"image/jpeg" L"image/gif" L"image/tiff" L"image/png"
GetEncoderClsid(L"image/jpeg", &bmpClsid);

Gdiplus::Bitmap* pBmpSrc = Gdiplus::Bitmap::FromHBITMAP(hBmp, NULL);

if(pBmpSrc->Save(L"1.jpg", &bmpClsid) == Ok)
{
bRet = TRUE;
}

delete pBmpSrc;

SelectObject(memDC, (HBITMAP)NULL);
DeleteDC(memDC);
DeleteObject(hBmp);
return bRet;
}

 

 

void CDemo01Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
HDC hDC = ::GetWindowDC(m_hWnd);
RECT rect;
::GetWindowRect(m_hWnd, &rect);
CString str;
str.Format("left=%d top=%d right=%d bottom=%d", rect.left, rect.top, rect.right, rect.bottom);
MessageBox(str);
SaveHDCToFile(hDC, &rect);
::ReleaseDC(m_hWnd, hDC);
}

 

void CDemo01Dlg::OnButton2() 
{
// TODO: Add your control notification handler code here
HDC hDC = ::GetDC(m_hWnd);
RECT rect;
::GetClientRect(m_hWnd, &rect);
CString str;
str.Format("left=%d top=%d right=%d bottom=%d", rect.left, rect.top, rect.right, rect.bottom);
MessageBox(str);
SaveHDCToFile(hDC, &rect);
::ReleaseDC(m_hWnd, hDC);
}

posted @ 2015-08-27 10:15  Auris  阅读(169)  评论(0编辑  收藏  举报