0

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

Aakash
  • 7
  • 6

1 Answers1

0

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.

user1
  • 536
  • 5
  • 8
  • I know i can load image like that, but i want to choose from folder ,when press open button it should open a dirctory ,from there i want to choose any image. – Aakash Jan 25 '13 at 06:11
  • I hope I understand it correctly now. I updated the answer, which is the event handler of the OnClick of your "Open" button. – user1 Feb 13 '13 at 13:27
  • How i will get pDC intialized ,when i am adding this code,it is showing debug assertion faild error – Aakash Feb 14 '13 at 05:49
  • You're talking about a "Device context", I guess you refer to some paint method? Then the easiest way is probably to store the CImage as a member variable of the SDI document/window class and use that in the paint method where you have your DC. – user1 Feb 14 '13 at 18:39