dotnet/wpf · error · ArgumentException

SR.ColorContext_FileTooLarge

Error message

SR.ColorContext_FileTooLarge

What it means

ColorContext.FromStream throws ArgumentException(SR.ColorContext_FileTooLarge) when the profile stream is larger than the maximum supported size for an in-memory raw profile buffer. The reader accumulates chunks into rawBytes and, if it still needs more space after the size cap, gives up and throws with the filename as parameter.

Solutions

  1. Check the profile stream length before constructing ColorContext and reject oversized files
  2. Validate the ICC file is well-formed (128-byte header, sane tag table) before loading
  3. Regenerate or re-export the color profile from a profile editor
  4. Catch ArgumentException and fall back to a standard profile

Example fix

// before
var ctx = new ColorContext(profileStream);
// after
if (profileStream.Length <= 4 * 1024 * 1024) { var ctx = new ColorContext(profileStream); } else { throw new InvalidOperationException("Profile too large"); }
Defensive patterns

Strategy: validation

Validate before calling

if (profileStream != null && profileStream.CanSeek && profileStream.Length > 4 * 1024 * 1024) throw new ArgumentException("ICC profile exceeds maximum supported size");

Try / catch

try { var ctx = new ColorContext(stream); } catch (ArgumentException ex) { /* reject oversized profile, use default profile */ }

Prevention

When it happens

Trigger: Calling ColorContext(Stream) / FromStream with a stream whose ICC profile data exceeds the library's maximum buffer size (a corrupted or maliciously oversized profile).

Common situations: Corrupt or truncated-then-padded ICC files; hostile XPS payloads containing huge profiles; passing a stream of the wrong file type by accident.

Understand the failure class

Background: "File too large" / "file size exceeds limit" errors: why libraries cap file sizes and how to fix them — this error's family across 46 libraries.

Related errors


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

Appendix: source

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

                    using (FactoryMaker factoryMaker = new FactoryMaker())
                    {
                        HRESULT.Check(UnsafeNativeMethodsMilCoreApi.WICCodec.CreateColorContext(factoryMaker.ImagingFactoryPtr, out _colorContextHandle));
                        HRESULT.Check(IWICCC.InitializeFromMemory(_colorContextHandle, rawBytes, (uint)numBytesRead));
                    }

                    return;
                }
                else
                {
                    bufferSize += _bufferSizeIncrement;
                    byte[] newRawBytes = new byte[bufferSize];
                    rawBytes.CopyTo(newRawBytes, 0);
                    rawBytes = newRawBytes;
                }
            }

            throw new ArgumentException(SR.ColorContext_FileTooLarge, filename);
        }

        /// Note: often the data buffer is larger than the actual data in it.
        ///
        /// dontThrowException is for preserving the 3.* behavior of ColorContext(SafeMILHandle)
        ///
        private void FromRawBytes(byte[] data, int dataLength, bool dontThrowException) 
        {
            Invariant.Assert(dataLength <= data.Length);
            Invariant.Assert(dataLength >= 0);
            
            UnsafeNativeMethods.PROFILEHEADER header;
            UnsafeNativeMethods.PROFILE profile;

            unsafe
            {
                fixed (void *dataPtr = data)
                {

View on GitHub (pinned to 81131a70a4)