1. 程式人生 > >儲存BITMAP點陣圖為各種圖片

儲存BITMAP點陣圖為各種圖片

void CClipScreenToolDlg::OnBnClickedBtnSave()
{
	CBitmap* pBitmap=CBitmap::FromHandle(CopyScreenToBitmap(&m_RectTracker.m_rect,TRUE));
	CFileDialog FileDlg(FALSE,_T("png"),_T("未命名"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("PNG 圖片格式 (*.png)|*.png|BMP 圖片格式 (*.bmp)|*.bmp|JPG 圖片格式 (*.jpg)|*.jpg||"));
	int nRet=FileDlg.DoModal();
	if (nRet==IDOK)
	{
		CString StringExe=_T("png");
		StringExe=FileDlg.GetFileExt();
		CString StringPath=FileDlg.GetPathName();

		CImage  Img;
		Img.Attach(*pBitmap);
		
		if (StringExe==_T("png"))
		{
			Img.Save(StringPath,Gdiplus::ImageFormatPNG);
		}else if (StringExe==_T("bmp"))
		{
			Img.Save(StringPath,Gdiplus::ImageFormatBMP);
		}else if (StringExe==_T("jpg"))
		{
			Img.Save(StringPath,Gdiplus::ImageFormatJPEG);
		}
		Img.Detach();
		PostQuitMessage(0);
	}
}

下面是獲取點陣圖,大家可以獲取自己的點陣圖:

HBITMAP CClipScreenToolDlg::CopyScreenToBitmap(LPRECT lpRect,BOOL bSave)
{
	ShowWindow(SW_HIDE);
	HDC hScreenDC,hMemDC;
	HBITMAP hBitmap,hOldBitmap;
	if (IsRectEmpty(lpRect))
	{
		return NULL;
	}
	hScreenDC=CreateDC(_T("DISPLAY"),NULL,NULL,NULL);
	hMemDC=CreateCompatibleDC(hScreenDC);
	int Left=lpRect->left;
	int Top=lpRect->top;
	int Right=lpRect->right;
	int Bottom=lpRect->bottom;
	if(Left<0) Left=0;
	if(Top<0) Top=0;
	if(Right>m_xScreen) Right=m_xScreen;
	if(Bottom>m_yScreen) Bottom=m_yScreen;
	int nWidth=Right-Left;
	int nHeight=Bottom-Top;
	hBitmap=CreateCompatibleBitmap(hScreenDC,nWidth,nHeight);
	hOldBitmap=(HBITMAP)SelectObject(hMemDC,hBitmap);
	
	BitBlt(hMemDC,0,0,nWidth,nHeight,hScreenDC,Left,Top,SRCCOPY);
	hBitmap=(HBITMAP)SelectObject(hMemDC,hOldBitmap);
	if (bSave==TRUE)
	{
		if (OpenClipboard())
		{
			EmptyClipboard();
			SetClipboardData(CF_BITMAP,hBitmap);
			CloseClipboard();
		}
	}
	DeleteDC(hMemDC);
	DeleteDC(hScreenDC);
	ShowWindow(SW_SHOW);
	return hBitmap;
}