{"record":{"id":"83a1cff3d727d7a4","repo":"dotnet/wpf","slug":"image-neitherargument-urisource-streamsource","errorCode":null,"errorMessage":"Image_NeitherArgument (UriSource, StreamSource)","messagePattern":"Image_NeitherArgument \\(UriSource, StreamSource\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapImage.cs","lineNumber":74,"sourceCode":"        /// Prepare the bitmap to accept initialize paramters.\n        /// </summary>\n        public void BeginInit()\n        {\n            WritePreamble();\n            _bitmapInit.BeginInit();\n        }\n\n        /// <summary>\n        /// Prevent the bitmap from accepting any further initialize paramters.\n        /// </summary>\n        public void EndInit()\n        {\n            WritePreamble();\n            _bitmapInit.EndInit();\n\n            if (UriSource == null && StreamSource == null)\n            {\n                throw new InvalidOperationException(SR.Format(SR.Image_NeitherArgument, \"UriSource\", \"StreamSource\"));\n            }\n\n            // Capture the XPS package origin during the parse window so that\n            // the deferred FinalizeCreation path (DelayCreation) can enforce\n            // same-package containment even after ActivePackageUri is restored.\n            _xpsPackageOrigin = XpsLoadingContext.ActivePackageUri;\n\n            // If the Uri is relative, use delay creation as the BaseUri could be set at a later point\n            if (UriSource != null && !UriSource.IsAbsoluteUri && CacheOption != BitmapCacheOption.OnLoad)\n            {\n                DelayCreation = true;\n            }\n\n            if (!DelayCreation && !CreationCompleted)\n                FinalizeCreation();\n        }\n\n        #endregion","sourceCodeStart":56,"sourceCodeEnd":92,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapImage.cs#L56-L92","documentation":"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.","triggerScenarios":"Completing a BitmapImage initialization (EndInit or the ISupportInitialize pattern / XAML element) without ever setting UriSource or StreamSource.","commonSituations":"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.","solutions":["Set either UriSource or StreamSource between BeginInit and EndInit.","In XAML, ensure the Image Source binding resolves to a non-null, valid pack/absolute URI.","Guard code-behind: only call EndInit when a source was assigned.","For dynamic images, create the BitmapImage lazily only when the URI/stream is available."],"exampleFix":"// before\nvar img = new BitmapImage();\nimg.BeginInit();\n// forgot img.UriSource\nimg.EndInit(); // InvalidOperationException\n\n// after\nvar img = new BitmapImage();\nimg.BeginInit();\nimg.UriSource = new Uri(path, UriKind.RelativeOrAbsolute);\nimg.EndInit();","handlingStrategy":"validation","validationCode":"if (bitmapImage.UriSource == null && bitmapImage.StreamSource == null)\n    throw new InvalidOperationException(\"Set UriSource or StreamSource before EndInit.\");","typeGuard":"bool isInitialized(BitmapImage img) => img.UriSource != null || img.StreamSource != null;","tryCatchPattern":"try { img.EndInit(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"UriSource\")) { img.UriSource = DefaultPlaceholderUri; img.EndInit(); }","preventionTips":["Always set UriSource/StreamSource between BeginInit and EndInit.","Check XAML bindings that produce null Source values.","Create BitmapImage lazily only when a real source exists."],"tags":["wpf","imaging","bitmapimage","initialization"],"backgroundTag":"missing-required-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"}