{"record":{"id":"672d41e64520f203","repo":"wmjordan/PDFPatcher","slug":"file-is-not-a-valid-jpeg","errorCode":null,"errorMessage":"File is not a valid JPEG","messagePattern":"File is not a valid JPEG","errorType":"exception","errorClass":"ExifLibException","httpStatus":null,"severity":"error","filePath":"App/Processor/Imaging/JpgHelper.cs","lineNumber":142,"sourceCode":"\t\t\t}\r\n\r\n\t\t\tprivate void Initialize() {\r\n\t\t\t\tif (_isInitialized)\r\n\t\t\t\t\treturn;\r\n\r\n\t\t\t\t_isInitialized = true;\r\n\r\n\t\t\t\t// JPEG encoding uses big endian (i.e. Motorola) byte aligns. The TIFF encoding\r\n\t\t\t\t// found later in the document will specify the byte aligns used for the\r\n\t\t\t\t// rest of the document.\r\n\t\t\t\t_isLittleEndian = false;\r\n\r\n\t\t\t\t// Open the file in a stream            \r\n\t\t\t\t_reader = new BinaryReader(_stream, System.Text.Encoding.UTF8);\r\n\r\n\t\t\t\t// Make sure the file's a JPEG.\r\n\t\t\t\tif (ReadUShort() != 0xFFD8)\r\n\t\t\t\t\tthrow new ExifLibException(\"File is not a valid JPEG\");\r\n\r\n\t\t\t\t// Scan to the start of the Exif content\r\n\t\t\t\tReadToExifStart();\r\n\r\n\t\t\t\t// Create an index of all Exif tags found within the document\r\n\t\t\t\tCreateTagIndex();\r\n\t\t\t}\r\n\r\n\t\t\t#region TIFF methods\r\n\r\n\t\t\t/// <summary>\r\n\t\t\t/// Returns the length (in bytes) per component of the specified TIFF data type\r\n\t\t\t/// </summary>\r\n\t\t\t/// <returns></returns>\r\n\t\t\tprivate static byte GetTIFFFieldLength(ushort tiffDataType) {\r\n\t\t\t\treturn tiffDataType switch {\r\n\t\t\t\t\t1 or 2 or 7 or 6 => 1,\r\n\t\t\t\t\t3 or 8 => 2,\r","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/wmjordan/PDFPatcher/blob/4782bbd9ada850b56258f29999b7a1c9076c9b1b/App/Processor/Imaging/JpgHelper.cs#L124-L160","documentation":"JpgHelper.Initialize throws ExifLibException('File is not a valid JPEG') when the first two bytes of the stream are not the JPEG SOI marker 0xFFD8. This is the entry-point validation that the supplied stream is actually a JPEG before attempting Exif parsing.","triggerScenarios":"Constructing the ExifReader (which lazily calls Initialize) with a stream whose first two bytes are not 0xFF 0xD8 — e.g. a PNG, GIF, TIFF, PDF, or truncated/corrupt file passed where a JPEG was expected.","commonSituations":"Passing a non-JPEG image stream to the Exif reader; a file with the wrong extension; a corrupted or zero-byte JPEG; a stream whose position is not at the start; mistakenly feeding a PDF object stream that isn't a JPEG.","solutions":["Verify the file type (by magic bytes / extension) before handing the stream to ExifReader.","Ensure the stream position is at 0 before construction.","For extracted PDF images, check the image filter/mime type to confirm it is DCTDecode (JPEG) before Exif parsing.","Handle ExifLibException gracefully and skip Exif extraction for non-JPEG formats."],"exampleFix":"// before\nvar reader = new ExifReader(stream);\n\n// after\nif (!IsJpeg(stream)) {\n  return; // skip Exif for non-JPEG\n}\nstream.Position = 0;\nvar reader = new ExifReader(stream);\n\nstatic bool IsJpeg(Stream s) {\n  long p = s.Position; s.Position = 0;\n  int b0 = s.ReadByte(), b1 = s.ReadByte();\n  s.Position = p;\n  return b0 == 0xFF && b1 == 0xD8;\n}","handlingStrategy":"validation","validationCode":"long pos = stream.Position; stream.Position = 0;\nint b0 = stream.ReadByte(), b1 = stream.ReadByte();\nstream.Position = pos;\nif (b0 != 0xFF || b1 != 0xD8) return; // not a JPEG","typeGuard":"static bool IsJpeg(Stream s) {\n  long p = s.Position; s.Position = 0;\n  int b0 = s.ReadByte(), b1 = s.ReadByte(); s.Position = p;\n  return b0 == 0xFF && b1 == 0xD8;\n}","tryCatchPattern":"try { var r = new ExifReader(stream); }\ncatch (ExifLibException) { /* not a JPEG; skip Exif */ }","preventionTips":["Check magic bytes before Exif parsing.","Reset stream position to 0 before constructing the reader.","Confirm PDF image filter is DCTDecode before treating as JPEG."],"tags":["image","jpeg","exif","validation","file-format"],"backgroundTag":null,"analyzedSha":"4782bbd9ada850b56258f29999b7a1c9076c9b1b","analyzedAt":"2026-08-13T17:44:50.812Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}