{"record":{"id":"0bccca71772df0d2","repo":"dotnet/wpf","slug":"sr-image-setpropertyoutsidebeginendinit","errorCode":null,"errorMessage":"SR.Image_SetPropertyOutsideBeginEndInit","messagePattern":"SR\\.Image_SetPropertyOutsideBeginEndInit","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapInitialize.cs","lineNumber":42,"sourceCode":"                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;\n            }\n        }\n\n        public bool IsInitAtLeastOnce\n        {\n            get\n            {\n                return _isInitialized;\n            }\n        }","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapInitialize.cs#L24-L60","documentation":"SetPrologue is the guard used by all property setters (UriSource, StreamSource, etc.) during initialization. It throws InvalidOperationException with SR.Image_SetPropertyOutsideBeginEndInit when a property is set while IsInInit is false, i.e. outside an active BeginInit/EndInit block.","triggerScenarios":"Setting BitmapImage properties like UriSource, StreamSource, CacheOption, or DecodePixelWidth without a preceding BeginInit(), or after EndInit() has already closed the init block.","commonSituations":"Assigning UriSource in code after the image was constructed via the parameterless constructor without BeginInit; updating properties on an already-initialized image to 'change' it; data-binding writing a property at a lifecycle point outside init.","solutions":["Wrap all property assignments between BeginInit() and EndInit()","Create a new BitmapImage and re-initialize it instead of mutating a completed one","Use the BitmapImage(Uri) constructor when only the URI needs to be set"],"exampleFix":"// before\nvar img = new BitmapImage();\nimg.UriSource = new Uri(path); // throws: outside init\n// after\nvar img = new BitmapImage();\nimg.BeginInit();\nimg.UriSource = new Uri(path);\nimg.CacheOption = BitmapCacheOption.OnLoad;\nimg.EndInit();","handlingStrategy":"validation","validationCode":"// ensure every property set happens inside init\nstatic BitmapImage LoadImage(Uri uri)\n{\n    var img = new BitmapImage();\n    img.BeginInit();          // open init block BEFORE any property set\n    img.UriSource = uri;\n    img.CacheOption = BitmapCacheOption.OnLoad;\n    img.EndInit();            // close init block\n    return img;\n}","typeGuard":null,"tryCatchPattern":"try { SetImageProperties(img); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"SetPropertyOutsideBeginEndInit\"))\n{\n    // properties set outside BeginInit/EndInit\n    logger.LogError(ex, \"BitmapImage property set outside init block\");\n}","preventionTips":["Always set properties between BeginInit() and EndInit()","Prefer the BitmapImage(Uri) constructor for the simple URI-only case","Never mutate properties on an already-initialized BitmapImage — create a new instance instead","Centralize BitmapImage construction in one factory helper to enforce the pattern"],"tags":["wpf","initialization","invalid-operation","property-setter"],"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"}