{"record":{"id":"6d0bfb701f32af1b","repo":"dotnet/wpf","slug":"image-cantdealwithuri","errorCode":null,"errorMessage":"Image_CantDealWithUri","messagePattern":"Image_CantDealWithUri","errorType":"exception","errorClass":"FileFormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDecoder.cs","lineNumber":93,"sourceCode":"                    bitmapUri,\n                    null,\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\n            if (clsId != expectedClsId)\n            {\n                throw new FileFormatException(bitmapUri, SR.Image_CantDealWithUri);\n            }\n\n            _uri = bitmapUri;\n            _createOptions = createOptions;\n            _cacheOption = cacheOption;\n            _syncObject = _decoderHandle;\n            _isOriginalWritable = isOriginalWritable;\n            Initialize(decoder);\n        }\n\n        /// <summary>\n        /// Constructor\n        /// </summary>\n        internal BitmapDecoder(\n            Stream bitmapStream,\n            BitmapCreateOptions createOptions,\n            BitmapCacheOption cacheOption,\n            Guid expectedClsId","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDecoder.cs#L75-L111","documentation":"BitmapDecoder's URI constructor sniffs the file and verifies the underlying WIC decoder CLSID matches the decoder type being constructed. If the decoded image's actual format differs from the expected CLSID (e.g. asking for a PngBitmapDecoder on a JPEG file), a FileFormatException with Image_CantDealWithUri is thrown.","triggerScenarios":"Calling e.g. BitmapDecoder.Create with a mismatched derived decoder (new PngBitmapDecoder(uri, ...) on a non-PNG file), or a URI whose content WIC resolves to a different decoder CLSID than expectedClsId.","commonSituations":"Files renamed to the wrong extension without conversion; byte streams served with wrong content; mixed-format image folders processed with one decoder type; corrupted file headers causing WIC to detect a different format.","solutions":["Use the generic BitmapDecoder.Create(uri, createOptions, cacheOption) and let WIC auto-detect the format instead of a format-specific subclass.","Verify the file's actual format (magic bytes / extension) matches the decoder type you construct.","Re-export or convert the image so its container matches the decoder you are using.","Catch FileFormatException and fall back to generic BitmapDecoder.Create."],"exampleFix":"// before\nvar dec = new PngBitmapDecoder(new Uri(path), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); // throws if not PNG\n// after\nvar dec = BitmapDecoder.Create(new Uri(path), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);","handlingStrategy":"try-catch","validationCode":"static string SniffImageFormat(string path)\n{\n    var b = File.ReadAllBytes(path);\n    if (b.Length > 8 && b[0] == 0x89 && b[1] == (byte)'P') return \"png\";\n    if (b.Length > 3 && b[0] == 0xFF && b[1] == 0xD8) return \"jpeg\";\n    if (b.Length > 3 && b[0] == (byte)'G' && b[1] == (byte)'I' && b[2] == (byte)'F') return \"gif\";\n    return \"unknown\";\n}","typeGuard":"bool MatchesDecoder(Uri uri, Type decoderType) =>\n    SniffImageFormat(uri.IsFile ? uri.LocalPath : null) == decoderType.Name.Replace(\"BitmapDecoder\", \"\").ToLowerInvariant();","tryCatchPattern":"try { decoder = new PngBitmapDecoder(uri, opts, cache); }\ncatch (FileFormatException) { decoder = BitmapDecoder.Create(uri, opts, cache); }","preventionTips":["Prefer the generic BitmapDecoder.Create factory over format-specific subclasses","Validate magic bytes before choosing a decoder type","Never trust file extensions; verify actual content format","Normalize uploaded images through re-encoding on ingest"],"tags":["wpf","wic","image-format","uri","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"}