| 订阅 | 在线投稿
分享
 
 
 

C/C++:如何在程序中加载JPG图片?

来源:互联网  宽屏版  评论
2008-06-01 02:07:51

本文为【C/C++:如何在程序中加载JPG图片?】的汉字拼音对照版显示拼音

MFCtigongdeCWndzhiyoumorenjiazaiBMPwenjiandejiekouduiJPGdengtuxiangshibuzhichideershijizhongjingchangxuyaoyongdaofeiBMPdetupianjiazaitamenxuyao使shiyongCOMjishushouxianxieruxiahanshu

yixiashiyinyongpianduan

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("%swufabeidakai"),fname);

MessageBox(str);

return FALSE;

}

//qudewenjiandaxiao

DWORD dwFileSize=GetFileSize(hFile,NULL);

if((DWORD)-1==dwFileSize)

{

CloseHandle(hFile);

MessageBox(_T("tuxiangwenjianshikongde"));

return FALSE;

}

//duqutuxiangwenjian

LPVOID pvData;

//anwenjiandaxiaofenpeineicun

HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,dwFileSize);

if(NULL==hGlobal)

{

CloseHandle(hFile);

MessageBox(_T("neicunbuzuwufafenpeizugouneicun"));

return FALSE;

}

pvData=GlobalLock(hGlobal);

if(NULL==pvData)

{

GlobalUnlock(hGlobal);

CloseHandle(hFile);

MessageBox(_T("wufasuodingneicun"));

return FALSE;

}

DWORD dwFileRead=0;

BOOL bRead=ReadFile(hFile,pvData,dwFileSize,&dwFileRead,NULL);

GlobalUnlock(hGlobal);

CloseHandle(hFile);

if(FALSE==bRead)

{

MessageBox(_T("duwenjianchucuo"));

return FALSE;

}

LPSTREAM pstm=NULL;

//congyifenpeineicunshengchengIStreamliu

HRESULT hr=CreateStreamOnHGlobal(hGlobal,TRUE,&pstm);

if(!SUCCEEDED(hr))

{

MessageBox(_T("shengchengliucaozuoshibai"));

if(pstm!=NULL)

pstm->Release();

return FALSE;

}

else if(pstm==NULL)

{

MessageBox(_T("shengchengliucaozuoshibai"));

return FALSE;

}

if(!*lppi)

(*lppi)->Release();

hr=OleLoadPicture(pstm,dwFileSize,FALSE,IID_IPicture,(LPVOID*)&(*lppi));

pstm->Release();

if(!SUCCEEDED(hr))

{

MessageBox(_T("jiazaicaozuoshibai"));

return FALSE;

}

else if(*lppi==NULL)

{

MessageBox(_T("jiazaicaozuoshibai"));

return FALSE;

}

return TRUE;

}

ranhouzaitouwenjianzhongjiarubianliangshengminghehanshushengming

yixiashiyinyongpianduan

BOOL LoadMyJpegFile(CString fname,LPPICTURE *lppi);

LPPICTURE m_lppi;//jiazaituxiangwenjiandeliu

BOOL m_bHadLoad;//yijingjiazailebeijingtuxiang

ranhouzaiOnPainthanshuzhongjiaru

if(m_bHadLoad)

{

CDC *pDC=GetDC();

CRect rc;

long hmWidth=0;

long hmHeight=0;

m_lppi->get_Height(&hmHeight);

m_lppi->get_Width(&hmWidth);

GetClientRect(&rc);

int nWidth,nHeight;

nWidth=rc.Width();

nHeight=rc.Height();

HRESULT hr=m_lppi->Render(pDC->m_hDC,nWidth,0,-nWidth,nHeight,hmWidth,hmHeight,-hmWidth,-hmHeight,&rc);

}

zaiOnInitDialoghanshuzhongzheyangtiaoyongshangmiandejiazaihanshu

TCHAR strPath[MAX_PATH];

memset(strPath,0,MAX_PATH);

GetCurrentDirectory(MAX_PATH,strPath);

wcscat_s(strPath,MAX_PATH,_T("\\a_bear.jpg"));

m_bHadLoad=LoadMyJpegFile(strPath,&m_lppi);

jiukeyixianshijpgtupianlezuihouyaojidezaiOnDestroyhanshuzhongjiaru

m_lppi->Release();

laishifangduixiang

原文
  MFC提供的CWnd只有默认加载BMP文件的接口,对JPG等图像是不支持的,而实际中经常需要用到非BMP的图片,加载它们需要使用COM技术。首先写如下函数: 以下是引用片段: 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; }   然后在头文件中加入变量声明和函数声明: 以下是引用片段: BOOL LoadMyJpegFile(CString fname,LPPICTURE *lppi); LPPICTURE m_lppi;//加载图像文件的流 BOOL m_bHadLoad;//已经加载了背景图像 然后在OnPaint函数中加入: if(m_bHadLoad) { CDC *pDC=GetDC(); CRect rc; long hmWidth=0; long hmHeight=0; m_lppi->get_Height(&hmHeight); m_lppi->get_Width(&hmWidth); GetClientRect(&rc); int nWidth,nHeight; nWidth=rc.Width(); nHeight=rc.Height(); HRESULT hr=m_lppi->Render(pDC->m_hDC,nWidth,0,-nWidth,nHeight,hmWidth,hmHeight,-hmWidth,-hmHeight,&rc); } 在OnInitDialog函数中这样调用上面的加载函数: TCHAR strPath[MAX_PATH]; memset(strPath,0,MAX_PATH); GetCurrentDirectory(MAX_PATH,strPath); wcscat_s(strPath,MAX_PATH,_T("\\a_bear.jpg")); m_bHadLoad=LoadMyJpegFile(strPath,&m_lppi); 就可以显示jpg图片了,最后要记得在OnDestroy函数中加入: m_lppi->Release(); 来释放对象。
󰈣󰈤
 
 
 
>>返回首页<<
 
 热帖排行
 
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
©2005- 王朝网络 版权所有