Windows Mobile 上顯示png,jpg,bmp等圖片

來源:互聯網
上載者:User

 不只是可以顯示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. }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.