dotnet/wpf · error · InvalidOperationException
SR.Image_NoArgument
Error message
SR.Image_NoArgument
What it means
Like the other derived bitmaps, FormatConvertedBitmap requires a Source to convert. If EndInit runs while Source is null, IsValidForFinalizeCreation throws an InvalidOperationException (Image_NoArgument with "Source") — a format conversion cannot be finalized without an input image.
Solutions
- Assign a valid BitmapSource to Source before calling EndInit
- When using the convenience constructor new FormatConvertedBitmap(source, format, palette, alphaThreshold), Source is set automatically — prefer it
- Create the FormatConvertedBitmap after the source BitmapImage has completed loading (e.g. in DownloadCompleted handler)
Example fix
// before
var fcb = new FormatConvertedBitmap();
fcb.BeginInit();
fcb.DestinationFormat = PixelFormats.Gray8;
fcb.EndInit(); // throws
// after
var fcb = new FormatConvertedBitmap();
fcb.BeginInit();
fcb.Source = new BitmapImage(new Uri("photo.png"));
fcb.DestinationFormat = PixelFormats.Gray8;
fcb.EndInit(); Defensive patterns
Strategy: validation
Validate before calling
if (fcb.Source == null) throw new ArgumentException("Source required before EndInit", nameof(source)); Type guard
static bool ReadyToConvert(FormatConvertedBitmap f) => f.Source != null && f.DestinationFormat != PixelFormats.Default;
Try / catch
try { fcb.EndInit(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Source")) { fcb.Source = placeholderBitmap; fcb.EndInit(); } Prevention
- Prefer the full-argument constructor FormatConvertedBitmap(source, format, palette, alphaThreshold)
- Complete source BitmapImage loading before building derived bitmaps
- In XAML, bind Source with Completed handling or use the Loaded event to construct derived bitmaps
When it happens
Trigger: new FormatConvertedBitmap { DestinationFormat = ... } with BeginInit/EndInit but no Source; XAML missing Source; Source bound to a value not yet loaded.
Common situations: Async image loading where binding has not delivered the BitmapImage before EndInit; code-built deferred init missing the Source assignment; refactors dropping the Source line.
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/3db3cec881a2f5e5.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/FormatConvertedBitmap.cs:169
/// </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;
}
if (DestinationFormat.Palettized)
{
if (DestinationPalette == null)
{
if (throwIfInvalid)
{
throw new InvalidOperationException(SR.Image_IndexedPixelFormatRequiresPalette);
}
return false;
}
else if ((1 << DestinationFormat.BitsPerPixel) < DestinationPalette.Colors.Count)
{
if (throwIfInvalid)
{
throw new InvalidOperationException(SR.Image_PaletteColorsDoNotMatchFormat);View on GitHub (pinned to 81131a70a4)