SixLabors/ImageSharp · error · ImageFormatException

Iptc profile size exceeds limit of

Error message

Iptc profile size exceeds limit of {maxBytes} bytes

What it means

WriteIptcProfile embeds the IPTC (IIM) profile inside an Adobe Photoshop APP13 marker during JPEG encoding. APP13 segment length is limited to 16-bit, so the encoder enforces a maximum profile size (maxBytes) and throws ImageFormatException when the serialized IPTC data exceeds it, since it could not be written as a single valid JPEG segment.

Solutions

  1. Reduce the IPTC profile size: remove unneeded/duplicate records before saving (rebuild the IptcProfile with only required values).
  2. Strip the IptcProfile from metadata (image.Metadata.IptcProfile = null) if IPTC embedding is not required.
  3. Export the oversized IPTC data to a sidecar (XMP or external file) instead of embedding it in the JPEG.
  4. Catch ImageFormatException around the save call and fall back to saving without the profile.

Example fix

// before
image.Metadata.IptcProfile.Set(new IptcValue(IptcTag.Keywords, longKeywords));
image.Save(path, new JpegEncoder());
// after
if (image.Metadata.IptcProfile != null && image.Metadata.IptcProfile.ToByteArray().Length > 65533)
{
    image.Metadata.IptcProfile = null; // or trim records first
}
image.Save(path, new JpegEncoder());
Defensive patterns

Strategy: validation

Validate before calling

var iptcBytes = image.Metadata.IptcProfile?.ToByteArray();
if (iptcBytes is { Length: > 65533 })
{
    // trim records or drop the profile before saving
    image.Metadata.IptcProfile = null;
}

Try / catch

try
{
    image.Save(path, new JpegEncoder());
}
catch (ImageFormatException ex) when (ex.Message.Contains("Iptc profile size"))
{
    image.Metadata.IptcProfile = null;
    image.Save(path, new JpegEncoder());
}

Prevention

When it happens

Trigger: Calling Save/SaveAsync with a JpegEncoder while the image metadata contains an IptcProfile whose serialized byte length exceeds the encoder's maxBytes limit (large IPTC record sets, e.g. many keywords or embedded trailer data).

Common situations: Re-encoding images transferred from photo-management systems that store very large IPTC blocks; accumulating duplicate IPTC records after multiple edit round-trips; images where the profile grew past the APP13 64KB practical limit.

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/0b32441f66152409. Report an issue: GitHub.

Appendix: source

Thrown at src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs:365

    /// </exception>
    private void WriteIptcProfile(IptcProfile iptcProfile, Span<byte> buffer)
    {
        const int maxBytes = 65533;
        if (iptcProfile is null || !iptcProfile.Values.Any())
        {
            return;
        }

        iptcProfile.UpdateData();
        byte[] data = iptcProfile.Data;
        if (data.Length == 0)
        {
            return;
        }

        if (data.Length > maxBytes)
        {
            throw new ImageFormatException($"Iptc profile size exceeds limit of {maxBytes} bytes");
        }

        int app13Length = 2 + Components.Decoder.ProfileResolver.AdobePhotoshopApp13Marker.Length +
                          Components.Decoder.ProfileResolver.AdobeImageResourceBlockMarker.Length +
                          Components.Decoder.ProfileResolver.AdobeIptcMarker.Length +
                          2 + 4 + data.Length;
        this.WriteAppHeader(app13Length, JpegConstants.Markers.APP13, buffer);
        this.outputStream.Write(Components.Decoder.ProfileResolver.AdobePhotoshopApp13Marker);
        this.outputStream.Write(Components.Decoder.ProfileResolver.AdobeImageResourceBlockMarker);
        this.outputStream.Write(Components.Decoder.ProfileResolver.AdobeIptcMarker);
        this.outputStream.WriteByte(0); // a empty pascal string (padded to make size even)
        this.outputStream.WriteByte(0);
        BinaryPrimitives.WriteInt32BigEndian(buffer, data.Length);
        this.outputStream.Write(buffer, 0, 4);
        this.outputStream.Write(data, 0, data.Length);
    }

    /// <summary>

View on GitHub (pinned to 59ce6af6fc)