; CFile f;
if (!f.Open(lpszPathName, CFile::modeRead))
{
TRACE(_T("Failed to open file %s, Error:%x\n"), lpszPathName, ::GetLastError());
return FALSE;
}
file://get the file size
DWORD dwFileSize = f.GetLength();
file://Allocate memory based on file size
LPVOID pvData = NULL;
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);
if (hGlobal == NULL)
{
TRACE(_T("Failed to allocate memory for file %s, Error:%x\n"), lpszPathName, ::GetLastError());
return FALSE;
}
pvData = GlobalLock(hGlobal);
file://ASSERT(pvData);
if(pvData==NULL)
{
TRACE(_T("Failed to lock memory\r\n"));
return FALSE;
}
// read file and store in global memory
if (f.Read(pvData, dwFileSize) != dwFileSize)
{
TRACE(_T("Failed to read in image date from file %s, Error:%x\n"), lpszPathName, ::GetLastError());
GlobalUnlock(hGlobal);
GlobalFree(hGlobal);
return FALSE;
}
file://Tidy up the memory and close the file handle
GlobalUnlock(hGlobal);
file://create IStream* from global memory
LPSTREAM pStream = NULL;
if (FAILED(CreateStreamOnHGlobal(hGlobal, TRUE, &pStream)))
{
TRACE(_T("Failed to create IStream interface from file %s, Error:%x\n"), lpszPathName, ::GetLastError());
GlobalFree(hGlobal);
return FALSE;
}
// Create IPicture from image file
if (SUCCEEDED(::OleLoadPicture(pStream, dwFileSize, FALSE, IID_IPicture, (LPVOID*)&m_pPicture)))
{
short nType = PICTYPE_UNINITIALIZED;
if (SUCCEEDED(m_pPicture->get_Type(&nType)) &