使用 ATL CImage 从内存缓冲区加载图像 [英] Load image from memory buffer using ATL CImage

查看:42
本文介绍了使用 ATL CImage 从内存缓冲区加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 CImage 从 BYTE* 数组加载图像?到目前为止,我的解决方法是简单地创建一个临时文件,但有时此操作非常昂贵......可能有一些库,但我不想链接到其他库,我所需要的只是获取图像大小并有效地显示到屏幕上,而 CImage 就是我所需要的......

How can I load an image from BYTE* array using CImage ? My workaround until now is to simply create a temporary file, but this operation is very expensive sometimes ... There are probably libraries for that, but I do not want to link to other libraries, all I need is to get image size and effectively display to screen, and CImage is all I need for that ...

p->pbData 是一个 BYTE* 数组,p->dwDataLen 是一个保存数组大小的 DWORD

p->pbData is a BYTE* array and p->dwDataLen is a DWORD that hold the array size

我的代码:

ATL::CAtlTemporaryFile TempFile;  
TempFile.Create(NULL, GENERIC_WRITE | GENERIC_READ);  
TempFile.Write(p->pbData, p->dwDataLen);  
TempFile.HandsOff();  
ATL::CImage m_image;  
m_image.Load(TempFile.TempFileName());  
    TempFile.Close();
int h = m_image.GetHeight();  
int w = m_image.GetWidth();  
// rest of code here

    m_image.Destroy();  
m_image.ReleaseGDIPlus();` 

推荐答案

bool Create(BYTE* heap, DWORD heap_len, CImage& img)
{
    bool ret = false;
    HGLOBAL hGlobal = ::GlobalAlloc(GHND, heap_len);
    LPBYTE  lpByte  = (LPBYTE)::GlobalLock(hGlobal);
    CopyMemory(lpByte, heap, heap_len);
    ::GlobalUnlock(hGlobal);
    IStream* pStream = NULL;
    if ( SUCCEEDED(::CreateStreamOnHGlobal(hGlobal, FALSE, &pStream)) )
    {
        img.Destroy();
        img.Load(pStream);
        pStream->Release();
        ret = true;
    }
    GlobalFree(hGlobal);
    return ret;
}

这篇关于使用 ATL CImage 从内存缓冲区加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆