I'm trying to use WIC to load images in C#, with SharpDX as a wrapper (this is a Direct2D application written in .NET). I can load my image perfectly fine by creating a BitmapDecoder
like so:
C# Code:
new BitmapDecoder(Factory, fileName, NativeFileAccess.Read, DecodeOptions.CacheOnLoad)
C++ Equivalent:
hr = m_pIWICFactory->CreateDecoderFromFilename(
fileName,
NULL,
GENERIC_READ,
WICDecodeMetadataCacheOnLoad,
&pIDecoder);
By the way, fileName
contains the path to a JPEG image. Now, this works perfectly well, but it breaks down if I try to load the image using a stream instead:
C# Code:
new BitmapDecoder(Factory, stream, DecodeOptions.CacheOnLoad)
C++ Equivalent:
hr = m_pIWICFactory->CreateDecoderFromStream(
pIWICStream,
NULL,
WICDecodeMetadataCacheOnLoad,
&pIDecoder);
This is literally the same data as is present in the JPEG file, and it works for the most part just like the previous way. But it breaks when I call SharpDX.Direct2D1.Bitmap.FromWicBitmap()
(ID2D1RenderTarget::CreateBitmapFromWicBitmap
). The former approach works flawlessly, while the latter approach causes this function to return HRESULT 0x88982F60 (WINCODEC_ERR_BADIMAGE).
To be clear: there is no difference in how the image is loaded other than loading it from a stream instead of a file name.
Why is this happening, and how do I fix it? I need to be able to load images that I have access to only as streams, and I do not want to save them to temporary files to accomplish that.