Load and Display image
Question
Friday, November 22, 2019 4:59 AM
can any one help me to open an image and display it using an MFC application in VC++.
image is gray scale image and file formats are TIFF,JPG and PNG
All replies (4)
Friday, November 22, 2019 9:42 AM ✅Answered
Hello,
Thank you for posting here.
>>can any one help me to open an image and display it using an MFC application in VC++.
It needs Picture Control. My demo uses button to trigger subsequent operations. FYI.
void CMFCApplication47Dlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
CWnd* m_pWnd;
m_pWnd = this->GetDlgItem(IDC_PICTURE);//IDC_PICTURE is the ID of Picture control
//********* load the picture ********************
CString filter;
filter = "File Type(*.png;*.tiff;*.jpg)|*.png;*.tiff;*.jpg||";
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, filter);
//**default path, no need to change
CString strForlder = TEXT("d:// ");
dlg.m_ofn.lpstrInitialDir = strForlder;
//**show modal dialog
if (dlg.DoModal() == IDOK)
{
try {
CRect rect;
CImage image;
image.Load(dlg.GetPathName().GetBuffer());
m_pWnd->GetWindowRect(&rect);
CWnd* pWnd = NULL;
pWnd = m_pWnd;
pWnd->GetClientRect(&rect);
CDC* pDc = NULL;
pDc = pWnd->GetDC();
pDc->SetStretchBltMode(STRETCH_HALFTONE);
image.Draw(pDc->m_hDC, rect);
ReleaseDC(pDc);
}
catch (CException * e)//&e
{
TCHAR szError[1024];
e->GetErrorMessage(szError,1024);
::AfxMessageBox(szError);
}
}
}
Best Regards,
Suarez Zhou
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Friday, November 22, 2019 8:09 AM
You can see this thread
Friday, November 22, 2019 8:37 AM
Hello,
you can use this CStatic class: https://www.codeproject.com/Articles/11507/PreviewCtrl
To load jpg etc and show it as Bitmap, use for example CImage class /en-us/cpp/atl-mfc-shared/reference/cimage-class?view=vs-2019 Load the file and use HBITMAP operator to convert it.
Regards, Guido
Tuesday, November 26, 2019 1:41 AM
Hello,
Do you resolve the issue? If you resolve the issue, please "Mark as answer" or "Vote as helpful" post to the appropriate answer, so that it will help other members to find solution quickly if they faces similar issue. If not, please feel free to contact us.
Best Regards,
Suarez Zhou
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.