How do I load and save an image from an SQL Server database using GDI+ and C++?

前端 未结 2 895
别跟我提以往
别跟我提以往 2021-01-13 19:24

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...

2条回答
  •  情歌与酒
    2021-01-13 19:51

    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 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 spStream;
        HRESULT hr = ::CreateStreamOnHGlobal(hMem,TRUE,&spStream);
    
        shared_ptr image = new Image(spStream);  
        return image;
    }
    

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

提交回复
热议问题