dotnet/wpf · error · InvalidOperationException
SR.Image_OnlyOneInit
Error message
SR.Image_OnlyOneInit
What it means
ISupportInitialize.BeginInit on BitmapCacheFile/BitmapInitialize types may only be called once per object; WPF deliberately forbids re-initialization. Calling BeginInit after the object has already been initialized throws InvalidOperationException with SR.Image_OnlyOneInit.
Solutions
- Create a new BitmapImage instance for each image instead of re-initializing an existing one
- Track initialization with a bool flag so BeginInit is only invoked once per instance
- If the URI changes, assign a fresh object or use the Freezable Clone pattern instead of re-running BeginInit
Example fix
// before
if (_bitmap == null) { _bitmap = new BitmapImage(); _bitmap.BeginInit(); }
_bitmap.BeginInit(); // throws on reuse
// after
_bitmap = new BitmapImage();
_bitmap.BeginInit();
_bitmap.UriSource = new Uri(path);
_bitmap.EndInit(); Defensive patterns
Strategy: validation
Validate before calling
bool canBeginInit(BitmapImage img) => img is { } && !_initializedImages.Contains(img); // track instances you already initialized
class InitTracker { private static readonly HashSet<BitmapImage> Done = new(); public static bool IsNew(BitmapImage i) => Done.Add(i); } Type guard
bool IsReusableImage(BitmapImage img) => img == null; // BitmapImage is single-init: treat as non-reusable, always create new
Prevention
- Treat BitmapImage as single-use: create a new instance per image load
- Never share one BitmapImage across multiple loads or data items
- Centralize image creation in a factory method so BeginInit is called exactly once
When it happens
Trigger: Calling BeginInit() a second time on the same BitmapImage (or other IBitmapInitialize implementer) after a prior BeginInit/EndInit cycle has completed (IsInitAtLeastOnce == true).
Common situations: Reusing a cached BitmapImage instance to load a new URI; loops that reconfigure one shared image object per item; copy-pasted init code running twice for the same instance.
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_EndInitWithoutBeginInit
- 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/4fbafe555180b826.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapInitialize.cs:21
using System.ComponentModel;
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()
{View on GitHub (pinned to 81131a70a4)