Answered by:
How to load a JPG image without the file dialog

Question
-
Have been able to load a JPG image with MFC C++ programming, but only after the file dialog is displayed and
a selection made. I need to be able to load the Jpg image without displying the file dialog.
Please advise,
PXU
The following code fragments may help answer my programming problem.
The first "OnCreate" routine displays a selected image correctly.The second "OnCreate" routine fails with "80004005 - Unspecified error",
I understand that I am not coding this correctly, but I have not found
the solution.// DLG6.cpp : implementation file
#include "stdafx.h"
#include "DLG6.h"
#include <atlimage.h>
#include <comdef.h>
#include <afxstr.h>
enum SizesEnum
{
SIZE_HALF,
SIZE_ORIGINAL,
SIZE_DOUBLE,
SIZE_FILL,
SIZE_NONE
};CImage imgOriginal;
int m_nFilterLoad;
SizesEnum m_nImageSize;
int DLG6::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;CString strFilter;
HRESULT hResult;
strFilter = _T("Jpeg image files (*.JPG)||");
CFileDialog dlg(TRUE,
_T("JPG"),
ex,
(OFN_FILEMUSTEXIST | OFN_HIDEREADONLY),
strFilter);
dlg.m_ofn.lpstrCustomFilter = NULL;
hResult = (int)dlg.DoModal();
if(FAILED(hResult)) return -1;hResult = imgOriginal.Load(dlg.GetFileName());
if (FAILED(hResult)) return -1;m_nImageSize = SIZE_FILL;
Invalidate();
UpdateWindow();
return 0;
}int DLG6::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;HANDLE fhx;
HRESULT hResult;
fhx = CreateFile(_T("C:\\test2.JPG"),
(GENERIC_READ),
NULL,
NULL,
OPEN_EXISTING,
NULL,
NULL);
if (fhx == INVALID_HANDLE_VALUE)
{
AfxMessageBox (_T("create fail"));
return -1;
}
hResult = imgOriginal.Load(_T("C:\\test2.JPG"));
if (FAILED(hResult)) {
CString fmt;
fmt.Format(_T("Load failed:\n%x - %s"),
hResult, _com_error(hResult).ErrorMessage());
::AfxMessageBox(fmt);
return -1;;
}
m_nImageSize = SIZE_FILL;
Invalidate();
UpdateWindow();
return 0;
}
Cordially,
PXU- Edited by PXU Wednesday, December 2, 2009 6:43 PM
Tuesday, December 1, 2009 6:27 PM
Answers
-
The wizard-generated MFC app has a message handler in the CWinApp-derived class for the ID_FILE_OPEN menu command. You can add your own OnFileOpen function in the app and do whatever you want there. Don't call CWinApp::OnFileOpen if you don't want the dialog displayed.
- Marked as answer by Nancy Shao Thursday, December 3, 2009 7:43 AM
Tuesday, December 1, 2009 6:45 PM
All replies
-
The wizard-generated MFC app has a message handler in the CWinApp-derived class for the ID_FILE_OPEN menu command. You can add your own OnFileOpen function in the app and do whatever you want there. Don't call CWinApp::OnFileOpen if you don't want the dialog displayed.
- Marked as answer by Nancy Shao Thursday, December 3, 2009 7:43 AM
Tuesday, December 1, 2009 6:45 PM -
Take a look at the CImage() documentation.Tuesday, December 1, 2009 8:54 PM