如何在程序运行中打开一幅图片 就是运行程序后,选“文件”->“打开”,然后选择路径导入一幅图片,而不是在编写时导入一个固定位置的图片 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 BOOLShowPIC(CDC *pDC, CString strPath, CRect rect,int ShowType){ CPen newpen(PS_DOT,1,RGB(255,255,255)); CPen *pOldPen=pDC->SelectObject(&newpen); pDC->Rectangle(rect); pDC->SelectObject(pOldPen); //显示BMP JPG GIF等格式的图片 IStream *pStm; CFileStatus fstatus; CFile file; LONG cb; HGLOBAL hGlobal; //打开文件并检测文件的有效性 if (file.Open(strPath,CFile::modeRead)&& file.GetStatus(strPath,fstatus)&& ((cb = fstatus.m_size) != -1)) { hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb); LPVOID pvData = NULL; if (hGlobal != NULL) { pvData = GlobalLock(hGlobal); if (pvData != NULL) { file.ReadHuge(pvData, cb); GlobalUnlock(hGlobal); CreateStreamOnHGlobal(hGlobal, TRUE, &pStm); } } } else { return false; } //打开文件结束 //显示JPEG和GIF格式的图片,GIF只能显示一帧,还不能显示动画, //要显示动画GIF请使用ACTIVE控//件。 IPicture *pPic; //load image from file stream if(SUCCEEDED(OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic))) { OLE_XSIZE_HIMETRIC hmWidth; OLE_YSIZE_HIMETRIC hmHeight; pPic->get_Width(&hmWidth); pPic->get_Height(&hmHeight); double fX,fY; //得到图片的高度与宽度 fX = (double)pDC->GetDeviceCaps(HORZRES)*(double)hmWidth/ ((double)pDC->GetDeviceCaps(HORZSIZE)*100.0); fY = (double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/ ((double)pDC->GetDeviceCaps(VERTSIZE)*100.0); long nwidth,nheight,nleft,ntop; int Rx,Ry; Rx=rect.Width(); Ry=rect.Height(); nheight=Ry; nwidth=Rx; nleft=rect.left; ntop=rect.top; //检测显示模式 //是否拉伸充满区域显示 if(ShowType==1) { nheight=Ry; nwidth=Rx; nleft=rect.left; ntop=rect.top; } //是否居中且按正常比例显示 else if(ShowType==2) { if(fX<Rx) nwidth=(long)fX; if(fY<Ry) nheight=(long)fY; if((fY>Ry)||(fX>Rx)) { double temp,temp2; temp=Ry/fY; temp2=Rx/fX; if(temp<temp2) { nwidth=(long)(fX*temp); nleft=(Rx-nwidth)/2+rect.left; } else if(temp>=temp2) { nheight=(long)(fY*temp2); ntop=(Ry-nheight)/2+rect.top; } } } //用 Render函数显示图片 //if(FAILED(pPic->Render(*pDC,rect.left,rect.top ,rect.Width(),rect.Height(),0, if(FAILED(pPic->Render(*pDC,nleft,ntop,nwidth,nheight,0, hmHeight,hmWidth,-hmHeight,NULL))) { pPic->Release(); return false; } pPic->Release(); } else { return false; } //释放内存 GlobalUnlock(hGlobal); GlobalFree(hGlobal); return true;} VC如何实现带图标显示的浏览器地址栏 如何将int转化为BSTR 用socket可以发送arp欺骗包吗? 请教CToolBar几个小问题. 非常急的问题,请高手帮忙! Wait...之类的函数能不能保证先调用的就先返回? 紧急求助:C语言函数 要求在VC下功过运行(100分 我在C语言发帖了但没人给回复) 视图切换的问题,急急!! 怎么把两个MediaPlayer控件做成画中画的形式,请高手指教!请高手指教! 关于CSplitterWnd的问题? 如何打开音乐文件 共享XP介面源码
{
CPen newpen(PS_DOT,1,RGB(255,255,255));
CPen *pOldPen=pDC->SelectObject(&newpen);
pDC->Rectangle(rect);
pDC->SelectObject(pOldPen);
//显示BMP JPG GIF等格式的图片
IStream *pStm;
CFileStatus fstatus;
CFile file;
LONG cb;
HGLOBAL hGlobal;
//打开文件并检测文件的有效性
if (file.Open(strPath,CFile::modeRead)&&
file.GetStatus(strPath,fstatus)&&
((cb = fstatus.m_size) != -1))
{
hGlobal = GlobalAlloc(GMEM_MOVEABLE, cb);
LPVOID pvData = NULL;
if (hGlobal != NULL)
{
pvData = GlobalLock(hGlobal);
if (pvData != NULL)
{
file.ReadHuge(pvData, cb);
GlobalUnlock(hGlobal);
CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);
}
}
}
else
{
return false;
}
//打开文件结束
//显示JPEG和GIF格式的图片,GIF只能显示一帧,还不能显示动画,
//要显示动画GIF请使用ACTIVE控//件。
IPicture *pPic;
//load image from file stream
if(SUCCEEDED(OleLoadPicture(pStm,fstatus.m_size,TRUE,IID_IPicture,(LPVOID*)&pPic)))
{
OLE_XSIZE_HIMETRIC hmWidth;
OLE_YSIZE_HIMETRIC hmHeight;
pPic->get_Width(&hmWidth);
pPic->get_Height(&hmHeight);
double fX,fY;
//得到图片的高度与宽度
fX = (double)pDC->GetDeviceCaps(HORZRES)*(double)hmWidth/
((double)pDC->GetDeviceCaps(HORZSIZE)*100.0);
fY = (double)pDC->GetDeviceCaps(VERTRES)*(double)hmHeight/
((double)pDC->GetDeviceCaps(VERTSIZE)*100.0);
long nwidth,nheight,nleft,ntop;
int Rx,Ry;
Rx=rect.Width();
Ry=rect.Height();
nheight=Ry;
nwidth=Rx;
nleft=rect.left;
ntop=rect.top;
//检测显示模式
//是否拉伸充满区域显示
if(ShowType==1)
{
nheight=Ry;
nwidth=Rx;
nleft=rect.left;
ntop=rect.top;
}
//是否居中且按正常比例显示
else if(ShowType==2)
{
if(fX<Rx) nwidth=(long)fX;
if(fY<Ry) nheight=(long)fY;
if((fY>Ry)||(fX>Rx))
{
double temp,temp2;
temp=Ry/fY;
temp2=Rx/fX;
if(temp<temp2)
{
nwidth=(long)(fX*temp);
nleft=(Rx-nwidth)/2+rect.left;
}
else if(temp>=temp2)
{
nheight=(long)(fY*temp2);
ntop=(Ry-nheight)/2+rect.top;
}
}
}
//用 Render函数显示图片
//if(FAILED(pPic->Render(*pDC,rect.left,rect.top ,rect.Width(),rect.Height(),0,
if(FAILED(pPic->Render(*pDC,nleft,ntop,nwidth,nheight,0,
hmHeight,hmWidth,-hmHeight,NULL)))
{
pPic->Release();
return false;
}
pPic->Release();
}
else
{
return false;
}
//释放内存
GlobalUnlock(hGlobal);
GlobalFree(hGlobal); return true;}