CImage::Load() from memory without using CreateStreamOnHGlobal

后端 未结 1 594
不知归路
不知归路 2021-01-27 02:00

I am displaying a live view video from camera. Every frame I download into a byte array (pImageData) which I have to allocate.

Now, to display, I am using a CImage (MFC)

相关标签:
1条回答
  • 2021-01-27 02:47

    Thanks to Hans for pointing out SHCreateMemStream, which I did not know existed. The code is much cleaner, but still unsure whether SHCreateMemStream creates a copy internally (documentation is unclear)

    [edit] As per Jonathan' comments, looks like it still has to make a copy internally. Dang ..

    Final code

    // pImageData is an array with bytes, size is the sizeOfThatArray
    
    // Still not clear if this is making a copy internally
    IStream* pMemStream = SHCreateMemStream(pImageData, size);
    
    CComPtr<IStream> stream;
    
    stream.Attach(pMemStream); // Need to Attach, otherwise ownership is not transferred and we leak memory
    
    CImage image;
    image.Load(stream); 
    
    // .. display image
    
    image.Destroy();
    
    // No need for further cleanup, CComPtr does the job
    
    0 讨论(0)
提交回复
热议问题