mfcsdi

Image Loading in Device context of SDI(MFC VC++)


How to load Images from disk to Device context of and SDI MFC application. I want to choose images from existing folder at runtime.


Solution

  • It is probably the easiest to use CImage, see: http://msdn.microsoft.com/en-us/library/bwea7by5(v=vs.80).aspx

    It's something like:

    void CMyDlg::OnBnClickedOpenImage()
    {
        CFileDialog dialog(TRUE, NULL, NULL,
            OFN_FILEMUSTEXIST | OFN_HIDEREADONLY,
            L"Windows Bitmap (*.bmp)|*.bmp|JPEG Compressed Image (*.jpg;*.jpeg)|*.jpg; *.jpeg|PNG Image (*.png)|*.png|All Images (*.bmp;*.jpg;*.jpeg;*.png)|*.bmp; *.jpg; *.jpeg; *.png|All Files (*.*)|*.*||");
    
        if (dialog.DoModal() == IDOK)
        {
            HDC hDC = pDC->GetSafeHdc();
            CImage image;
            image.Load(dialog.GetPathName());
            image.Draw(hDC, 0, 0);
        }
    }
    

    Edit: Added the open file dialog.