SixLabors/ImageSharp · error · ArgumentException

The iptc value exceeds the limit of

Error message

The iptc value exceeds the limit of {maxLength} bytes for the tag {this.Tag}

What it means

IPTC records cap each field at a fixed byte length (per tag). The IptcValue constructor truncates the string to maxLength characters, but the encoded bytes can still exceed the limit, in which case it throws ArgumentException rather than silently writing a malformed record.

Solutions

  1. Shorten the value so it fits the tag's byte limit under the chosen encoding
  2. Use a single-byte encoding such as Latin-1 (ISO-8859-1) for the IptcProfile so more characters fit per byte
  3. Truncate the value yourself by encoding incrementally and cutting at a character boundary within maxLength
  4. Catch ArgumentException and skip or log the oversized field when writing metadata in bulk

Example fix

// before
new IptcValue(Encoding.UTF8, IptcTag.Caption, veryLongUnicodeCaption, true);
// after
profile.Update(new IptcValue(Encoding.Latin1, IptcTag.Caption, veryLongUnicodeCaption[..80], true));
Defensive patterns

Strategy: validation

Validate before calling

int max = 64; // per-tag limit
byte[] bytes = Encoding.Latin1.GetBytes(value);
if (bytes.Length > max) value = Encoding.Latin1.GetString(bytes[..max]);

Type guard

static bool FitsTag(string v, Encoding enc, int maxLength) => enc.GetByteCount(v) <= maxLength;

Try / catch

try { profile.Update(new IptcValue(encoding, tag, value, strict: true)); } catch (ArgumentException ex) { log.Warn(ex, $"IPTC value too long for {tag}"); }

Prevention

When it happens

Trigger: Constructing new IptcValue(encoding, tag, value, strict) where encoding.GetBytes(value) produces more bytes than the tag's maxLength even after string-level truncation — typical with multi-byte encodings (UTF-8/UTF-16) where characters cost more than one byte.

Common situations: Long captions or keywords written with UTF-8 non-ASCII characters (accents, CJK) exceeding e.g. a 64-byte limit; using an encoding wider than the record expects.

Understand the failure class

Background: payload too large / request exceeds maximum size: why libraries cap bytes and how to fix oversize payloads — this error's family across 50 libraries.

Related errors


AI-assisted analysis of SixLabors/ImageSharp@59ce6af6fc (2026-09-13). Data as JSON: /api/errors/58b60ad5c7270af6. Report an issue: GitHub.

Appendix: source

Thrown at src/ImageSharp/Metadata/Profiles/IPTC/IptcValue.cs:109

        set
        {
            if (string.IsNullOrEmpty(value))
            {
                this.data = [];
            }
            else
            {
                int maxLength = this.Tag.MaxLength();
                byte[] valueBytes;
                if (this.Strict && value.Length > maxLength)
                {
                    string cappedValue = value[..maxLength];
                    valueBytes = this.encoding.GetBytes(cappedValue);

                    // It is still possible that the bytes of the string exceed the limit.
                    if (valueBytes.Length > maxLength)
                    {
                        throw new ArgumentException($"The iptc value exceeds the limit of {maxLength} bytes for the tag {this.Tag}");
                    }
                }
                else
                {
                    valueBytes = this.encoding.GetBytes(value);
                }

                this.data = valueBytes;
            }
        }
    }

    /// <summary>
    /// Gets the length of the value.
    /// </summary>
    public int Length => this.data.Length;

    /// <inheritdoc/>

View on GitHub (pinned to 59ce6af6fc)