dotnet/wpf · error · ArgumentException

SR.WpfPayload_InvalidImageSource

Error message

SR.WpfPayload_InvalidImageSource

What it means

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.

Solutions

  1. Ensure every Image has a valid Source before conversion, e.g. img.Source = new BitmapImage(new Uri(path)).
  2. Pre-check image.Source != null and !string.IsNullOrEmpty(image.Source.ToString()) before the conversion call.
  3. If images come from data binding, fix the bound property/path so it always yields a real ImageSource.
  4. Remove or replace placeholder Image elements that were never given a source.

Example fix

// before
var img = new Image(); // Source never set
payload.AddImage(img); // throws

// after
var img = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/img.png")) };
if (img.Source == null || string.IsNullOrEmpty(img.Source.ToString()))
    throw new InvalidOperationException("Image has no valid Source");
payload.AddImage(img);
Defensive patterns

Strategy: validation

Validate before calling

static bool HasUsableSource(System.Windows.Controls.Image img) =>
    img?.Source != null && !string.IsNullOrEmpty(img.Source.ToString());
// run before payload conversion: if (!HasUsableSource(img)) fix or skip;

Type guard

static bool IsValidImage(System.Windows.Controls.Image img) =>
    img is not null && img.Source is ImageSource s && !string.IsNullOrEmpty(s.ToString());

Try / catch

try
{
    payload.AddImage(image);
}
catch (ArgumentException ex)
{
    // log and skip/substitute a placeholder image
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/70d7f71b02dd3c88. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/WpfPayload.cs:489

                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());

            // Check whether we already have the image with the same BitmapFrame
            for (int i = 0; i < _images.Count; i++)
            {
                if (ImagesAreIdentical(GetBitmapSourceFromImage(_images[i]), GetBitmapSourceFromImage(image)))
                {

View on GitHub (pinned to 81131a70a4)