stride3d/stride · error · ArgumentException

Invalid 'uri' argument.

Error message

Invalid 'uri' argument.

What it means

The string overload of ImageExtensions.SetSource throws ArgumentException("Invalid 'uri' argument.") when the string is null, empty, or whitespace — a deliberate ArgumentException (not ArgumentNullException) covering the empty/whitespace case as well.

Solutions

  1. Validate with string.IsNullOrWhiteSpace and provide a default/placeholder image instead.
  2. Fix the configuration or resource supplying the empty path.
  3. Guard the call site: only call SetSource when the path is non-empty.
  4. If empty means 'no image', clear image.Source explicitly rather than calling SetSource.

Example fix

// before
logo.SetSource(config.IconPath ?? ""); // throws for empty
// after
if (!string.IsNullOrWhiteSpace(config.IconPath))
    logo.SetSource(config.IconPath);
else
    logo.Source = null;
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(imagePath))
{
    image.Source = null; // or placeholder
    return;
}
image.SetSource(imagePath);

Type guard

static bool IsValidImageUri(string uri) => Uri.TryCreate(uri, UriKind.Absolute, out _);

Try / catch

try { image.SetSource(imagePath); }
catch (ArgumentException) { image.Source = placeholderImage; }

Prevention

When it happens

Trigger: Calling image.SetSource("") or image.SetSource(" ") with the string overload.

Common situations: Binding a path property that defaults to empty string; a settings file or resource key resolved to blank; trimming user input left an empty value that is then used as an image source.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/434c96139081e0b2. Report an issue: GitHub.

Appendix: source

Thrown at sources/presentation/Stride.Core.Presentation.Wpf/Extensions/ImageExtensions.cs:27

namespace Stride.Core.Presentation.Extensions
{
    public static class ImageExtensions
    {
        public static void SetSource([NotNull] this Image image, [NotNull] Uri uri)
        {
            if (image == null)
                throw new ArgumentNullException(nameof(image));
            if (uri == null)
                throw new ArgumentNullException(nameof(uri));

            image.Source = ImageSourceFromFile(uri);
        }

        public static void SetSource([NotNull] this Image image, [NotNull] string uri)
        {
            if (string.IsNullOrWhiteSpace(uri))
                throw new ArgumentException("Invalid 'uri' argument.");

            SetSource(image, new Uri(uri));
        }

        [NotNull]
        public static ImageSource ImageSourceFromFile([NotNull] Uri uri)
        {
            if (uri == null)
                throw new ArgumentNullException(nameof(uri));

            var source = new BitmapImage();
            source.BeginInit();
            source.UriSource = uri;
            source.CacheOption = BitmapCacheOption.OnLoad;
            source.EndInit();

            return source;
        }

View on GitHub (pinned to 96fad776d2)