{"record":{"id":"c6e64194f1d2295c","repo":"dotnet/wpf","slug":"image-cantdealwithstream","errorCode":null,"errorMessage":"Image_CantDealWithStream","messagePattern":"Image_CantDealWithStream","errorType":"exception","errorClass":"FileFormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDecoder.cs","lineNumber":137,"sourceCode":"            _decoderHandle = SetupDecoderFromUriOrStream(\n                null,\n                bitmapStream,\n                cacheOption,\n                out clsId,\n                out isOriginalWritable,\n                out _uriStream,\n                out _unmanagedMemoryStream,\n                out _safeFilehandle\n                );\n\n            if (_uriStream == null)\n            {\n                GC.SuppressFinalize(this);\n            }\n\n            if (clsId != Guid.Empty && clsId != expectedClsId)\n            {\n                throw new FileFormatException(null, SR.Image_CantDealWithStream);\n            }\n\n            _stream = bitmapStream;\n            _createOptions = createOptions;\n            _cacheOption = cacheOption;\n            _syncObject = _decoderHandle;\n            _isOriginalWritable = isOriginalWritable;\n            Initialize(null);\n        }\n\n        /// <summary>\n        /// Constructor\n        /// </summary>\n        internal BitmapDecoder(\n            SafeMILHandle decoderHandle,\n            BitmapDecoder decoder,\n            Uri baseUri,\n            Uri uri,","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDecoder.cs#L119-L155","documentation":"The stream-based BitmapDecoder constructor validates that the stream's decoded CLSID matches the decoder type being constructed. When the stream contains a different image format than the specific decoder expects, a FileFormatException with Image_CantDealWithStream is thrown.","triggerScenarios":"Constructing e.g. new JpegBitmapDecoder(stream, ...) with a stream holding PNG/GIF data; feeding a stream whose embedded clsId is non-empty but not equal to the expected decoder CLSID.","commonSituations":"Downloading images where Content-Type lies about the format; database BLOBs stored with the wrong format label; user-uploaded files that bypassed extension checks.","solutions":["Use BitmapDecoder.Create(stream, createOptions, cacheOption) for automatic format detection.","Probe the stream's magic bytes (first 8-16 bytes) before choosing a format-specific decoder.","Ensure the stream is positioned at 0 and contains a complete image payload.","Catch FileFormatException and retry with the generic factory."],"exampleFix":"// before\nvar dec = new JpegBitmapDecoder(ms, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); // ms holds PNG\n// after\nvar dec = BitmapDecoder.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);","handlingStrategy":"validation","validationCode":"static bool IsPng(Stream s) { s.Position = 0; Span<byte> b = stackalloc byte[8]; s.Read(b); s.Position = 0; return b[0] == 0x89 && b[1] == (byte)'P'; }","typeGuard":"bool CanDecode(Stream s) => s != null && s.CanRead && s.Length > 0;","tryCatchPattern":"try { decoder = new JpegBitmapDecoder(stream, opts, cache); }\ncatch (FileFormatException) { stream.Position = 0; decoder = BitmapDecoder.Create(stream, opts, cache); }","preventionTips":["Use BitmapDecoder.Create (auto-detect) instead of format-specific constructors on untrusted streams","Probe magic bytes before constructing a typed decoder","Reset stream Position to 0 before decoding","Sanitize uploads by re-encoding rather than trusting stored formats"],"tags":["wpf","wic","image-format","stream","file-format"],"backgroundTag":"incompatible-source-type","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}