{"record":{"id":"3373dc216e1451a9","repo":"SubtitleEdit/subtitleedit","slug":"file-type-not-supported","errorCode":null,"errorMessage":"File type not supported","messagePattern":"File type not supported","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/ui/Logic/Ocr/GoogleLens/Core.cs","lineNumber":75,"sourceCode":"        return await ScanByBufferAsync(file);\n    }\n\n    public async Task<List<string>> ScanByBufferAsync(byte[] buffer)\n    {\n        SKEncodedImageFormat? format = null;\n        \n        try\n        {\n            using var stream = new MemoryStream(buffer);\n            using var codec = SKCodec.Create(stream);\n            if (codec != null)\n            {\n                format = codec.EncodedFormat;\n            }\n        }\n        catch\n        {\n            throw new Exception(\"File type not supported\");\n        }\n\n        var (Width, Height) = Helper.ImageDimensionsFromData(buffer);\n        if (Width == 0 && Height == 0)\n        {\n            throw new Exception(\"Could not determine image dimensions\");\n        }\n\n        // Google Lens does not accept images larger than 1000x1000\n        if (Width > 1000 || Height > 1000)\n        {\n            buffer = await Helper.ResizeImageAsync(buffer, 1000, 1000);\n        }\n\n        string mimeType = format switch\n        {\n            SKEncodedImageFormat.Png => \"image/png\",\n            SKEncodedImageFormat.Jpeg => \"image/jpeg\",","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/ui/Logic/Ocr/GoogleLens/Core.cs#L57-L93","documentation":"Thrown by Core.ScanByBufferAsync when SKCodec.Create throws while probing the buffer to detect the image format. The catch swallows the underlying SkiaSharp exception and rethrows a generic 'File type not supported' Exception, losing the original cause. It means the byte buffer is not a decodable image (PNG/JPEG/WEBP) that SkiaSharp recognizes.","triggerScenarios":"Calling ScanByBufferAsync with a byte array that is not an image, is a corrupted/truncated image, or is in a format SkiaSharp cannot decode (e.g. TIFF, HEIC without native codec, BMP in some builds).","commonSituations":"User selected a non-image file for OCR; a partial download; a format not bundled in the platform's SkiaSharp native dependencies; an SVG or PDF mistaken for a raster image.","solutions":["Validate the magic bytes (PNG/JPEG/WEBP signatures) before calling ScanByBufferAsync.","Ensure the SkiaSharp native assets are installed for the target runtime.","Convert unsupported formats (HEIC/TIFF) to PNG/JPEG upstream.","Re-download or re-export the source image if it may be truncated."],"exampleFix":"// before\nvar result = await core.ScanByBufferAsync(fileBytes);\n\n// after\nif (!IsRecognizedImage(fileBytes)) throw new ArgumentException(\"Provide a PNG/JPEG/WEBP image\");\nvar result = await core.ScanByBufferAsync(fileBytes);","handlingStrategy":"validation","validationCode":"static readonly byte[] Png = { 0x89,0x50,0x4E,0x47 };\nstatic readonly byte[] Jpeg = { 0xFF,0xD8,0xFF };\nstatic readonly byte[] Webp = { 0x52,0x49,0x46,0x46 }; // RIFF (check WEBP at offset 8)\nstatic bool IsSupportedImage(byte[] b) =>\n    b != null && b.Length >= 12 &&\n    (b.Take(4).SequenceEqual(Png) || b.Take(3).SequenceEqual(Jpeg) ||\n     (b.Take(4).SequenceEqual(Webp) && Encoding.ASCII.GetString(b,8,4) == \"WEBP\"));\nif (!IsSupportedImage(buffer)) throw new ArgumentException(\"File type not supported\");","typeGuard":"static bool IsRecognizedImage(byte[] b) => /* magic-byte check as above */ true;","tryCatchPattern":"try { var r = await core.ScanByBufferAsync(buffer); }\ncatch (Exception ex) when (ex.Message == \"File type not supported\") { /* notify user to pick PNG/JPEG/WEBP */ }","preventionTips":["Filter file pickers to image extensions and re-validate by magic bytes.","Ensure SkiaSharp native codecs are deployed for the runtime.","Convert unsupported formats to PNG/JPEG before OCR.","Surface a clear user message for non-image selections."],"tags":["google-lens","ocr","image","skiasharp","validation"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}