How can I create an Image in GDI+ from a Base64-Encoded string in C++?

前端 未结 2 730
日久生厌
日久生厌 2020-12-19 11:26

I have an application, currently written in C#, which can take a Base64-encoded string and turn it into an Image (a TIFF image in this case), and vice versa. In C# this is a

2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-19 12:00

    OK, using the info from the Base64 decoder I linked and the example Ben Straub linked, I got it working

    using namespace Gdiplus; // Using GDI+
    
    Graphics graphics(hdc); // Get this however you get this
    
    std::string encodedImage = "";
    
    std::string decodedImage = base64_decode(encodedImage); // using the base64 
                                                            // library I linked
    
    DWORD imageSize = decodedImage.length();
    HGLOBAL hMem = ::GlobalAlloc(GMEM_MOVEABLE, imageSize);
    LPVOID pImage = ::GlobalLock(hMem);
    memcpy(pImage, decodedImage.c_str(), imageSize);
    
    IStream* pStream = NULL;
    ::CreateStreamOnHGlobal(hMem, FALSE, &pStream);
    
    Image image(pStream);
    
    graphics.DrawImage(&image, destRect);
    
    pStream->Release();
    GlobalUnlock(hMem);
    GlobalFree(hMem);
    

    I'm sure it can be improved considerably, but it works.

提交回复
热议问题