{"record":{"id":"fbeb79b560f825c7","repo":"dotnet/wpf","slug":"sr-image-initializationincomplete","errorCode":null,"errorMessage":"SR.Image_InitializationIncomplete","messagePattern":"SR\\.Image_InitializationIncomplete","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapInitialize.cs","lineNumber":65,"sourceCode":"        {\n            get\n            {\n                return _inInit;\n            }\n        }\n\n        public bool IsInitAtLeastOnce\n        {\n            get\n            {\n                return _isInitialized;\n            }\n        }\n\n        public void EnsureInitializedComplete()\n        {\n            if (IsInInit)\n                throw new InvalidOperationException(SR.Format(SR.Image_InitializationIncomplete, null));\n\n            if (!IsInitAtLeastOnce)\n                throw new InvalidOperationException(SR.Format(SR.Image_NotInitialized, null));\n        }\n\n        public void Reset()\n        {\n            _inInit = false;\n            _isInitialized = false;\n        }\n\n        private bool _inInit = false;\n        private bool _isInitialized = false;\n    }\n\n    #endregion\n}\n","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapInitialize.cs#L47-L83","documentation":"WPF throws this InvalidOperationException when BitmapImage/BitmapInitialize finalization is invoked while the bitmap is still inside its BeginInit/EndInit initialization window. The library tracks an _inInit flag; EndInit() clears it, and any API that requires a fully initialized bitmap calls EnsureInitializedComplete() first. Throwing here prevents use of a half-configured bitmap object whose underlying unmanaged resources were never created.","triggerScenarios":"Calling an API that internally invokes BitmapInitialize.EnsureInitializedComplete() while IsInInit is true — i.e., after BeginInit() but before EndInit() on a BitmapImage. Typical concrete calls: assigning/reading Source, accessing stream/frame data, cloning, or Freezing the bitmap inside the BeginInit/EndInit window.","commonSituations":"XAML data-binding or event handlers reading an Image.Source while the BitmapImage is still being initialized; code that calls BeginInit() but forgets EndInit() or returns early inside the window; async image loading where a callback touches the bitmap before EndInit runs.","solutions":["Ensure every BeginInit() is paired with EndInit() on the same BitmapImage instance, using try/finally so EndInit always runs.","Move any access to the bitmap (reading Source, freezing, cloning) to after EndInit() has returned.","If initialization is cancelled or fails, call the internal Reset path (or discard the BitmapImage and create a new one) instead of reusing it.","Wrap the consumer code in try/catch for InvalidOperationException and surface a clear message that the image was not fully initialized."],"exampleFix":"// before\nvar bmp = new BitmapImage();\nbmp.BeginInit();\nbmp.UriSource = uri;\nvar w = bmp.PixelWidth; // throws: still in init\n// after\nvar bmp = new BitmapImage();\nbmp.BeginInit();\nbmp.UriSource = uri;\nbmp.EndInit();\nbmp.Freeze();\nvar w = bmp.PixelWidth;","handlingStrategy":"validation","validationCode":"bool IsSafeToUse(BitmapImage bmp) => bmp != null && bmp.IsFrozen;","typeGuard":"static bool IsInitialized(BitmapImage bmp) => bmp != null && (bmp.IsFrozen || bmp.PixelWidth > 0);","tryCatchPattern":"try { UseBitmap(bmp); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"initialization\")) { /* defer use until after EndInit */ }","preventionTips":["Always pair BeginInit/EndInit in try/finally.","Freeze() the bitmap right after EndInit so late use is impossible to misorder.","Prefer the BitmapImage(Uri) constructor over manual init when no special options are needed.","Never touch the bitmap inside the BeginInit/EndInit window from event handlers or bindings."],"tags":["wpf","bitmapimage","invalidoperation","initialization","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"}