dotnet/wpf · error · FileNotFoundException

SR.Format(SR.FileNotFoundExceptionWithFileName…

Error message

SR.Format(SR.FileNotFoundExceptionWithFileName, profileUri.AbsolutePath)

What it means

ColorContext.Initialize throws FileNotFoundException with FileNotFoundExceptionWithFileName when the color profile stream for the given URI could not be obtained. The URI was user-supplied (not a standard profile) and no profile data exists at profileUri, so the file/embbeded resource is missing. The exception message includes the profile URI path for diagnostics.

Solutions

  1. Verify the profile file exists at the URI's AbsolutePath before constructing the ColorContext
  2. Use pack/application URIs for resources embedded in assemblies and confirm the file's Build Action is Resource
  3. Correct the path spelling/casing and deploy the .icc/.icm file alongside the app
  4. Catch FileNotFoundException and fall back to the standard sRGB ColorContext

Example fix

// before
var ctx = new ColorContext(new Uri("C:\\Profiles\\missing.icc"));
// after
if (File.Exists(profilePath)) { var ctx = new ColorContext(new Uri(profilePath)); } else { var ctx = new ColorContext(PixelFormats.Pbgra32); }
Defensive patterns

Strategy: validation

Validate before calling

if (profileUri == null || !File.Exists(profileUri.AbsolutePath) && !Application.GetResourceStream(profileUri).Stream.CanRead) throw new FileNotFoundException(profileUri.AbsolutePath);

Try / catch

try { var ctx = new ColorContext(profileUri); } catch (FileNotFoundException ex) { log.Warn($"Profile missing: {ex.FileName}"); ctx = new ColorContext(PixelFormats.Srgb); }

Prevention

When it happens

Trigger: new ColorContext(Uri) with a user-provided URI whose AbsolutePath does not resolve to an existing profile file or embedded package part; Initialize reaches profileStream == null after the boundary check passed.

Common situations: Typo in profile file path; profile file deleted or not deployed with the app; URI pointing to a resource not embedded in the assembly/XPS package; case-sensitivity or path-format mistakes in pack URIs.

Understand the failure class

Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ColorContext.cs:541

            }

            if (profileStream == null)
            {
                if (tryProfileFromResource)
                {
                    ResourceManager resourceManager = new ResourceManager(_colorProfileResources, Assembly.GetAssembly(typeof(ColorContext)));
                    byte[] sRGBProfile = (byte[])resourceManager.GetObject(_sRGBProfileName);

                    profileStream = new MemoryStream(sRGBProfile);
                }
                else
                {
                    //
                    // SECURITY WARNING: This exception includes the profile URI which may contain sensitive information. However, as of right now,
                    // this is safe because it can only happen when the URI is given to us by the user.
                    //
                    Invariant.Assert(!isStandardProfileUriNotFromUser);
                    throw new FileNotFoundException(SR.Format(SR.FileNotFoundExceptionWithFileName, profileUri.AbsolutePath), profileUri.AbsolutePath);
                }
            }

            FromStream(profileStream, profileUri.AbsolutePath);
        }

        /// <summary>
        /// Obtains the system color profile path
        /// </summary>
        private static Uri GetStandardColorSpaceProfile()
        {
            const int SIZE = NativeMethods.MAX_PATH;
            
            uint dwProfileID = (uint)NativeMethods.ColorSpace.SPACE_sRGB;
            uint bufferSize = SIZE;
            StringBuilder buffer = new StringBuilder(SIZE);

            HRESULT.Check(UnsafeNativeMethodsMilCoreApi.Mscms.GetStandardColorSpaceProfile(IntPtr.Zero, dwProfileID, buffer, out bufferSize));

View on GitHub (pinned to 81131a70a4)