如何使用GDI +和C ++从SQL Server数据库加载和保存映像? [英] How do I load and save an image from an SQL Server database using GDI+ and C++?

查看:158
本文介绍了如何使用GDI +和C ++从SQL Server数据库加载和保存映像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要专门加载保存为blob的JPG图片。

解决方案

查看 Image :: Image(IStream *,BOOL)。这需要一个指向实现IStream接口的COM对象的指针。您可以通过 GlobalAlloc 分配一些全局内存, / a>,然后在返回的 CreateStreamOnHGlobal 上调用处理。它看起来像这样:

  shared_ptr< Image> CreateImage(BYTE * blob,size_t blobSize)
{
HGLOBAL hMem = :: GlobalAlloc(GMEM_MOVEABLE,blobSize);
BYTE * pImage =(BYTE *):: GlobalLock(hMem);

for(size_t iBlob = 0; iBlob< blobSize; ++ iBlob)
pImage [iBlob] = blob [iBlob]

:: GlobalUnlock(hMem);

CComPtr< IStream> spStream;
HRESULT hr = :: CreateStreamOnHGlobal(hMem,TRUE,& spStream);

shared_ptr< Image> image = new Image(spStream);
return image;
}

但是带有错误检查和错误检查p>

I need specifically to load a JPG image that was saved as a blob. GDI+ makes it very easy to retrieve images from files but not from databases...

解决方案

Take a look at Image::Image(IStream *, BOOL). This takes a pointer to a COM object implementing the IStream interface. You can get one of these by allocating some global memory with GlobalAlloc and then calling CreateStreamOnHGlobal on the returned handle. It'll look something like this:

shared_ptr<Image> CreateImage(BYTE *blob, size_t blobSize)
{
    HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE,blobSize);
    BYTE *pImage = (BYTE*)::GlobalLock(hMem);

    for (size_t iBlob = 0; iBlob < blobSize; ++iBlob)
        pImage[iBlob] = blob[iBlob];

    ::GlobalUnlock(hMem);

    CComPtr<IStream> spStream;
    HRESULT hr = ::CreateStreamOnHGlobal(hMem,TRUE,&spStream);

    shared_ptr<Image> image = new Image(spStream);  
    return image;
}

But with error checking and such (omitted here to make things clearer)

这篇关于如何使用GDI +和C ++从SQL Server数据库加载和保存映像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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