分类:
2008-10-15 16:40:39
BOOL LoadMyJpegFile(CString fname,LPPICTURE *lppi)
{
HANDLE hFile=CreateFile(fname,GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);if(hFile==INVALID_HANDLE_VALUE)
{
CString str;
str.Format(_T("%s无法被打开"),fname);
MessageBox(str);
return FALSE;
}
//取得文件大小
DWORD dwFileSize=GetFileSize(hFile,NULL);
if((DWORD)-1==dwFileSize)
{
CloseHandle(hFile);
MessageBox(_T("图像文件是空的"));
return FALSE;
}
//读取图像文件
LPVOID pvData;//按文件大小分配内存
HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize);
if(NULL==hGlobal)
{
CloseHandle(hFile);
MessageBox(_T("内存不足,无法分配足够内存"));
return FALSE;
} pvData=GlobalLock(hGlobal);
if(NULL==pvData)
{
GlobalUnlock(hGlobal);
CloseHandle(hFile);
MessageBox(_T("无法锁定内存"));
return FALSE;
} DWORD dwFileRead=0;
BOOL bRead=ReadFile(hFile,pvData,dwFileSize,&dwFileRead,NULL);
GlobalUnlock(hGlobal);
CloseHandle(hFile);
if(FALSE==bRead)
{
MessageBox(_T("读文件出错"));
return FALSE;
} LPSTREAM pstm=NULL;
//从已分配内存生成IStream流
HRESULT hr=CreateStreamOnHGlobal(hGlobal,TRUE,&pstm);
if(!SUCCEEDED(hr))
{
MessageBox(_T("生成流操作失败"));
if(pstm!=NULL)
pstm->Release();
return FALSE;
}
else if(pstm==NULL)
{
MessageBox(_T("生成流操作失败"));
return FALSE;
} if(!*lppi)
(*lppi)->Release();
hr=OleLoadPicture(pstm,dwFileSize,FALSE,IID_IPicture,(LPVOID*)&(*lppi));
pstm->Release();
if(!SUCCEEDED(hr))
{
MessageBox(_T("加载操作失败"));
return FALSE;
}
else if(*lppi==NULL)
{
MessageBox(_T("加载操作失败"));
return FALSE;
}
return TRUE;
}