We're getting this weird exception when trying to create a BitmapDecoder off an ASP.Net response stream. This is the line of code that throws the exception:
BitmapDecoder dec = BitmapDecoder.Create(
Request.Files[0].InputStream,
BitmapCreateOptions.PreservePixelFormat,
BitmapCacheOption.OnLoad);
Here's some info on the file being uploaded:
Filename: bank statement.jpg, Content length: 266041, Mime type: image/jpeg
This is the exception stack trace:
System.IO.IOException: Cannot read from the stream. --->
System.Runtime.InteropServices.COMException (0x88982F72):
Exception from HRESULT: 0x88982F72
--- End of inner exception stack trace ---
at System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle)
at System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache)
at System.Windows.Media.Imaging.BitmapDecoder.Create(Stream bitmapStream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption)
Does anyone have any ideas on how we can prevent this from happening?
0x88982f72 is actually WINCODEC_ERR_STREAMREAD (from Codec Error Codes), which indeed is "Cannot read from the stream".
If the stream is coming in from "the wild world" (aka: the Internet), it maybe mean that it's ...just garbage or a hack tentative. If your own tests work fine and don't exhibit this problem, I think it's fair to just handle this specific error and fail gracefully.
User contributions licensed under CC BY-SA 3.0