{"record":{"id":"3db3cec881a2f5e5","repo":"dotnet/wpf","slug":"sr-image-noargument-formatconvertedbitmap","errorCode":null,"errorMessage":"SR.Image_NoArgument","messagePattern":"SR\\.Image_NoArgument","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/FormatConvertedBitmap.cs","lineNumber":169,"sourceCode":"        /// </summary>\n        private void SourcePropertyChangedHook(DependencyPropertyChangedEventArgs e)\n        {\n            if (!e.IsASubPropertyChange)\n            {\n                BitmapSource newSource = e.NewValue as BitmapSource;\n                _source = newSource;\n                RegisterDownloadEventSource(_source);\n                _syncObject = (newSource != null) ? newSource.SyncObject : _bitmapInit;\n            }\n        }\n\n        internal override bool IsValidForFinalizeCreation(bool throwIfInvalid)\n        {\n            if (Source == null)\n            {\n                if (throwIfInvalid)\n                {\n                    throw new InvalidOperationException(SR.Format(SR.Image_NoArgument, \"Source\"));\n                }\n                return false;\n            }\n            if (DestinationFormat.Palettized)\n            {\n                if (DestinationPalette == null)\n                {\n                    if (throwIfInvalid)\n                    {\n                        throw new InvalidOperationException(SR.Image_IndexedPixelFormatRequiresPalette);\n                    }\n                    return false;\n                }\n                else if ((1 << DestinationFormat.BitsPerPixel) < DestinationPalette.Colors.Count)\n                {\n                    if (throwIfInvalid)\n                    {\n                        throw new InvalidOperationException(SR.Image_PaletteColorsDoNotMatchFormat);","sourceCodeStart":151,"sourceCodeEnd":187,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/FormatConvertedBitmap.cs#L151-L187","documentation":"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.","triggerScenarios":"new FormatConvertedBitmap { DestinationFormat = ... } with BeginInit/EndInit but no Source; XAML missing Source; Source bound to a value not yet loaded.","commonSituations":"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.","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)"],"exampleFix":"// before\nvar fcb = new FormatConvertedBitmap();\nfcb.BeginInit();\nfcb.DestinationFormat = PixelFormats.Gray8;\nfcb.EndInit(); // throws\n// after\nvar fcb = new FormatConvertedBitmap();\nfcb.BeginInit();\nfcb.Source = new BitmapImage(new Uri(\"photo.png\"));\nfcb.DestinationFormat = PixelFormats.Gray8;\nfcb.EndInit();","handlingStrategy":"validation","validationCode":"if (fcb.Source == null) throw new ArgumentException(\"Source required before EndInit\", nameof(source));","typeGuard":"static bool ReadyToConvert(FormatConvertedBitmap f) => f.Source != null && f.DestinationFormat != PixelFormats.Default;","tryCatchPattern":"try { fcb.EndInit(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Source\")) { fcb.Source = placeholderBitmap; fcb.EndInit(); }","preventionTips":["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"],"tags":["wpf","imaging","initialization"],"backgroundTag":"missing-required-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}