This sample code below demonstrates using CImage (from ATLImage.h) to load a file based image, save it to a stream, and create a COM picture interface which can be the return value in an OLE automation property.
The crazy thing is that OleLoadPicture fails with an error code indicating a corrupted file format. However, I am able to demonstrate in the error condition that the stream can again be loaded in a second CImage and saved back to disk. Comparison of the two file based bitmaps shows them to be identical.
It is a confounding problem as this seems the best way to pass image data around in COM.
Can anyone think of why this fails? My only suspicion is that the saved BMP somehow does not conform to some standard but then again I can look at it with any image viewer
COleStreamFile ImageStream;
if (ImageStream.CreateMemoryStream())
{
CImage ImageDIB;
if (SUCCEEDED(ImageDIB.Load(L"C:\\Users\\abantly\\AppData\\Local\\Temp\\Image.bmp")))
{
IStream * pImageStream = ImageStream.GetStream();
if (pImageStream && SUCCEEDED(ImageDIB.Save(pImageStream,Gdiplus::ImageFormatBMP)))
{
CComPtr<IPicture> Picture;
HRESULT hr = OleLoadPicture(pImageStream,(LONG)ImageStream.GetLength(),FALSE,IID_IPicture,(LPVOID *)&Picture);
if (SUCCEEDED(hr))
{
CComQIPtr<IPictureDisp> PictureDisp(Picture);
}
else
{
CImage ImageDIB2;
if (SUCCEEDED(ImageDIB2.Load(pImageStream)))
ImageDIB2.Save(L"C:\\Users\\abantly\\AppData\\Local\\Temp\\Image2.bmp");
}
pImageStream->Release();
}
}
}