DLL图片资源加载到内存中

4年以前  |  阅读数:210 次  |  编程语言:C++ 

头文件(.h)

class CResourceImage
{
public:
    CResourceImage();
    ~CResourceImage();

    BOOL LoadImage( HMODULE hModule, const WCHAR * lpName, const WCHAR * lpType );

    int GetWidth();
    int GetHeight();

    void Draw(HDC hDC, int x, int y, int cx, int cy, int x_src, int y_src, int cx_src, int cy_src);

private:
    HGLOBAL m_hGlobal;
    Bitmap* m_pBitmap;
};

#endif

实现文件(.cpp)

CResourceImage::CResourceImage() : m_hGlobal(NULL),m_pBitmap(NULL)
{

}

CResourceImage::~CResourceImage()
{

}

int CResourceImage::GetWidth()
{
    if (m_pBitmap)
    {
        return m_pBitmap->GetWidth();
    }
    return 0;
}

int CResourceImage::GetHeight()
{
    if (m_pBitmap)
    {
        return m_pBitmap->GetHeight();
    }
    return 0;
}

void CResourceImage::Draw(HDC hDC, int x, int y, int cx, int cy, int x_src, int y_src, int cx_src, int cy_src)
{
    if (m_pBitmap)
    {
        Graphics g(hDC);
        g.DrawImage(m_pBitmap,RectF(x, y, cx, cy),x_src,y_src,cx_src,cy_src, UnitPixel);
    }
}

BOOL CResourceImage::LoadImage( HMODULE hModule, const WCHAR * lpName, const WCHAR * lpType )
{
    CResReader sResReader;
    sResReader.LoadResource(hModule, lpName, lpType);

    if(sResReader.GetData() == NULL) return FALSE;
    if(sResReader.GetDataSize() <= 0) return FALSE;

    BOOL bSuccess = TRUE;
    do 
    {
        if (m_hGlobal)
        {
            GlobalFree(m_hGlobal);
            m_hGlobal = NULL;
        }

        if (m_pBitmap)
        {
            delete m_pBitmap;
            m_pBitmap = NULL;
        }

        bSuccess = FALSE;

        //分配文件长度大小的内存块
        m_hGlobal = ::GlobalAlloc (GPTR, sResReader.GetDataSize());
        if (m_hGlobal == NULL) break;

        //将文件数据读入内存块
        void* pBuffer = (void*)m_hGlobal;
        memcpy(pBuffer, sResReader.GetData(), sResReader.GetDataSize());

        //在内存块上创建流
        IStream* pStream = NULL;
        bSuccess = SUCCEEDED (::CreateStreamOnHGlobal (m_hGlobal, TRUE, &pStream));
        if(bSuccess && pStream)
        {
            m_pBitmap = new Bitmap(pStream, FALSE);  
            if (!m_pBitmap || m_pBitmap->GetLastStatus() != Ok)
            {
                delete m_pBitmap;
            }
        }
    } while (FALSE);

    return TRUE;
}
 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
将二进制数据转为16进制以便显示
获取IMSI
获取IMEI
Java生成UUID
PHP自定义函数获取搜索引擎来源关键字的方法
让你成为最历害的git提交人
在Zeus Web Server中安装PHP语言支持
指定应用ID以获取对应的应用名称
再谈PHP中单双引号的区别详解
Python 2与Python 3版本和编码的对比
php+ajax+json 详解及实例代码
Yii2汉字转拼音类的实例代码
php封装的page分页类完整实例
php数组合并array_merge()函数使用注意事项
PHP实现简单爬虫的方法
PHP设计模式之工厂模式与单例模式
php实现数组中索引关联数据转换成json对象的方法
wget使用技巧