dotnet/wpf · error · InvalidOperationException
SR.Image_EndInitWithoutBeginInit
Error message
SR.Image_EndInitWithoutBeginInit
What it means
EndInit finalizes initialization and may only run inside an open BeginInit/EndInit pair. If EndInit is called without a prior BeginInit (IsInInit == false), WPF throws InvalidOperationException with SR.Image_EndInitWithoutBeginInit.
Solutions
- Always pair EndInit with a preceding BeginInit in the same scope
- Guard EndInit with a check of the init state, or track it with your own flag before calling
- If you construct via constructor, do not call BeginInit/EndInit at all — set properties and use the image directly
Example fix
// before var img = new BitmapImage(); img.EndInit(); // throws: no BeginInit // after var img = new BitmapImage(); img.BeginInit(); img.UriSource = new Uri(path); img.EndInit();
Defensive patterns
Strategy: validation
Validate before calling
bool canEndInit(BitmapImage img, bool beginWasCalled) => beginWasCalled; // only EndInit after your own BeginInit
Try / catch
bool begun = false;
try { if (begun) { img.EndInit(); begun = false; } }
catch (InvalidOperationException) { /* EndInit without BeginInit */ } Prevention
- Always write BeginInit/EndInit as a matched pair in the same method
- Never call EndInit in a finally block unless BeginInit succeeded (track with a flag)
- Avoid mixing the BitmapImage(Uri) constructor with manual BeginInit/EndInit calls
When it happens
Trigger: Calling EndInit() on a BitmapImage that never had BeginInit called, or calling it twice after the first EndInit already reset _inInit, or after construction via constructor instead of the ISupportInitialize pattern.
Common situations: Mixing the BitmapImage(uri) constructor with a stray EndInit call; defensive cleanup code that calls EndInit in a finally block even when init was never started; XAML loader path combined with manual init calls.
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_OnlyOneInit
- SR.EndInitWithoutBeginInitNotSupported
- SR.Format(SR.PropertyMustHaveValue, "DataType"…
- SR.Illegal_InheritanceBehaviorSettor
- SR.Image_InInitialize
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/54643a7d6c82c36a.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapInitialize.cs:32
public BitmapInitialize()
{
}
public void BeginInit()
{
if (IsInitAtLeastOnce)
throw new InvalidOperationException(SR.Format(SR.Image_OnlyOneInit, null));
if (IsInInit)
throw new InvalidOperationException(SR.Format(SR.Image_InInitialize, null));
_inInit = true;
}
public void EndInit()
{
if (!IsInInit)
throw new InvalidOperationException(SR.Format(SR.Image_EndInitWithoutBeginInit, null));
_inInit = false;
_isInitialized = true;
}
public void SetPrologue()
{
if (!IsInInit)
{
throw new InvalidOperationException(SR.Format(SR.Image_SetPropertyOutsideBeginEndInit, null));
}
}
public bool IsInInit
{
get
{
return _inInit;View on GitHub (pinned to 81131a70a4)