{"record":{"id":"d2374abf42d2ef67","repo":"dotnet/wpf","slug":"sr-image-noargument-croppedbitmap","errorCode":null,"errorMessage":"SR.Image_NoArgument","messagePattern":"SR\\.Image_NoArgument","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/CroppedBitmap.cs","lineNumber":154,"sourceCode":"        /// </summary>\n        private void SourcePropertyChangedHook(DependencyPropertyChangedEventArgs e)\n        {\n            if (!e.IsASubPropertyChange)\n            {\n                BitmapSource newSource = e.NewValue as BitmapSource;\n                _source = newSource;\n                RegisterDownloadEventSource(_source);\n                _syncObject = (newSource != null) ? newSource.SyncObject : _bitmapInit;\n            }\n        }\n\n        internal override bool IsValidForFinalizeCreation(bool throwIfInvalid)\n        {\n            if (Source == null)\n            {\n                if (throwIfInvalid)\n                {\n                    throw new InvalidOperationException(SR.Format(SR.Image_NoArgument, \"Source\"));\n                }\n                return false;\n            }\n\n            return true;\n        }\n\n        /// <summary>\n        ///     Notification on source rect changing.\n        /// </summary>\n        private void SourceRectPropertyChangedHook(DependencyPropertyChangedEventArgs e)\n        {\n            if (!e.IsASubPropertyChange)\n            {\n                _sourceRect = (Int32Rect)e.NewValue;\n            }\n        }\n","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/CroppedBitmap.cs#L136-L172","documentation":"CroppedBitmap crops a region of a Source bitmap; without a source there is nothing to crop. When EndInit finalizes creation and Source is null, IsValidForFinalizeCreation throws an InvalidOperationException (Image_NoArgument with \"Source\").","triggerScenarios":"new CroppedBitmap { SourceRect = new Int32Rect(...) } with BeginInit/EndInit but Source never set; setting only SourceRect in XAML; Source binding not yet resolved at EndInit.","commonSituations":"XAML binding to a bitmap loaded asynchronously; forgetting the Source line in code-built deferred init; copy-paste of SourceRect setup without the source assignment.","solutions":["Set Source to a loaded, non-null BitmapSource before EndInit","Ensure the source BitmapSource is fully initialized (its own EndInit completed) before assigning","For async loads, create the CroppedBitmap in the load-completed callback"],"exampleFix":"// before\nvar crop = new CroppedBitmap();\ncrop.BeginInit();\ncrop.SourceRect = new Int32Rect(0, 0, 100, 100);\ncrop.EndInit(); // throws\n// after\nvar crop = new CroppedBitmap();\ncrop.BeginInit();\ncrop.Source = new BitmapImage(new Uri(\"photo.png\"));\ncrop.SourceRect = new Int32Rect(0, 0, 100, 100);\ncrop.EndInit();","handlingStrategy":"validation","validationCode":"if (crop.Source == null) throw new ArgumentException(\"Source required\", nameof(source));\nif (crop.Source is BitmapImage bi && bi.IsDownloading) throw new InvalidOperationException(\"Wait for the source to finish loading\");","typeGuard":"static bool ReadyToCrop(BitmapSource s) => s != null && !((s as BitmapImage)?.IsDownloading ?? false);","tryCatchPattern":"try { crop.EndInit(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Source\")) { Log(\"CroppedBitmap created without Source\"); return fallbackCrop; }","preventionTips":["Always assign Source before SourceRect in deferred initialization","For async sources, build the CroppedBitmap inside the DownloadCompleted handler","Prefer the CroppedBitmap(source, sourceRect) constructor for synchronous code"],"tags":["wpf","imaging","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-22T01:17:13.364Z"}