{"record":{"id":"54643a7d6c82c36a","repo":"dotnet/wpf","slug":"sr-image-endinitwithoutbegininit","errorCode":null,"errorMessage":"SR.Image_EndInitWithoutBeginInit","messagePattern":"SR\\.Image_EndInitWithoutBeginInit","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapInitialize.cs","lineNumber":32,"sourceCode":"        public BitmapInitialize()\n        {\n        }\n\n        public void BeginInit()\n        {\n            if (IsInitAtLeastOnce)\n                throw new InvalidOperationException(SR.Format(SR.Image_OnlyOneInit, null));\n\n            if (IsInInit)\n                throw new InvalidOperationException(SR.Format(SR.Image_InInitialize, null));\n\n            _inInit = true;\n        }\n\n        public void EndInit()\n        {\n            if (!IsInInit)\n                throw new InvalidOperationException(SR.Format(SR.Image_EndInitWithoutBeginInit, null));\n\n            _inInit = false;\n            _isInitialized = true;\n        }\n\n        public void SetPrologue()\n        {\n            if (!IsInInit)\n            {\n                throw new InvalidOperationException(SR.Format(SR.Image_SetPropertyOutsideBeginEndInit, null));\n            }\n        }\n\n        public bool IsInInit\n        {\n            get\n            {\n                return _inInit;","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapInitialize.cs#L14-L50","documentation":"EndInit finalizes initialization and may only run inside an open BeginInit/EndInit pair. If EndInit is called without a prior BeginInit (IsInInit == false), WPF throws InvalidOperationException with SR.Image_EndInitWithoutBeginInit.","triggerScenarios":"Calling EndInit() on a BitmapImage that never had BeginInit called, or calling it twice after the first EndInit already reset _inInit, or after construction via constructor instead of the ISupportInitialize pattern.","commonSituations":"Mixing the BitmapImage(uri) constructor with a stray EndInit call; defensive cleanup code that calls EndInit in a finally block even when init was never started; XAML loader path combined with manual init calls.","solutions":["Always pair EndInit with a preceding BeginInit in the same scope","Guard EndInit with a check of the init state, or track it with your own flag before calling","If you construct via constructor, do not call BeginInit/EndInit at all — set properties and use the image directly"],"exampleFix":"// before\nvar img = new BitmapImage();\nimg.EndInit(); // throws: no BeginInit\n// after\nvar img = new BitmapImage();\nimg.BeginInit();\nimg.UriSource = new Uri(path);\nimg.EndInit();","handlingStrategy":"validation","validationCode":"bool canEndInit(BitmapImage img, bool beginWasCalled) => beginWasCalled; // only EndInit after your own BeginInit","typeGuard":null,"tryCatchPattern":"bool begun = false;\ntry { if (begun) { img.EndInit(); begun = false; } }\ncatch (InvalidOperationException) { /* EndInit without BeginInit */ }","preventionTips":["Always write BeginInit/EndInit as a matched pair in the same method","Never call EndInit in a finally block unless BeginInit succeeded (track with a flag)","Avoid mixing the BitmapImage(Uri) constructor with manual BeginInit/EndInit calls"],"tags":["wpf","initialization","invalid-operation","lifecycle"],"backgroundTag":"invalid-state-transition","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"}