{"record":{"id":"b9937e66ea5a1196","repo":"dotnet/wpf","slug":"sr-image-alphathresholdoutofrange","errorCode":null,"errorMessage":"SR.Image_AlphaThresholdOutOfRange","messagePattern":"SR\\.Image_AlphaThresholdOutOfRange","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/FormatConvertedBitmap.cs","lineNumber":37,"sourceCode":"        /// </summary>\n        public FormatConvertedBitmap() : base(true)\n        {\n        }\n\n        /// <summary>\n        /// Construct a FormatConvertedBitmap\n        /// </summary>\n        /// <param name=\"source\">BitmapSource to apply to the format conversion to</param>\n        /// <param name=\"destinationFormat\">Destionation Format to  apply to the bitmap</param>\n        /// <param name=\"destinationPalette\">Palette if format is palettized</param>\n        /// <param name=\"alphaThreshold\">Alpha threshold</param>\n        public FormatConvertedBitmap(BitmapSource source, PixelFormat destinationFormat, BitmapPalette destinationPalette, double alphaThreshold)\n            : base(true) // Use base class virtuals\n        {\n            ArgumentNullException.ThrowIfNull(source);\n            if (alphaThreshold < (double)(0.0) || alphaThreshold > (double)(100.0))\n            {\n                throw new ArgumentException(SR.Image_AlphaThresholdOutOfRange);\n            }\n\n            _bitmapInit.BeginInit();\n\n            Source = source;\n            DestinationFormat = destinationFormat;\n            DestinationPalette = destinationPalette;\n            AlphaThreshold = alphaThreshold;\n\n            _bitmapInit.EndInit();\n            FinalizeCreation();\n        }\n\n        // ISupportInitialize\n\n        /// <summary>\n        /// Prepare the bitmap to accept initialize paramters.\n        /// </summary>","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/FormatConvertedBitmap.cs#L19-L55","documentation":"FormatConvertedBitmap's constructor validates that alphaThreshold is a percentage within [0, 100]. Values outside this range cannot be used for alpha-threshold conversion, so an ArgumentException is thrown before initialization begins.","triggerScenarios":"new FormatConvertedBitmap(source, format, palette, -1) or alphaThreshold = 150, NaN passed through comparisons; any double outside 0..100 including negative opacity-derived values.","commonSituations":"Passing an alpha in 0..255 (byte) units instead of 0..100 percent; sign errors; computing the threshold from a config value with wrong scale.","solutions":["Clamp the value: Math.Max(0, Math.Min(100, alphaThreshold))","Convert from 0-255 scale: alphaThreshold = alpha255 * 100.0 / 255.0","Pass 0 (no thresholding) or 100 (full thresholding) if unsure"],"exampleFix":"// before\nbyte alpha = 128;\nvar fcb = new FormatConvertedBitmap(src, PixelFormats.Bgr32, null, alpha); // 128 > 100, throws\n// after\nbyte alpha = 128;\ndouble alphaThreshold = alpha * 100.0 / 255.0; // ~50.2\nvar fcb = new FormatConvertedBitmap(src, PixelFormats.Bgr32, null, alphaThreshold);","handlingStrategy":"validation","validationCode":"static double NormalizeAlphaThreshold(double t) => double.IsNaN(t) ? 0 : Math.Clamp(t, 0.0, 100.0);","typeGuard":"static bool IsValidAlphaThreshold(double t) => t >= 0.0 && t <= 100.0 && !double.IsNaN(t);","tryCatchPattern":"try { return new FormatConvertedBitmap(src, fmt, palette, alphaThreshold); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"AlphaThreshold\")) { return new FormatConvertedBitmap(src, fmt, palette, Math.Clamp(alphaThreshold, 0, 100)); }","preventionTips":["Remember alphaThreshold is a percentage (0-100), not a byte alpha (0-255)","Clamp config- or UI-derived values before constructing","Use Math.Clamp at the API boundary of any image-conversion helper"],"tags":["wpf","imaging","argument"],"backgroundTag":"value-out-of-range","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"}