SubtitleEdit/subtitleedit · error · Exception

Could not resize image

Error message

Could not resize image

What it means

Thrown in the Google Lens OCR pipeline when an image larger than MAX_DIMENSION (1200px) must be downscaled and SkiaSharp's SKBitmap.Resize returns null. Resize yields null when Skia cannot produce the target bitmap: an unsupported source color type, degenerate/zero target dimensions, or a memory allocation failure.

Source

Thrown at src/ui/Logic/Ocr/GoogleLens/Lens.cs:57

        }

        var bitmapToProcess = bitmap;
        var finalMime = "image/png";

        const int MAX_DIMENSION = 1200;

        // Only process if absolutely necessary
        if (originalWidth > MAX_DIMENSION || originalHeight > MAX_DIMENSION)
        {
            // Calculate new dimensions maintaining aspect ratio
            float ratio = Math.Min((float)MAX_DIMENSION / originalWidth, (float)MAX_DIMENSION / originalHeight);
            int newWidth = (int)(originalWidth * ratio);
            int newHeight = (int)(originalHeight * ratio);

            using var resized = bitmap.Resize(new SKImageInfo(newWidth, newHeight), new SKSamplingOptions(SKFilterMode.Linear, SKMipmapMode.Linear));
            if (resized == null)
            {
                throw new Exception("Could not resize image");
            }

            var imageToProcessBuffer = resized.ToPngArray();
            return await ScanByData(imageToProcessBuffer, finalMime, new[] { originalWidth, originalHeight }, twoLetterLanguageCode);
        }

        var buffer = bitmapToProcess.ToPngArray();
        return await ScanByData(buffer, finalMime, [originalWidth, originalHeight], twoLetterLanguageCode);
    }
}

View on GitHub (pinned to 17a9f07487)

Solutions

  1. Guard that newWidth > 0 && newHeight > 0 before calling Resize; if zero, fall back to scanning the original buffer.
  2. Normalize the source bitmap to a scaler-friendly color type (e.g. SKColorType.Bgra8888) before resizing.
  3. Catch a null resized result and fall back to ScanByData on the original bitmapToProcess buffer instead of throwing.
  4. Cap incoming image dimensions earlier in the pipeline to reduce resize pressure.

Example fix

// before
using var resized = bitmap.Resize(new SKImageInfo(newWidth, newHeight), sampling);
if (resized == null) throw new Exception("Could not resize image");

// after
if (newWidth <= 0 || newHeight <= 0)
    return await ScanByData(bitmapToProcess.ToPngArray(), finalMime, new[] { originalWidth, originalHeight }, twoLetterLanguageCode);
var normalized = bitmap.ColorType == SKColorType.Bgra8888 ? bitmap : bitmap.Copy(new SKImageInfo(bitmap.Width, bitmap.Height, SKColorType.Bgra8888));
using var resized = normalized.Resize(new SKImageInfo(newWidth, newHeight), sampling);
if (resized == null)
    return await ScanByData(bitmapToProcess.ToPngArray(), finalMime, new[] { originalWidth, originalHeight }, twoLetterLanguageCode);
Defensive patterns

Strategy: validation

Validate before calling

if (newWidth <= 0 || newHeight <= 0)
    return await ScanByData(bitmapToProcess.ToPngArray(), finalMime, new[] { originalWidth, originalHeight }, twoLetterLanguageCode);
if (bitmap.ColorType != SKColorType.Bgra8888)
    bitmap = bitmap.Copy(new SKImageInfo(bitmap.Width, bitmap.Height, SKColorType.Bgra8888));

Try / catch

try { return await lens.ScanByBitmap(bitmap, lang); }
catch (Exception ex) when (ex.Message == "Could not resize image")
{
    return await lens.ScanByData(bitmap.ToPngArray(), "image/png", new[] { bitmap.Width, bitmap.Height }, lang);
}

Prevention

When it happens

Trigger: ScanByBitmap is called with a bitmap whose width or height exceeds 1200. The computed ratio yields newWidth/newHeight that are 0 (extreme aspect/underflow), the source SKColorType is one Skia's scaler rejects (vendor/unknown/alpha-only), or the process is low on memory when allocating the destination bitmap.

Common situations: Screenshots or photos decoded from HEIF/RAW with unusual color types, very large source images on memory-constrained machines, or a SkiaSharp version regression in the bitmap scaler path.

Related errors


AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13). Data as JSON: /api/errors/6caa1e2be3dcea23. Report an issue: GitHub.