dotnet/wpf · error · ArgumentNullException
image.Source
Error message
image.Source
What it means
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.
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.
Example fix
// before
var image = new Image { Width = 100 }; // Source never set
wpfPayload.AddImage(image); // throws
// after
if (image.Source == null || string.IsNullOrEmpty(image.Source.ToString()))
return null; // skip placeholder image
wpfPayload.AddImage(image); Defensive patterns
Strategy: validation
Validate before calling
if (image?.Source == null || string.IsNullOrEmpty(image.Source.ToString()))
return null; // cannot serialize an image with no source Type guard
static bool HasUsableSource(Image image) =>
image?.Source != null && !string.IsNullOrEmpty(image.Source.ToString()); Try / catch
try { wpfPayload.AddImage(image); }
catch (ArgumentNullException)
call image = PlaceholderImage(); /* or skip */ } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- SR.WpfPayload_InvalidImageSource
- ' ' ContentType is not valid.
- annotation component
- ArgumentNullException
- ArgumentNullException(argName)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/2644dbd15cb4190f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/WpfPayload.cs:484
// Save encoded image data into the image part in the package
Stream imageStream = imagePart.GetSeekableStream();
using (imageStream)
{
bitmapEncoder.Save(imageStream);
}
}
// Adds an image data to the package.
// Returns a local Uri that must be used to access this data
// from the package - from its top level directory.
internal string AddImage(Image image)
{
ArgumentNullException.ThrowIfNull(image);
if (image.Source == null)
{
throw new ArgumentNullException("image.Source");
}
if (string.IsNullOrEmpty(image.Source.ToString()))
{
throw new ArgumentException(SR.WpfPayload_InvalidImageSource);
}
if (_images == null)
{
_images = new List<Image>();
}
// Define the image uri for the new image
string imagePartUriString = null;
// Define image type
string imageContentType = GetImageContentType(image.Source.ToString());
View on GitHub (pinned to 81131a70a4)