{"record":{"id":"75e3d341569780b5","repo":"dotnet/wpf","slug":"sr-image-notinitialized","errorCode":null,"errorMessage":"SR.Image_NotInitialized","messagePattern":"SR\\.Image_NotInitialized","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapInitialize.cs","lineNumber":68,"sourceCode":"                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":50,"sourceCodeEnd":83,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapInitialize.cs#L50-L83","documentation":"This error means BitmapInitialize.EnsureInitializedComplete() found IsInitAtLeastOnce == false: neither BeginInit() nor EndInit() was ever called on the bitmap, so the object was never configured at all. WPF requires bitmaps with deferred initialization to go through the BeginInit/EndInit cycle before being used; using a brand-new, never-initialized BitmapImage triggers this throw.","triggerScenarios":"Invoking any consumer API that calls EnsureInitializedComplete() on a BitmapImage on which BeginInit()/EndInit() were never invoked (IsInitAtLeastOnce false) — e.g., reading PixelWidth/Source, or using the bitmap in an Image control where the initialization check runs, without ever calling BeginInit().","commonSituations":"Constructing a BitmapImage with new BitmapImage() and immediately setting properties like PixelWidth manually or consuming it without the init cycle; code paths that conditionally skip BeginInit (e.g., only initializing when a URI is non-null) but unconditionally consume the result.","solutions":["Call BeginInit(), set properties, then EndInit() before consuming the BitmapImage.","Alternatively use the BitmapImage(Uri) constructor, which performs initialization for you.","Audit conditional initialization code so the consume path cannot run when the init path was skipped.","Catch InvalidOperationException around first use and fall back to re-creating the bitmap properly initialized."],"exampleFix":"// before\nvar bmp = new BitmapImage();\nif (uri != null) { bmp.BeginInit(); bmp.UriSource = uri; bmp.EndInit(); }\nvar w = bmp.PixelWidth; // throws when uri == null: never initialized\n// after\nvar bmp = uri != null ? new BitmapImage(uri) : null;\nvar w = bmp?.PixelWidth;","handlingStrategy":"validation","validationCode":"if (bmp == null) throw new ArgumentNullException(nameof(bmp));\n// ensure the init cycle ran before consuming\nvar safe = bmp.PixelWidth >= 0; // first touch only after BeginInit/EndInit","typeGuard":"static bool IsUsable(BitmapImage bmp) => bmp != null && bmp.PixelWidth > 0;","tryCatchPattern":"try { UseBitmap(bmp); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"initialized\")) { bmp = RecreateBitmap(uri); }","preventionTips":["Use the BitmapImage(Uri) constructor instead of BeginInit/EndInit when possible.","Keep init and consumption in the same method so one cannot run without the other.","Never branch around BeginInit without also branching the consumer.","Add a debug assert that EndInit ran before any bitmap property read."],"tags":["wpf","bitmapimage","invalidoperation","uninitialized","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-22T01:17:13.364Z"}