我要在VS2005下开发一个基于三星6410开发板和WinCE6.0操作系统的应用软件,其中需要在ListBox中显示JPG格式的图片文件,然后双击图片名就在指定的Static Text控件中显示该图片。在网上查了很多资料,用了IPicture和CImage都没有支持,最后用IImage就可以了。这个过程确实很痛苦,为了给后来者一点参考的价值,下面我将我的源程序列出来供借鉴。
包含的头文件:
#include <initguid.h>#include <imaging.h>#pragma comment (lib,"Ole32.lib")注意:必须按上面的顺序!
void CCameraImgAnalyseDlg::OnLbnDblclkLstImagefile(){ // TODO: Add your control notification handler code here CString strFilePath = L" Card\\"; CString strFileName; CString strFileType;mFileType.GetWindowTextW(strFileType)
if(strFileType == "图片文件")
{ int index = mImgFileList.GetCurSel(); mImgFileList.GetText(index,strFileName); strFilePath = strFilePath + strFileName;CDC *pDC;
CRect rc; mPicShow.GetClientRect(rc); pDC = GetDlgItem(IDC_STA_SHOWAREA)->GetDC(); IImage * m_pImage; IImagingFactory * m_pImagingFactory; HRESULT hr;//COM初始化
if (FAILED(hr = CoInitializeEx(NULL, COINIT_MULTITHREADED))) { goto END; }//创建COM实例
if(FAILED(hr = CoCreateInstance(CLSID_ImagingFactory,NULL,CLSCTX_INPROC_SERVER,IID_IImagingFactory,(void**) &m_pImagingFactory))) { goto END; }//从文件中创建图片
if(FAILED(hr = m_pImagingFactory->CreateImageFromFile(strFilePath, &m_pImage))) { goto END; } //绘制图片 if(FAILED(hr = m_pImage->Draw(pDC->m_hDC,&rc,NULL))) { goto END; } } else if(strFileType == "视频文件") {}
else { } END: //释放资源 if(m_pImage != NULL) { m_pImage->Release(); m_pImage = NULL; }if(m_pImagingFactory != NULL)
{ m_pImagingFactory->Release(); m_pImagingFactory = NULL; }CoUninitialize();
}