VC加载jpeg, png图片的方法
2010-09-15 11:06
423 查看
在VC中我们可以直接利用LoadIcon, LoadBitmap或LoadImage来加载bmp和ico图片, 但是加载jpg和png等格式就没有这么方便了. 其实我们可以通过两个系统提供的两个com接口IPicture和IStream类实现类似于VB的Picture控件, 具体的用法请查看msdn或google下, 下面代码是我以前在网上找的一个例子, 主要用到了几个API函数: AfxGetResourceHandle, FindResource, SizeofResource, LoadResource, CreateStreamOnHGlobal, 如果需要保存为新的图片就需要用到IPicture接口了.
另外, 网上还有一个用IPicture写的Picture类, 源码如下:
m_pImage = new CImage(); ImageFromIDResource(*m_pImage, IDB_PNG_IDISK, _T("PNG")); HBITMAP hBitmap = m_pImage->Detach(); m_statBitmap.SetBitmap(hBitmap); void ImageFromIDResource(CImage& image, UINT nID, LPCTSTR lpType) { HINSTANCE hInst = AfxGetResourceHandle(); HRSRC hRsrc = ::FindResource (hInst,MAKEINTRESOURCE(nID),lpType); if(hRsrc == NULL) return; DWORD dwLen = SizeofResource(hInst, hRsrc); BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc); if (!lpRsrc) return;; HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, dwLen); BYTE* pmem = (BYTE*)GlobalLock(m_hMem); memcpy(pmem,lpRsrc,dwLen); IStream* pstm; CreateStreamOnHGlobal(m_hMem,FALSE,&pstm); image.Load(pstm); GlobalUnlock(m_hMem); pstm->Release(); FreeResource(lpRsrc); }
另外, 网上还有一个用IPicture写的Picture类, 源码如下:
//////////////////////////////////////////////////////////////// // MSDN Magazine -- October 2001 // If this code works, it was written by Paul DiLascia. // If not, I don't know who wrote it. // Compiles with Visual C++ 6.0 for Windows 98 and probably Windows 2000 too. // Set tabsize = 3 in your editor. // #pragma once #include <atlbase.h> ////////////////// // Picture object--encapsulates IPicture // class CPicture { public: CPicture(); ~CPicture(); // Load frm various sosurces BOOL Load(UINT nIDRes); BOOL Load(LPCTSTR pszPathName); BOOL Load(CFile& file); BOOL Load(CArchive& ar); BOOL Load(IStream* pstm); // render to device context BOOL Render(CDC* pDC, CRect rc=CRect(0,0,0,0), LPCRECT prcMFBounds=NULL) const; CSize GetImageSize(CDC* pDC=NULL) const; operator IPicture*() { return m_spIPicture; } void GetHIMETRICSize(OLE_XSIZE_HIMETRIC& cx, OLE_YSIZE_HIMETRIC& cy) const { cx = cy = 0; const_cast<CPicture*>(this)->m_hr = m_spIPicture->get_Width(&cx); ASSERT(SUCCEEDED(m_hr)); const_cast<CPicture*>(this)->m_hr = m_spIPicture->get_Height(&cy); ASSERT(SUCCEEDED(m_hr)); } void Free() { if (m_spIPicture) { m_spIPicture.Release(); } } protected: CComQIPtr<IPicture>m_spIPicture; // ATL smart pointer to IPicture HRESULT m_hr; // last error code }; #include "StdAfx.h" #include "Picture.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif //////////////////////////////////////////////////////////////// // CPicture implementation // CPicture::CPicture() { } CPicture::~CPicture() { } ////////////////// // Load from resource. Looks for "IMAGE" type. // BOOL CPicture::Load(UINT nIDRes) { // find resource in resource file HINSTANCE hInst = AfxGetResourceHandle(); HRSRC hRsrc = ::FindResource(hInst, MAKEINTRESOURCE(nIDRes), "IMAGE"); // type if (!hRsrc) return FALSE; // load resource into memory DWORD len = SizeofResource(hInst, hRsrc); BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc); if (!lpRsrc) return FALSE; // create memory file and load it CMemFile file(lpRsrc, len); BOOL bRet = Load(file); FreeResource(hRsrc); return bRet; } ////////////////// // Load from path name. // BOOL CPicture::Load(LPCTSTR pszPathName) { CFile file; if (!file.Open(pszPathName, CFile::modeRead|CFile::shareDenyWrite)) return FALSE; BOOL bRet = Load(file); file.Close(); return bRet; } ////////////////// // Load from CFile // BOOL CPicture::Load(CFile& file) { CArchive ar(&file, CArchive::load | CArchive::bNoFlushOnDelete); return Load(ar); } ////////////////// // Load from archive--create stream and load from stream. // BOOL CPicture::Load(CArchive& ar) { CArchiveStream arcstream(&ar); return Load((IStream*)&arcstream); } ////////////////// // Load from stream (IStream). This is the one that really does it: call // OleLoadPicture to do the work. // BOOL CPicture::Load(IStream* pstm) { Free(); HRESULT hr = OleLoadPicture(pstm, 0, FALSE, IID_IPicture, (void**)&m_spIPicture); ASSERT(SUCCEEDED(hr) && m_spIPicture); return TRUE; } ////////////////// // Render to device context. Covert to HIMETRIC for IPicture. // BOOL CPicture::Render(CDC* pDC, CRect rc, LPCRECT prcMFBounds) const { ASSERT(pDC); if (rc.IsRectNull()) { CSize sz = GetImageSize(pDC); rc.right = sz.cx; rc.bottom = sz.cy; } long hmWidth,hmHeight; // HIMETRIC units GetHIMETRICSize(hmWidth, hmHeight); m_spIPicture->Render(*pDC, rc.left, rc.top, rc.Width(), rc.Height(), 0, hmHeight, hmWidth, -hmHeight, prcMFBounds); return TRUE; } ////////////////// // Get image size in pixels. Converts from HIMETRIC to device coords. // CSize CPicture::GetImageSize(CDC* pDC) const { if (!m_spIPicture) return CSize(0,0); LONG hmWidth, hmHeight; // HIMETRIC units m_spIPicture->get_Width(&hmWidth); m_spIPicture->get_Height(&hmHeight); CSize sz(hmWidth,hmHeight); if (pDC==NULL) { CWindowDC dc(NULL); dc.HIMETRICtoDP(&sz); // convert to pixels } else { pDC->HIMETRICtoDP(&sz); } return sz; }
相关文章推荐
- VC 下加载 JPGJPEGGIFPNG 图片最简单的方法
- VC下加载JPG,JPEG,GIF,PNG图片最简单的方法
- VC 下加载 JPG / JPEG / GIF / PNG 图片最简单的方法
- VC 下加载 JPG / JPEG / GIF / PNG 图片最简单的方法
- vc++加载透明png图片方法——GDI+和CImage两种 ,vs2005 vs2008 PNG背景
- vc++加载透明png图片的两种方法-GDI+和CImage
- vc++加载透明png图片方法——GDI+和CImage两种
- vc++加载透明png图片方法——GDI+和CImage两种
- VC++加载透明png图片的两种常用方法——GDI+和CImage
- vc++加载透明png图片方法——GDI+和CImage两种
- VC下加载JPG/GIF/PNG图片的两种方法
- VC下加载JPG/GIF/PNG图片的两种方法
- VC下加载JPG/GIF/PNG图片的两种方法
- VC下加载JPG/GIF/PNG图片的两种方法
- VC下加载JPG/GIF/PNG图片的两种方法(转)
- VC下加载JPG/GIF/PNG图片的两种方法
- vc++加载透明png图片方法——GDI+和CImage两种
- vc++加载透明png图片方法——GDI+和CImage两种
- vc++加载透明png图片方法-GDI+和CImage两种
- VC下加载JPG/GIF/PNG图片的两种方法