{"record":{"id":"2644dbd15cb4190f","repo":"dotnet/wpf","slug":"image-source","errorCode":null,"errorMessage":"image.Source","messagePattern":"image\\.Source","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/WpfPayload.cs","lineNumber":484,"sourceCode":"\n            // Save encoded image data into the image part in the package\n            Stream imageStream = imagePart.GetSeekableStream();\n            using (imageStream)\n            {\n                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","sourceCodeStart":466,"sourceCodeEnd":502,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/WpfPayload.cs#L466-L502","documentation":"WpfPayload.AddImage throws ArgumentNullException named \"image.Source\" when the Image control handed to the payload has no Source set. A WPF payload image part needs an actual image source (URI/bitmap) to serialize into the package, so a Source-less Image is rejected.","triggerScenarios":"Constructing a new Image() (or one whose ImageSource failed to load, e.g. broken URI or failed download) and passing it to WpfPayload.AddImage while building an XAML/RTF payload for clipboard or document content.","commonSituations":"Images whose source URI 404'd or failed decode leaving Source null; dynamically created Image elements where the source is assigned asynchronously after AddImage runs; copy/paste code paths iterating a document containing placeholder images.","solutions":["Set image.Source (e.g. new BitmapImage(new Uri(...))) before calling AddImage, and verify the URI resolves so Source is non-null.","Skip or placeholder images with null Source instead of adding them to the payload.","If Source loads asynchronously, wait for DownloadCompleted/DecodeFailed (or use the decoded BitmapSource directly) before adding.","Catch ArgumentNullException/ArgumentException and remove the offending image from the payload, logging a warning."],"exampleFix":"// before\nvar image = new Image { Width = 100 }; // Source never set\nwpfPayload.AddImage(image); // throws\n// after\nif (image.Source == null || string.IsNullOrEmpty(image.Source.ToString()))\n    return null; // skip placeholder image\nwpfPayload.AddImage(image);","handlingStrategy":"validation","validationCode":"if (image?.Source == null || string.IsNullOrEmpty(image.Source.ToString()))\n    return null; // cannot serialize an image with no source","typeGuard":"static bool HasUsableSource(Image image) =>\n    image?.Source != null && !string.IsNullOrEmpty(image.Source.ToString());","tryCatchPattern":"try { wpfPayload.AddImage(image); }\ncatch (ArgumentNullException)\ncall image = PlaceholderImage(); /* or skip */ }","preventionTips":["Only pass Image elements whose Source is assigned and successfully decoded","Await async image loads (DownloadCompleted) before payload creation","Skip placeholder images instead of adding them to the payload","Use a valid, reachable URI or decoded BitmapSource — not a dangling reference"],"tags":["wpf","image","clipboard","null-argument"],"backgroundTag":"null-argument","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"}