简单绘图类

来源:互联网 发布:网络还原精灵 编辑:程序博客网 时间:2024/05/20 12:21

简单绘图类

文章来源: http://www.codeproject.com/KB/graphics/cpicture.aspx

代码下载: http://www.codeproject.com/KB/graphics/cpicture/CPicture_Demo.zip

         http://www.codeproject.com/KB/graphics/cpicture/CPicture_src.zip

本文介绍了一个简单的类,能将大部分类型的图片绘制到屏幕上。

此外,该类还可以将图片存为资源。通过使用GetResource函数,可以很容易的将波形文件或文档模板存储到应用程序中。本文所举的例子将三种类型的影像存储在内存中:1号图片是JPEG格式,2号图片是GIF格式,3号图片是位图。

通过这个例子你将会学习到如何使用该类。下面将展示最为核心的一些代码。

下面的例子同样用到了Keith RuleCmemDC类来增强绘图功能。

// The function 'LoadFromBuffer' prepares the IPicture class.
// It requires a buffer filled with the contents of an image.
 
bool CPicture::LoadFromBuffer(BYTE* pBuff, int nSize)
{
    bool bResult = false;
 
    HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize);
    void* pData = GlobalLock(hGlobal);
    memcpy(pData, pBuff, nSize);
    GlobalUnlock(hGlobal);
 
    IStream* pStream = NULL;
 
    if (CreateStreamOnHGlobal(hGlobal, TRUE, &pStream) == S_OK)
    {
        HRESULT hr;
        if ((hr = OleLoadPicture(pStream, nSize, FALSE, IID_IPicture,
                                 (LPVOID *)&m_pPicture)) == S_OK)
            bResult = true;
 
        pStream->Release();
    }
 
    return bResult;
}
 
// This function draws the picture on the device context
 
bool CPicture::Draw(CDC* pDC, int x, int y, int cx, int cy)
{
    long hmWidth;
    long hmHeight;
    m_pPicture->get_Width(&hmWidth);
    m_pPicture->get_Height(&hmHeight);
 
    if (m_pPicture->Render(pDC->m_hDC, x, y, cx, cy, 0, 
                              hmHeight, hmWidth, -hmHeight, NULL) == S_OK)
        return true;
 
    return false;
}