JPG,动态GIF,BMP一些常用解决办法

[入库:2005年8月19日] [更新:2007年3月24日]

本文简介:选择自 i_like_cpp 的 blog

jpg,动态gif,bmp一些常用解决办法

常用方式0,使用gif89a类:

优势: 一招鲜吃遍天,上至动态gif,下至静态jpg,bmp两句搞定,简单方便

缺点: 有时会出现边缘效果, 有的图像会挺明显

使用方法:
#include "gif89a.h"
cgif89a* gif;
在调用函数内添加:
 gif=new cgif89a(this->m_hwnd);
 gif->load("test.gif");
 gif->setposition(100,100);
 gif->play();

 //gif->pause(true); //pause(true)为暂停gif的播放
 //gif->pause(false);//pause(false)为继续gif的播放


常用方式1,使用ipicture:

优势: ms早期的图片类, 可以显示所有的静态图片, 显示动态gif尚需一个gif的图象储存结构gifimage,如果有兴趣可以看看薛碧的如何在vc中显示动态的gif

缺点: 用户自主定义不太方便, 薛碧文章是介绍在rc静态编辑显示动态gif的办法,本来打算改写一下作个gif类, 发现了gif89a, 于是没作了多余工

调用方法:
cstring spath;

spath=_t("1.jpg");
 //cdc* pdc = getdlgitem(idc_control)->getdc();//获得控件dc
 crect zcrect;
 getclientrect(&zcrect);
 showjpggif(pdc,spath,zcrect.left,zcrect.top,zcrect.width(),zcrect.height());

函数实现

bool showjpggif(cdc* pdc,cstring strpath, int x, int y ,int width, int height)
{
 istream *pstm;

 cfilestatus fstatus;

 cfile file;

 long cb;

 //打开文件并检测文件的有效性

 if (file.open(strpath,cfile&::moderead)&&

 file.getstatus(strpath,fstatus)&&

((cb = fstatus.m_size) != -1))
 {
hglobal hglobal = globalalloc(gmem_moveable, cb);

lpvoid pvdata = null;

if (hglobal != null)
{
 pvdata = globallock(hglobal);

 if (pvdata != null)
 {
file.read(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;

//get image height and width

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;

//use render function display image

if(failed(ppic->render(*pdc, x, y, width, height,0,

 hmheight,hmwidth,-hmheight,null)))
 {
 ppic->release();
 return false;
}
 ppic->release();
}
else
{
return false;
}
return true;
}


常用方式2, 使用ms cimage

优点: ms的最新图像类,mfc/atl通用, 应该是可靠的,使用也很简单方便,较为灵活,目的应该为替代ipicture类

缺点: 还是不能完美的解决动态gif问题, #include <atlimage.h>

cimage m_image;

//ps: 以下插入调用函数
 cstring strfilter;
 csimplearray<guid> aguidfiletypes;
 hresult hresult;
 //cimage m_image;

 // 获取cimage支持的图像文件的过滤字符串
 hresult = m_image.getexporterfilterstring(strfilter,aguidfiletypes,_t( "all image files"));
 if (failed(hresult))
 {
messagebox("getexporterfilter调用失败!");
return;
 }
 cfiledialog dlg(true, null, null, ofn_filemustexist, strfilter);
 if(idok != dlg.domodal())
return;

 m_image.destroy();
 // 将外部图像文件装载到cimage对象中
 hresult = m_image.load(dlg.getfilename());
 if (failed(hresult))
 {
messagebox("调用图像文件失败!");
return;
 }

 // 设置主窗口标题栏内容
 cstring str;
 str.loadstring(afx_ids_app_title);
 afxgetmainwnd()->setwindowtext(str + " - " +dlg.getfilename());

 invalidate(); // 强制调用ondraw

//ps: 以下插入重画函数

void ctemp7view::ondraw(cdc* pdc)
{
ctemp7doc* pdoc = getdocument.);
assert_valid(pdoc);

 // todo: 在此处为本机数据添加绘制代码
if (!m_image.isnull())
{
 crect zcrect;
 getclientrect(&zcrect);
 m_image.draw(pdc->m_hdc,zcrect.left,zcrect.top,zcrect.width(),zcrect.height());
}
}

本文演示代码打包下载: pic_test.rar

本文关键:JPG,动态GIF,BMP一些常用解决办法
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top