Windows Mobile 上显示png,jpg,bmp等图片

 不只是可以显示png,jpg,bmp等等都可以,gif也行,不过只能显示单帧。

一种方法是用IImagingFactory 中的CreateImageFromFile

先看看msdn:

This method lets an application create a decoded image object from a file.

Syntax

HRESULT CreateImageFromFile(
  const WCHAR* filename,
  IImage**     image
);

Parameters

filename

[in] A WCHAR array containing the name of the source file.

image

[out] A pointer to the resulting IImage interface pointer.

Return Value

If successful, this method returns S_OK.

This method may return E_POINTER if it fails.

Remarks

When the decoded image object is created, it only keeps a reference to the external data source and does not immediately decode the image. The decoded image opens the file in read-only mode and allows shared-read access to it.

Be aware that decoded image objects are read-only. In particular, you cannot modify the image data. However, you can display it onto a destination graphics context or push its data into an image sink. For more information, see IImage.

Requirements

OS Versions: Windows CE 5.0 and later.

Header: Imaging.h.

Link Library: Imaging.

 

所以很简单了

  1. /************************************************************************************
  2. *
  3. *   函数名称        ShowPng
  4. *   函数介绍        显示png图片
  5. *   入口参数        const WCHAR *filename,  //文件路径
  6. *                           CRect *pRect,                    //显示区域
  7. *                           CDC *pDc,                        //dc
  8. *   出口参数        无
  9. *   返回  值          void 
  10. *
  11. ***********************************************************************************/
  12. void ShowPng(const WCHAR *filename, CRect *pRect, CDC *pDc)
  13. {
  14.     IImagingFactory *pImageFactory = NULL;
  15.     IImage *pImage = NULL;
  16.     HRESULT hrCreInstance = CoCreateInstance( CLSID_ImagingFactory, NULL,                   CLSCTX_INPROC_SERVER,IID_IImagingFactory, (void **)&pImageFactory);
  17.     HRESULT hrLoadFile = pImageFactory->CreateImageFromFile(filename, &pImage);
  18.     if (S_OK != hrCreInstance || S_OK != hrLoadFile)
  19.     {
  20.         AfxMessageBox(L"加载图片失败!!!");
  21.         return;
  22.     }
  23.     pImage->Draw( pDc->GetSafeHdc(), pRect, NULL );
  24.     pImage->Release();
  25.     pImageFactory->Release();
  26. }

注意:

  1. #include <Imaging.h>

 

还有另外一种方法:

就是用SHLoadImageFile函数。

先看msdn:

This function reads an image file, decompresses it, and returns a handle to a bitmap in memory.

Syntax

HBITMAP SHLoadImageFile (
  LPCTSTR pszFileName
);

Parameters

pszFileName

[in] The name of the image file to be loaded.

Return Value

A handle to a bitmap if successful, NULL otherwise.

Remarks

This function converts files of several types, including GIF (Graphics Interchange Format), PNG (Portable Network Graphics), JPG (Joint Photographic Experts Group), ICO (icon), and BMP (bitmap) file formats. Other image file types may be supported if the correct decoder is installed.

When you no longer need the bitmap, call the DeleteObject function to delete it.

Requirements

Pocket PC: Windows Mobile 2003 and later.

OS Versions: Windows CE .NET 4.0 and later.

Header: Declared in Aygshell.h.

Library: Use Aygshell.lib.

 

  1. /************************************************************************************
  2. *
  3. *   函数名称        ShowPic
  4. *   函数介绍        显示png图片
  5. *   入口参数        const WCHAR *filename,  //文件路径
  6. *               CRect *pRect,           //显示区域
  7. *               CDC *pDc,       //dc
  8. *   出口参数        无
  9. *   返回  值       void 
  10. *
  11. ***********************************************************************************/
  12. void ShowPic(const WCHAR *filename, CRect *pRect, CDC *pDc)
  13. {
  14.     CDC dccom;
  15.     dccom.CreateCompatibleDC(pDc);
  16.     HBITMAP hbitmap = SHLoadImageFile(filename);
  17.     CBitmap *bk,pp;
  18.     bk = pp.FromHandle(hbitmap);
  19.     BITMAP bitmap;
  20.     bk->GetBitmap(&bitmap);
  21.     CBitmap *pOldbmp = dccom.SelectObject(bk);
  22.     pDc->StretchBlt(0,0,pRect->Width(),pRect->Height(),&dccom,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);
  23. }

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值