dotnet/wpf · error · InvalidOperationException
SR.Image_SetPropertyOutsideBeginEndInit
Error message
SR.Image_SetPropertyOutsideBeginEndInit
What it means
SetPrologue is the guard used by all property setters (UriSource, StreamSource, etc.) during initialization. It throws InvalidOperationException with SR.Image_SetPropertyOutsideBeginEndInit when a property is set while IsInInit is false, i.e. outside an active BeginInit/EndInit block.
Solutions
- Wrap all property assignments between BeginInit() and EndInit()
- Create a new BitmapImage and re-initialize it instead of mutating a completed one
- Use the BitmapImage(Uri) constructor when only the URI needs to be set
Example fix
// before var img = new BitmapImage(); img.UriSource = new Uri(path); // throws: outside init // after var img = new BitmapImage(); img.BeginInit(); img.UriSource = new Uri(path); img.CacheOption = BitmapCacheOption.OnLoad; img.EndInit();
Defensive patterns
Strategy: validation
Validate before calling
// ensure every property set happens inside init
static BitmapImage LoadImage(Uri uri)
{
var img = new BitmapImage();
img.BeginInit(); // open init block BEFORE any property set
img.UriSource = uri;
img.CacheOption = BitmapCacheOption.OnLoad;
img.EndInit(); // close init block
return img;
} Try / catch
try { SetImageProperties(img); }
catch (InvalidOperationException ex) when (ex.Message.Contains("SetPropertyOutsideBeginEndInit"))
{
// properties set outside BeginInit/EndInit
logger.LogError(ex, "BitmapImage property set outside init block");
} Prevention
- Always set properties between BeginInit() and EndInit()
- Prefer the BitmapImage(Uri) constructor for the simple URI-only case
- Never mutate properties on an already-initialized BitmapImage — create a new instance instead
- Centralize BitmapImage construction in one factory helper to enforce the pattern
When it happens
Trigger: Setting BitmapImage properties like UriSource, StreamSource, CacheOption, or DecodePixelWidth without a preceding BeginInit(), or after EndInit() has already closed the init block.
Common situations: Assigning UriSource in code after the image was constructed via the parameterless constructor without BeginInit; updating properties on an already-initialized image to 'change' it; data-binding writing a property at a lifecycle point outside init.
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_InInitialize
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/0bccca71772df0d2.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapInitialize.cs:42
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;
}
}
public bool IsInitAtLeastOnce
{
get
{
return _isInitialized;
}
}View on GitHub (pinned to 81131a70a4)