15,979
社区成员
发帖
与我相关
我的任务
分享
/************************************************************** 代码开始 **********************************************/
int CAllPoleDiglog::ShowThumbNail(int nId)
{
char strTmp[MAX_PARA_LEN];
CString strPath;
LPVOID pvData;
IStream *pStm;
IPicture *pPic;
OLE_XSIZE_HIMETRIC hmWidth;
OLE_YSIZE_HIMETRIC hmHeight;
CDC* pDC;
int s32PicWidth;
int s32PicHeight;
int x = 0;
int y = 0;
CFileStatus fstatus;
CFile file;
ULONGLONG cb;
HGLOBAL hGlobal;
/************************************************
step2: 打开 jpeg 文件 m_apFileName[nId]
*************************************************/
strPath.Format(m_apFileName[nId]);
if (!file.Open(strPath,CFile::modeRead))
{
MessageBox("file has not found");
return -1;
}
if (!file.GetStatus(strPath, fstatus))
{
MessageBox("file status is not right");
return -1;
}
if ((cb =fstatus.m_size)<=0)
{
MessageBox("file size is error");
return -1;
}
/************************************************
step3: 根据文件大小分配内存空间,记得释放内存
*************************************************/
hGlobal = GlobalAlloc(GMEM_MOVEABLE, (unsigned int)cb);
if (hGlobal == NULL)
{
return -1;
}
/************************************************
step4: 锁定刚才分配的内存空间
*************************************************/
pvData = GlobalLock(hGlobal);
if (pvData == NULL)
{
GlobalFree(hGlobal); // 记得释放内存
return -1;
}
/************************************************
step5: 将文件放到流中
*************************************************/
file.Read(pvData,(unsigned int)cb);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
/************************************************
step6: 从流中加载图片 (能显示jpeg图片和gif图片的一帧)
*************************************************/
if(OleLoadPicture(pStm,(LONG)fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic)!=S_OK)
{
GlobalFree(hGlobal); // 记得释放内存
return -1;
}
/************************************************
step7: 获取图像宽和高,注意这里的宽和高不是图像的分辨率
*************************************************/
pPic->get_Width(&hmWidth);
pPic->get_Height(&hmHeight);
/************************************************
step8: use render function display image
*************************************************/
x = m_aCRectThumbNail[nId].left;
y = m_aCRectThumbNail[nId].top;
s32PicWidth = m_aCRectThumbNail[nId].right - m_aCRectThumbNail[nId].left;
s32PicHeight = m_aCRectThumbNail[nId].bottom - m_aCRectThumbNail[nId].top;
pDC = GetDC();
if(FAILED(pPic->Render(*pDC, x, y, s32PicWidth, s32PicHeight, 0, hmHeight, hmWidth, -hmHeight, NULL)))
{
pPic->Release();
GlobalFree(hGlobal); // 记得释放内存
return -1;
}
if (NULL != hGlobal)
{
GlobalFree(hGlobal); // 记得释放内存
}
ReleaseDC(pDC);
pPic->Release();
return 0;
}
/************************************************************** 代码结束 **********************************************/