{"record":{"id":"0b32441f66152409","repo":"SixLabors/ImageSharp","slug":"iptc-profile-size-exceeds-limit-of-maxbytes-bytes","errorCode":null,"errorMessage":"Iptc profile size exceeds limit of {maxBytes} bytes","messagePattern":"Iptc profile size exceeds limit of (.+?) bytes","errorType":"exception","errorClass":"ImageFormatException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs","lineNumber":365,"sourceCode":"    /// </exception>\n    private void WriteIptcProfile(IptcProfile iptcProfile, Span<byte> buffer)\n    {\n        const int maxBytes = 65533;\n        if (iptcProfile is null || !iptcProfile.Values.Any())\n        {\n            return;\n        }\n\n        iptcProfile.UpdateData();\n        byte[] data = iptcProfile.Data;\n        if (data.Length == 0)\n        {\n            return;\n        }\n\n        if (data.Length > maxBytes)\n        {\n            throw new ImageFormatException($\"Iptc profile size exceeds limit of {maxBytes} bytes\");\n        }\n\n        int app13Length = 2 + Components.Decoder.ProfileResolver.AdobePhotoshopApp13Marker.Length +\n                          Components.Decoder.ProfileResolver.AdobeImageResourceBlockMarker.Length +\n                          Components.Decoder.ProfileResolver.AdobeIptcMarker.Length +\n                          2 + 4 + data.Length;\n        this.WriteAppHeader(app13Length, JpegConstants.Markers.APP13, buffer);\n        this.outputStream.Write(Components.Decoder.ProfileResolver.AdobePhotoshopApp13Marker);\n        this.outputStream.Write(Components.Decoder.ProfileResolver.AdobeImageResourceBlockMarker);\n        this.outputStream.Write(Components.Decoder.ProfileResolver.AdobeIptcMarker);\n        this.outputStream.WriteByte(0); // a empty pascal string (padded to make size even)\n        this.outputStream.WriteByte(0);\n        BinaryPrimitives.WriteInt32BigEndian(buffer, data.Length);\n        this.outputStream.Write(buffer, 0, 4);\n        this.outputStream.Write(data, 0, data.Length);\n    }\n\n    /// <summary>","sourceCodeStart":347,"sourceCodeEnd":383,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs#L347-L383","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Reduce the IPTC profile size: remove unneeded/duplicate records before saving (rebuild the IptcProfile with only required values).","Strip the IptcProfile from metadata (image.Metadata.IptcProfile = null) if IPTC embedding is not required.","Export the oversized IPTC data to a sidecar (XMP or external file) instead of embedding it in the JPEG.","Catch ImageFormatException around the save call and fall back to saving without the profile."],"exampleFix":"// before\nimage.Metadata.IptcProfile.Set(new IptcValue(IptcTag.Keywords, longKeywords));\nimage.Save(path, new JpegEncoder());\n// after\nif (image.Metadata.IptcProfile != null && image.Metadata.IptcProfile.ToByteArray().Length > 65533)\n{\n    image.Metadata.IptcProfile = null; // or trim records first\n}\nimage.Save(path, new JpegEncoder());","handlingStrategy":"validation","validationCode":"var iptcBytes = image.Metadata.IptcProfile?.ToByteArray();\nif (iptcBytes is { Length: > 65533 })\n{\n    // trim records or drop the profile before saving\n    image.Metadata.IptcProfile = null;\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    image.Save(path, new JpegEncoder());\n}\ncatch (ImageFormatException ex) when (ex.Message.Contains(\"Iptc profile size\"))\n{\n    image.Metadata.IptcProfile = null;\n    image.Save(path, new JpegEncoder());\n}","preventionTips":["Check the serialized IPTC size before every JPEG save when profiles may be present.","Deduplicate IPTC records after metadata round-trips; profiles grow silently.","Keep very large metadata in XMP or sidecar files instead of APP13 IPTC."],"tags":["jpeg","metadata","iptc","size-limit"],"backgroundTag":"payload-too-large","analyzedSha":"59ce6af6fc29027cda277ef62d4d1694a8acce91","analyzedAt":"2026-09-13T18:34:59.331Z","contentChangedAt":"2026-09-13T18:34:59.331Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}