dotnet/wpf · error · InvalidOperationException
SR.Image_NoArgument
Error message
SR.Image_NoArgument
What it means
CroppedBitmap crops a region of a Source bitmap; without a source there is nothing to crop. When EndInit finalizes creation and Source is null, IsValidForFinalizeCreation throws an InvalidOperationException (Image_NoArgument with "Source").
Solutions
- Set Source to a loaded, non-null BitmapSource before EndInit
- Ensure the source BitmapSource is fully initialized (its own EndInit completed) before assigning
- For async loads, create the CroppedBitmap in the load-completed callback
Example fix
// before
var crop = new CroppedBitmap();
crop.BeginInit();
crop.SourceRect = new Int32Rect(0, 0, 100, 100);
crop.EndInit(); // throws
// after
var crop = new CroppedBitmap();
crop.BeginInit();
crop.Source = new BitmapImage(new Uri("photo.png"));
crop.SourceRect = new Int32Rect(0, 0, 100, 100);
crop.EndInit(); Defensive patterns
Strategy: validation
Validate before calling
if (crop.Source == null) throw new ArgumentException("Source required", nameof(source));
if (crop.Source is BitmapImage bi && bi.IsDownloading) throw new InvalidOperationException("Wait for the source to finish loading"); Type guard
static bool ReadyToCrop(BitmapSource s) => s != null && !((s as BitmapImage)?.IsDownloading ?? false);
Try / catch
try { crop.EndInit(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Source")) { Log("CroppedBitmap created without Source"); return fallbackCrop; } Prevention
- Always assign Source before SourceRect in deferred initialization
- For async sources, build the CroppedBitmap inside the DownloadCompleted handler
- Prefer the CroppedBitmap(source, sourceRect) constructor for synchronous code
When it happens
Trigger: new CroppedBitmap { SourceRect = new Int32Rect(...) } with BeginInit/EndInit but Source never set; setting only SourceRect in XAML; Source binding not yet resolved at EndInit.
Common situations: XAML binding to a bitmap loaded asynchronously; forgetting the Source line in code-built deferred init; copy-paste of SourceRect setup without the source assignment.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- Image_NeitherArgument (UriSource, StreamSource)
- SR.Image_NoArgument
- SR.Image_NoArgument
- Cannot initialize compressor.
- Image_EncoderNoColorContext
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/d2374abf42d2ef67.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/CroppedBitmap.cs:154
/// </summary>
private void SourcePropertyChangedHook(DependencyPropertyChangedEventArgs e)
{
if (!e.IsASubPropertyChange)
{
BitmapSource newSource = e.NewValue as BitmapSource;
_source = newSource;
RegisterDownloadEventSource(_source);
_syncObject = (newSource != null) ? newSource.SyncObject : _bitmapInit;
}
}
internal override bool IsValidForFinalizeCreation(bool throwIfInvalid)
{
if (Source == null)
{
if (throwIfInvalid)
{
throw new InvalidOperationException(SR.Format(SR.Image_NoArgument, "Source"));
}
return false;
}
return true;
}
/// <summary>
/// Notification on source rect changing.
/// </summary>
private void SourceRectPropertyChangedHook(DependencyPropertyChangedEventArgs e)
{
if (!e.IsASubPropertyChange)
{
_sourceRect = (Int32Rect)e.NewValue;
}
}
View on GitHub (pinned to 81131a70a4)