dotnet/wpf · error · InvalidOperationException

SR.Image_NotInitialized

Error message

SR.Image_NotInitialized

What it means

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.

Solutions

  1. Call BeginInit(), set properties, then EndInit() before consuming the BitmapImage.
  2. Alternatively use the BitmapImage(Uri) constructor, which performs initialization for you.
  3. Audit conditional initialization code so the consume path cannot run when the init path was skipped.
  4. Catch InvalidOperationException around first use and fall back to re-creating the bitmap properly initialized.

Example fix

// before
var bmp = new BitmapImage();
if (uri != null) { bmp.BeginInit(); bmp.UriSource = uri; bmp.EndInit(); }
var w = bmp.PixelWidth; // throws when uri == null: never initialized
// after
var bmp = uri != null ? new BitmapImage(uri) : null;
var w = bmp?.PixelWidth;
Defensive patterns

Strategy: validation

Validate before calling

if (bmp == null) throw new ArgumentNullException(nameof(bmp));
// ensure the init cycle ran before consuming
var safe = bmp.PixelWidth >= 0; // first touch only after BeginInit/EndInit

Type guard

static bool IsUsable(BitmapImage bmp) => bmp != null && bmp.PixelWidth > 0;

Try / catch

try { UseBitmap(bmp); }
catch (InvalidOperationException ex) when (ex.Message.Contains("initialized")) { bmp = RecreateBitmap(uri); }

Prevention

When it happens

Trigger: 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().

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/75e3d341569780b5. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapInitialize.cs:68

                return _inInit;
            }
        }

        public bool IsInitAtLeastOnce
        {
            get
            {
                return _isInitialized;
            }
        }

        public void EnsureInitializedComplete()
        {
            if (IsInInit)
                throw new InvalidOperationException(SR.Format(SR.Image_InitializationIncomplete, null));

            if (!IsInitAtLeastOnce)
                throw new InvalidOperationException(SR.Format(SR.Image_NotInitialized, null));
        }

        public void Reset()
        {
            _inInit = false;
            _isInitialized = false;
        }

        private bool _inInit = false;
        private bool _isInitialized = false;
    }

    #endregion
}

View on GitHub (pinned to 81131a70a4)