dotnet/wpf · error · InvalidOperationException
SR.Image_InitializationIncomplete
Error message
SR.Image_InitializationIncomplete
What it means
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.
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.
Example fix
// before var bmp = new BitmapImage(); bmp.BeginInit(); bmp.UriSource = uri; var w = bmp.PixelWidth; // throws: still in init // after var bmp = new BitmapImage(); bmp.BeginInit(); bmp.UriSource = uri; bmp.EndInit(); bmp.Freeze(); var w = bmp.PixelWidth;
Defensive patterns
Strategy: validation
Validate before calling
bool IsSafeToUse(BitmapImage bmp) => bmp != null && bmp.IsFrozen;
Type guard
static bool IsInitialized(BitmapImage bmp) => bmp != null && (bmp.IsFrozen || bmp.PixelWidth > 0);
Try / catch
try { UseBitmap(bmp); }
catch (InvalidOperationException ex) when (ex.Message.Contains("initialization")) { /* defer use until after EndInit */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
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
- SR.Image_NotInitialized
- Image_NeitherArgument (UriSource, StreamSource)
- SR.Image_EndInitWithoutBeginInit
- SR.Image_OnlyOneInit
- Cannot initialize compressor.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/fbeb79b560f825c7.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapInitialize.cs:65
{
get
{
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)