您的位置:程序门 -> vc/mfc -> 界面



如何在vc中显示png图片


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何在vc中显示png图片
发表于:2007-03-26 11:14:46 楼主
最近写个程序,其中需要打开一副png格式的图片,不过vc中好像没有直接提供支持的类或函数啊,各位高手们帮帮忙吧~~~~~~~~~~~~~~~~
发表于:2007-03-26 12:53:421楼 得分:0
唉,这个你早问我就好了啊,呵呵。

如果你有一定的自学能力的话,赶紧打开msdn(或一些相关网上论坛等)学一下gdi+,你将收获到的,不仅仅是搞定png这种琐事!

不过当然了,我在这里并不是帮助gdi+搞宣传。gdi+太封闭了。
发表于:2007-03-26 16:06:302楼 得分:0
http://www.codeproject.com/bitmap/cximage.asp
发表于:2007-03-26 16:37:073楼 得分:0
hresult   showpic(tchar*   lpstrfile,   hwnd   hwnd,   int   nscrwidth,   int   nscrheight)  
{
hdc   hdc_temp=getdc(hwnd);  

ipicture   *ppic;  
istream   *pstm;  

bool   bresult;  
handle   hfile   =   null;  
dword   dwfilesize,   dwbyteread;  

//打开硬盘中的图形文件  
hfile=createfile(lpstrfile,   generic_read,   file_share_read,   null,   open_existing,   file_attribute_normal,   null);  

if   (hfile   !=   invalid_handle_value)  
{  
dwfilesize=getfilesize(hfile,   null);//获取文件字节数  
if   (dwfilesize==0xffffffff)  
return   e_fail;  
}  
else  
{  
return   e_fail;  
}  


//分配全局存储空间  
hglobal   hglobal   =   globalalloc(gmem_moveable,   dwfilesize);  
lpvoid   pvdata   =   null;  

if   (hglobal   ==   null)  
return   e_fail;  

if   ((pvdata   =   globallock(hglobal))   ==   null)//锁定分配内存块  
return   e_fail;  

readfile(hfile,   pvdata,   dwfilesize,   &dwbyteread,   null);//把文件读入内存缓冲区  
globalunlock(hglobal);  
createstreamonhglobal(hglobal,   true,   &pstm);  

//装入图形文件  
bresult   =   oleloadpicture(pstm,dwfilesize,true,iid_ipicture,(lpvoid*)&ppic);  

if(failed(bresult))  
return   e_fail;  

ole_xsize_himetric   hmwidth;//图片的真实宽度  
ole_ysize_himetric   hmheight;//图片的真实高度  
ppic-> get_width(&hmwidth);  
ppic-> get_height(&hmheight);  


//将图形输出到屏幕上(有点像bitblt)  

bresult   =   ppic-> render(hdc_temp,   0,   0,   nscrheight,   nscrwidth,   0,   hmheight,   hmwidth,   -hmheight,   null);  

ppic-> release();  

closehandle(hfile);//关闭打开的文件  

if   (succeeded(bresult))  
{  
return   s_ok;  
}  
else  
{  
return   e_fail;  
}  
}


以上代码自己改吧,适合更多的图片格式
发表于:2007-03-26 16:44:074楼 得分:0
同意用gdi+
简单方便
毕竟我们没有必要去研究png等图像的编码原理
造汽车不必从炼铁开始
发表于:2007-03-27 08:33:485楼 得分:0
to   loveinsnowing   and   楼主:  

不要再使用oleloadpicture了,那会让你看起来很愚蠢。同样功能的函数,你看看gdi+会有几行:

hresult   showpic(const   tchar*   lpstrfile,   hdc   hdc,   int   x,   int   y,   int   iwidth,   int   iheight)
{
_assert(lpstrfile);
uses_conversion;

//   create   bitmap   object   and   load   image
auto_ptr <bitmap>   pbmp(new   bitmap(ct2cw(lpstrfile)));
if(!pbmp.get()   ¦ ¦   ok   !=   pbmp-> laststatus())
return   e_failed;

//   draw   bitmap   on   dc
graphic   grp(hdc);
if(ok   !=   grp.drawimage(pbmp.get(),   x,   y,   iwidth,   iheight))
return   e_failed;

return   s_ok;
}
发表于:2007-03-27 13:37:436楼 得分:0
用cimage类就可以


快速检索

最新资讯
热门点击