dotnet/wpf · error · ArgumentException

SR.Image_SizeOptionsAngle

Error message

SR.Image_SizeOptionsAngle

What it means

BitmapSizeOptions.FromRotation validates its Rotation parameter and throws ArgumentException with SR.Image_SizeOptionsAngle when the value is not one of Rotation.Rotate0/90/180/270. Rotation is a bounded enum and any other value (e.g. a casted invalid int) is rejected.

Solutions

  1. Restrict rotation values to multiples of 90 degrees using the Rotation enum members.
  2. Validate the numeric source with a bounds check before casting to Rotation.
  3. If arbitrary-angle rotation is needed, use TransformedBitmap with a RotateTransform instead of FromRotation.

Example fix

// before
var opts = BitmapSizeOptions.FromRotation((Rotation)angle); // angle=45
// after
var opts = BitmapSizeOptions.FromRotation((Rotation)((angle / 90) * 90 % 360));
Defensive patterns

Strategy: validation

Validate before calling

if (angle % 90 != 0 || angle < 0 || angle > 270)
    throw new ArgumentOutOfRangeException(nameof(angle));
var rotation = (Rotation)angle;

Type guard

bool IsValidRotation(Rotation r) => r is Rotation.Rotate0 or Rotation.Rotate90 or Rotation.Rotate180 or Rotation.Rotate270;

Try / catch

try { var opts = BitmapSizeOptions.FromRotation(rotation); } catch (ArgumentException ex) { /* fix rotation param */ }

Prevention

When it happens

Trigger: Calling BitmapSizeOptions.FromRotation((Rotation)45) or passing a default/uninitialized Rotation value cast from an out-of-range integer.

Common situations: Computing rotation angles dynamically (arbitrary degrees) and casting to Rotation; deserializing a rotation setting from config that holds a non-multiple-of-90 value.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapSizeOptions.cs:176

            return sizeOptions;
        }

        /// <summary>
        /// Constructs an BitmapSizeOptions that does not preserve the aspect ratio and
        /// instead uses dimensions pixelWidth x pixelHeight.
        /// </summary>
        /// <param name="rotation">Angle to rotate</param>
        public static BitmapSizeOptions FromRotation(Rotation rotation)
        {
            switch(rotation)
            {
                case Rotation.Rotate0:
                case Rotation.Rotate90:
                case Rotation.Rotate180:
                case Rotation.Rotate270:
                    break;
                default:
                    throw new ArgumentException(SR.Image_SizeOptionsAngle, nameof(rotation));
            }

            BitmapSizeOptions sizeOptions = new BitmapSizeOptions
            {
                _rotationAngle = rotation,
                _preservesAspectRatio = true,
                _pixelWidth = 0,
                _pixelHeight = 0
            };

            return sizeOptions;
        }

        // Note: In this method, newWidth, newHeight are not affected by the
        // rotation angle.
        internal void GetScaledWidthAndHeight(
            uint width,
            uint height,

View on GitHub (pinned to 81131a70a4)