1. 程式人生 > >將圖片顯示在一個控制元件上

將圖片顯示在一個控制元件上

將圖片顯示在一個控制元件上

void CmfctestDlg::OnBnClickedBtnOpenimg()
{
	// TODO: Add your control notification handler code here
	CFileDialog filedlg(TRUE);
	filedlg.m_ofn.lpstrTitle = _T("開啟一個圖片");
	filedlg.m_ofn.lpstrFilter = _T("bmp Files(*.bmp)\0*.bmp\0png Files(*.png)\0*.png\0All Files(*.*)\0*.*\0Jpeg Files(*.jpg)\0*.jpg\0\0");

	if (IDOK == filedlg.DoModal())
	{
		MessageBox(_T("Open file!"));
	}
 	CString strpath =filedlg.GetPathName();//完成路徑
 	CString strfile = filedlg.GetFileName();//檔名
 	MessageBox(strpath);
 	MessageBox(strfile);
 	MessageBox(filedlg.GetFileExt());//副檔名
 	MessageBox(filedlg.GetFileTitle());//檔名不帶副檔名
 	MessageBox(filedlg.GetFolderPath());//檔案所在目錄

	HRESULT hrsult = m_img.Load(filedlg.GetPathName());
 	if (FAILED(hrsult))
 	{
 		MessageBox(_T("呼叫影象失敗!"));
 		return ;
 	}
	int cx = m_img.GetWidth();
	int cy = m_img.GetHeight();
	
	CRect rect;
	GetDlgItem(IDC_PITCURE)->GetWindowRect(rect);
	ScreenToClient(rect);
	GetDlgItem(IDC_PITCURE)->MoveWindow(rect.left,rect.top,cx,cy,TRUE);
	
	CWnd* pwnd = GetDlgItem(IDC_PITCURE);
	pwnd->GetClientRect(rect);

	CDC* pDc = pwnd->GetDC();

	m_img.Draw(pDc->m_hDC,rect);
	ReleaseDC(pDc);

}

將一個圖片進行縮放或擴充套件

void CmfctestDlg::CreateStretchImage(CImage* pImage,CImage* DstImage,int height,int width)
{
	if (pImage->IsDIBSection())
	{
		CDC* pImagedc1=CDC::FromHandle(pImage->GetDC());

		CBitmap* bitmap1=pImagedc1->GetCurrentBitmap();
		BITMAP bmpinfo;
		bitmap1->GetBitmap(&bmpinfo);

		DstImage->Create(width,height,bmpinfo.bmBitsPixel);
		CDC* dstdc=CDC::FromHandle(DstImage->GetDC());

		dstdc->SetStretchBltMode(HALFTONE);
		::SetBrushOrgEx(dstdc->m_hDC,0,0,NULL);

		StretchBlt(*dstdc,0,0,width,height,*pImagedc1,0,0,pImage->GetWidth(),pImage->GetHeight(),SRCCOPY);

		pImage->ReleaseDC();
		DstImage->ReleaseDC();
	}
}
將得到的image儲存到檔案

m_img2.Save(_T("c:\\2.bmp"));