zoukankan      html  css  js  c++  java
  • 加载图片资源的方法

     
    BOOL CImageManager::LoadResImage(UINT nResID,
    								 LPCTSTR lpType,
    								 Bitmap *&lpImage, 
    								 HINSTANCE hInstance)
    {
    	lpImage  = NULL;
    
    	hInstance = (NULL == hInstance) ? ::AfxGetResourceHandle() : hInstance;
    	// If bmp,use system load.
    	if (RT_BITMAP == lpType)
    	{
    		HBITMAP hBmp = ::LoadBitmap(hInstance, MAKEINTRESOURCE(nResID));
    		lpImage = Bitmap::FromHBITMAP(hBmp, 0);
    		::DeleteObject(hBmp);
    
    		if (!lpImage)
    		{
    			TRACE("lpImage is NULL\n");
    			return FALSE;
    		}
    		else
    		{
    			if (Gdiplus::Ok != lpImage->GetLastStatus())
    			{
    				TRACE("lpImage is error\n");
    				return FALSE;
    			}
    			else
    			{
    				return TRUE;
    			}
    		}
    	}
    
    	// User-Defined
    
    	HRSRC hRsrc = ::FindResource(hInstance, MAKEINTRESOURCE(nResID), lpType);
    	if (!hRsrc)
    	{
    		TRACE("hRscs is NULL\n");
    		return FALSE;
    	}
    
    	// Load resource into memory ---------------------------------
    	DWORD len = ::SizeofResource(hInstance, hRsrc);
    
    	BYTE *lpRsrc = (BYTE*)::LoadResource(hInstance, hRsrc);
    	if (!lpRsrc)
    	{
    		TRACE("lpRsrc is NULL\n");
    		return FALSE;
    	}
    
    	// Allocate global memory on which to create stream ----------
    	HGLOBAL hMem = ::GlobalAlloc(GMEM_FIXED, len);
    
    	BYTE *pMem = (BYTE*)::GlobalLock(hMem);
    	memcpy(pMem, lpRsrc, len);
    
    	IStream *pStream = NULL;
    	// You can query MSDN,why I use TRUE, -- hgy notes.
    	HRESULT ht = ::CreateStreamOnHGlobal(hMem, TRUE, &pStream);
    	if (S_OK != ht)
    	{
    		TRACE("ht is error\n");
    		return FALSE;
    	}
    
    	// Load from stream -------------------------------------------
    	lpImage = Gdiplus::Bitmap::FromStream(pStream);
    
    	// free/release stuff -----------------------------------------
    	::GlobalUnlock(hMem);
    	pStream->Release();
    	::FreeResource(lpRsrc);
    
    	if (!lpImage)
    	{
    		TRACE("lpImage is NULL\n");
    		return FALSE;
    	}
    	else
    	{
    		if (Gdiplus::Ok != lpImage->GetLastStatus())
    		{
    			TRACE("lpImage is error\n");
    			return FALSE;
    		}
    		else
    		{
    			return TRUE;
    		}
    	}
    }
    
    自己写的,经常用到,记录下,总不能每次都重写吧。
  • 相关阅读:
    python正则表达式基础,以及pattern.match(),re.match(),pattern.search(),re.search()方法的使用和区别
    python正则表达式--分组、后向引用、前(后)向断言
    python正则表达式--flag修饰符、match对象属性
    mybatis-核心配置文件和mappe.xml
    mybatis mapper标签
    spring JdbcTemplate 常用的自定义工具包
    web基础
    8.24 JDBC 调用存储过程
    8.24 事务处理
    8.24 批处理
  • 原文地址:https://www.cnblogs.com/hgy413/p/3693566.html
Copyright © 2011-2022 走看看