dotnet/wpf · error · InvalidOperationException

Image_NeitherArgument (UriSource, StreamSource)

Error message

Image_NeitherArgument (UriSource, StreamSource)

What it means

BitmapImage.EndInit validates that exactly one of UriSource or StreamSource was set; if both are null it throws InvalidOperationException with SR.Image_NeitherArgument naming "UriSource" and "StreamSource". XAML/parse-time initialization must supply one image source.

Solutions

  1. Set either UriSource or StreamSource between BeginInit and EndInit.
  2. In XAML, ensure the Image Source binding resolves to a non-null, valid pack/absolute URI.
  3. Guard code-behind: only call EndInit when a source was assigned.
  4. For dynamic images, create the BitmapImage lazily only when the URI/stream is available.

Example fix

// before
var img = new BitmapImage();
img.BeginInit();
// forgot img.UriSource
img.EndInit(); // InvalidOperationException

// after
var img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(path, UriKind.RelativeOrAbsolute);
img.EndInit();
Defensive patterns

Strategy: validation

Validate before calling

if (bitmapImage.UriSource == null && bitmapImage.StreamSource == null)
    throw new InvalidOperationException("Set UriSource or StreamSource before EndInit.");

Type guard

bool isInitialized(BitmapImage img) => img.UriSource != null || img.StreamSource != null;

Try / catch

try { img.EndInit(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("UriSource")) { img.UriSource = DefaultPlaceholderUri; img.EndInit(); }

Prevention

When it happens

Trigger: Completing a BitmapImage initialization (EndInit or the ISupportInitialize pattern / XAML element) without ever setting UriSource or StreamSource.

Common situations: A XAML <Image Source='{Binding ...}'> where the binding fails before parse completes leaving BitmapImage sourceless; code-behind doing new BitmapImage() + BeginInit() but the property assignment is skipped or an empty binding yields null; data binding supplying an empty string for Source.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapImage.cs:74

        /// Prepare the bitmap to accept initialize paramters.
        /// </summary>
        public void BeginInit()
        {
            WritePreamble();
            _bitmapInit.BeginInit();
        }

        /// <summary>
        /// Prevent the bitmap from accepting any further initialize paramters.
        /// </summary>
        public void EndInit()
        {
            WritePreamble();
            _bitmapInit.EndInit();

            if (UriSource == null && StreamSource == null)
            {
                throw new InvalidOperationException(SR.Format(SR.Image_NeitherArgument, "UriSource", "StreamSource"));
            }

            // Capture the XPS package origin during the parse window so that
            // the deferred FinalizeCreation path (DelayCreation) can enforce
            // same-package containment even after ActivePackageUri is restored.
            _xpsPackageOrigin = XpsLoadingContext.ActivePackageUri;

            // If the Uri is relative, use delay creation as the BaseUri could be set at a later point
            if (UriSource != null && !UriSource.IsAbsoluteUri && CacheOption != BitmapCacheOption.OnLoad)
            {
                DelayCreation = true;
            }

            if (!DelayCreation && !CreationCompleted)
                FinalizeCreation();
        }

        #endregion

View on GitHub (pinned to 81131a70a4)