{"record":{"id":"e91758ac5f04053a","repo":"dotnet/wpf","slug":"value-must-be-of-type-bitmapsource","errorCode":null,"errorMessage":"'value' must be of type 'BitmapSource'.","messagePattern":"'value' must be of type 'BitmapSource'\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/ImageSourceTypeConverter.cs","lineNumber":162,"sourceCode":"            object                              value,\n            Type                                destinationType\n            )\n        {\n            Toolbox.EmitEvent(EventTrace.Event.WClientDRXConvertImageBegin);\n\n            ArgumentNullException.ThrowIfNull(context);\n            if (!IsSupportedType(destinationType))\n            {\n                throw new NotSupportedException(SR.Converter_ConvertToNotSupported);\n            }\n\n            //\n            // Check that we have a valid BitmapSource instance.\n            //\n            BitmapSource bitmapSource = (BitmapSource)value;\n            if (bitmapSource == null)\n            {\n                throw new ArgumentException(SR.Format(SR.MustBeOfType, \"value\", \"BitmapSource\"));\n            }\n\n            //\n            // Get the current serialization manager.\n            //\n            PackageSerializationManager manager = (PackageSerializationManager)context.GetService(typeof(XpsSerializationManager));\n\n            //Get the image Uri if it has already been serialized\n            Uri imageUri = GetBitmapSourceFromImageTable(manager, bitmapSource);\n\n            //\n            // Get the current page image cache\n            //\n            Dictionary<int, Uri> currentPageImageTable = manager.ResourcePolicy.CurrentPageImageTable;\n            if (imageUri != null)\n            {\n                int uriHashCode = imageUri.GetHashCode();\n                if(!currentPageImageTable.ContainsKey(uriHashCode))","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/ImageSourceTypeConverter.cs#L144-L180","documentation":"ImageSourceTypeConverter.ConvertTo casts the value parameter to BitmapSource; when the cast fails (value is not a BitmapSource and not null-castable) or the resulting reference is null, it throws ArgumentException with MustBeOfType(\"value\", \"BitmapSource\"). The converter only serializes actual BitmapSource instances into the XPS package.","triggerScenarios":"Passing a non-BitmapSource object (e.g. an Image control, a string URI, a Bitmap directly, or null) as the value argument of ConvertTo while destinationType is supported.","commonSituations":"Serializing System.Drawing.Bitmap or GDI+ images without converting to BitmapSource first; passing null for an image that failed to load; passing UIElement wrappers instead of the underlying image source.","solutions":["Ensure value is a System.Windows.Media.Imaging.BitmapSource before calling (convert GDI images with Imaging.CreateBitmapSourceFromHBitmap, files with BitmapFrame.Create).","Null-check the image before serialization and substitute an empty/placeholder BitmapSource.","Catch ArgumentException around ConvertTo and report which image value was invalid."],"exampleFix":"// before\nconverter.ConvertTo(context, culture, gdiBitmap, destinationType); // ArgumentException\n\n// after\nBitmapSource source = Imaging.CreateBitmapSourceFromHBitmap(\n    gdiBitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());\nconverter.ConvertTo(context, culture, source, destinationType);","handlingStrategy":"type-guard","validationCode":"if (value is not BitmapSource bitmapSource || bitmapSource == null)\n{\n    throw new ArgumentException($\"Expected BitmapSource, got {value?.GetType().Name ?? \"null\"}\");\n}","typeGuard":"static bool IsBitmapSource(object value) => value is BitmapSource b && b != null;\n\n// usage: if (!IsBitmapSource(value)) throw new ArgumentException(...);","tryCatchPattern":"try\n{\n    converter.ConvertTo(context, culture, value, destinationType);\n}\ncatch (ArgumentException ex) when (ex.ParamName == null && ex.Message.Contains(\"BitmapSource\"))\n{\n    // convert the offending value to a BitmapSource and retry\n}","preventionTips":["Convert GDI+ Bitmaps via Imaging.CreateBitmapSourceFromHBitmap before serialization.","Null-check images that may have failed to load.","Keep a single conversion helper so every image entering serialization is a frozen BitmapSource."],"tags":["wpf","xps","typeconverter","argument","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}