{"record":{"id":"70d7f71b02dd3c88","repo":"dotnet/wpf","slug":"sr-wpfpayload-invalidimagesource","errorCode":null,"errorMessage":"SR.WpfPayload_InvalidImageSource","messagePattern":"SR\\.WpfPayload_InvalidImageSource","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/WpfPayload.cs","lineNumber":489,"sourceCode":"                bitmapEncoder.Save(imageStream);\n            }\n        }\n\n        // Adds an image data to the package.\n        // Returns a local Uri that must be used to access this data\n        // from the package - from its top level directory.\n        internal string AddImage(Image image)\n        {\n            ArgumentNullException.ThrowIfNull(image);\n\n            if (image.Source == null)\n            {\n                throw new ArgumentNullException(\"image.Source\");\n            }\n\n            if (string.IsNullOrEmpty(image.Source.ToString()))\n            {\n                throw new ArgumentException(SR.WpfPayload_InvalidImageSource);\n            }\n\n            if (_images == null)\n            {\n                _images = new List<Image>();\n            }\n\n            // Define the image uri for the new image\n            string imagePartUriString = null;\n\n            // Define image type\n            string imageContentType = GetImageContentType(image.Source.ToString());\n\n            // Check whether we already have the image with the same BitmapFrame\n            for (int i = 0; i < _images.Count; i++)\n            {\n                if (ImagesAreIdentical(GetBitmapSourceFromImage(_images[i]), GetBitmapSourceFromImage(image)))\n                {","sourceCodeStart":471,"sourceCodeEnd":507,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/WpfPayload.cs#L471-L507","documentation":"WpfPayload.AddImage throws ArgumentException (SR.WpfPayload_InvalidImageSource) while serializing a WPF Image element into an XPS/clipboard payload. After checking the Image and its Source are non-null, it verifies that image.Source.ToString() yields a non-empty string; if not, the image has no usable source and cannot be written as a package image part.","triggerScenarios":"Calling the WPF payload conversion (document copy/save to XAML/HTML/XPS) with a System.Windows.Controls.Image whose Source property is set to an object whose ToString() returns an empty string, or is otherwise an invalid/unresolved ImageSource (WpfPayload.cs:489).","commonSituations":"An Image declared in XAML with Source omitted or failing to resolve; a programmatically constructed Image where Source was never assigned; a data binding that produced an empty/unset value; an image that failed to decode leaving a degenerate ImageSource.","solutions":["Ensure every Image has a valid Source before conversion, e.g. img.Source = new BitmapImage(new Uri(path)).","Pre-check image.Source != null and !string.IsNullOrEmpty(image.Source.ToString()) before the conversion call.","If images come from data binding, fix the bound property/path so it always yields a real ImageSource.","Remove or replace placeholder Image elements that were never given a source."],"exampleFix":"// before\nvar img = new Image(); // Source never set\npayload.AddImage(img); // throws\n\n// after\nvar img = new Image { Source = new BitmapImage(new Uri(\"pack://application:,,,/img.png\")) };\nif (img.Source == null || string.IsNullOrEmpty(img.Source.ToString()))\n    throw new InvalidOperationException(\"Image has no valid Source\");\npayload.AddImage(img);","handlingStrategy":"validation","validationCode":"static bool HasUsableSource(System.Windows.Controls.Image img) =>\n    img?.Source != null && !string.IsNullOrEmpty(img.Source.ToString());\n// run before payload conversion: if (!HasUsableSource(img)) fix or skip;","typeGuard":"static bool IsValidImage(System.Windows.Controls.Image img) =>\n    img is not null && img.Source is ImageSource s && !string.IsNullOrEmpty(s.ToString());","tryCatchPattern":"try\n{\n    payload.AddImage(image);\n}\ncatch (ArgumentException ex)\n{\n    // log and skip/substitute a placeholder image\n}","preventionTips":["Never add an Image to a document without assigning Source","Check Source and Source.ToString() in tests covering all document imagery","Guard data-bound Image sources with a fallback converter","Validate documents before clipboard/save conversion"],"tags":["wpf","xaml","image","invalid-argument","clipboard"],"backgroundTag":"invalid-argument-value","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"}