dotnet/wpf · error · InvalidOperationException
SR.Image_InInitialize
Error message
SR.Image_InInitialize
What it means
BeginInit sets the _inInit flag; calling BeginInit again while an initialization block is already open means a previous BeginInit was never paired with EndInit. WPF throws InvalidOperationException with SR.Image_InInitialize to prevent nested initialization.
Solutions
- Remove the redundant BeginInit call — only call it once before setting properties
- Ensure EndInit is called in a finally block so a failed init resets state, and create a new instance for retries
- Refactor helpers to accept an already-initializing object instead of calling BeginInit themselves
Example fix
// before img.BeginInit(); Configure(img); // helper also calls img.BeginInit() -> throws img.EndInit(); // after img.BeginInit(); Configure(img); // helper only sets properties (UriSource, etc.) img.EndInit();
Defensive patterns
Strategy: validation
Validate before calling
bool canEndInitSafely(BitmapImage img, ref bool begun) => begun == false; // call BeginInit only when no init block is open
Try / catch
try { img.BeginInit(); /* props */; img.EndInit(); }
catch (InvalidOperationException ex)
{
// nested init: init block already open
logger.LogError(ex, "Nested BeginInit on BitmapImage");
} Prevention
- Call BeginInit exactly once per init block, never inside helper methods that the caller also uses within the block
- Ensure helpers only set properties, not BeginInit
- Use try/finally so EndInit always runs after BeginInit, preventing leaked init state
When it happens
Trigger: Calling BeginInit() while IsInInit is true — i.e. a second BeginInit before the matching EndInit, typically from a helper method called inside an already-open BeginInit/EndInit block.
Common situations: A shared helper that calls BeginInit itself while the caller already called BeginInit; exception in the middle of init skipped EndInit, then retry logic calls BeginInit again; event handlers or async callbacks re-entering init code.
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.EndInitWithoutBeginInitNotSupported
- SR.Format(SR.PropertyMustHaveValue, "DataType"…
- SR.Illegal_InheritanceBehaviorSettor
- SR.Image_EndInitWithoutBeginInit
- SR.Image_OnlyOneInit
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/c9b0ae85707e4b06.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapInitialize.cs:24
namespace System.Windows.Media.Imaging
{
#region BitmapInitialize
/// <summary>
/// Utility class providing support for ISupportInitialize
/// </summary>
internal class BitmapInitialize : ISupportInitialize
{
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));View on GitHub (pinned to 81131a70a4)