dotnet/wpf · error · InvalidOperationException

SR.Image_NoArgument

Error message

SR.Image_NoArgument

What it means

ColorConvertedBitmap requires a Source bitmap to convert. During EndInit, IsValidForFinalizeCreation detects Source == null and throws an InvalidOperationException (Image_NoArgument with parameter name "Source") because a color conversion cannot be finalized without an input image.

Solutions

  1. Set Source (a non-null BitmapSource) before calling EndInit
  2. In XAML, set <ColorConvertedBitmap.Source> or the Source attribute explicitly
  3. If using bindings, set Source synchronously or call BeginInit/EndInit only after the value is available

Example fix

// before
var ccb = new ColorConvertedBitmap();
ccb.BeginInit();
ccb.SourceColorContext = srcCtx;
ccb.DestinationColorContext = dstCtx;
ccb.EndInit(); // throws
// after
var ccb = new ColorConvertedBitmap();
ccb.BeginInit();
ccb.Source = srcBitmap;
ccb.SourceColorContext = srcCtx;
ccb.DestinationColorContext = dstCtx;
ccb.EndInit();
Defensive patterns

Strategy: validation

Validate before calling

if (ccb.Source == null) throw new ArgumentException("Set Source before EndInit", nameof(source));

Type guard

static bool CanFinalize(ColorConvertedBitmap c) => c.Source != null && c.SourceColorContext != null && c.DestinationColorContext != null;

Try / catch

try { ccb.EndInit(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Source")) { ccb.Source = fallbackBitmap; ccb.EndInit(); }

Prevention

When it happens

Trigger: new ColorConvertedBitmap { DestinationColorContext = ..., SourceColorContext = ... } inside a BeginInit/EndInit block without ever setting Source; setting only the color contexts in XAML.

Common situations: XAML data binding failing so the Source property is never set before EndInit; forgetting Source in code-built deferred initialization; a binding that resolves after EndInit runs.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/f51df5f86197df1f. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/ColorConvertedBitmap.cs:138

        /// </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 (SourceColorContext == null)
            {
                if (throwIfInvalid)
                {
                    throw new InvalidOperationException(SR.Color_NullColorContext);
                }
                return false;
            }

            if (DestinationColorContext == null)
            {
                if (throwIfInvalid)
                {
                    throw new InvalidOperationException(SR.Format(SR.Image_NoArgument, "DestinationColorContext"));

View on GitHub (pinned to 81131a70a4)