SubtitleEdit/subtitleedit · error · InvalidOperationException
Could not resize image
Error message
Could not resize image
What it means
Thrown by Helper.ResizeImageAsync when SKBitmap.Resize returns null. The source decoded fine, but the resize operation (using SKSamplingOptions with Linear filter + Linear mipmap) failed to produce a destination bitmap — typically due to a degenerate target size (newWidth/newHeight computed as 0), an unsupported source color type, or a native resize failure.
Source
Thrown at src/ui/Logic/Ocr/GoogleLens/Helper.cs:32
using var original = SKBitmap.Decode(inputStream);
if (original == null)
{
throw new InvalidOperationException("Could not decode image");
}
// Calculate new dimensions maintaining aspect ratio
float ratioX = (float)maxWidth / original.Width;
float ratioY = (float)maxHeight / original.Height;
float ratio = Math.Min(ratioX, ratioY);
int newWidth = (int)(original.Width * ratio);
int newHeight = (int)(original.Height * ratio);
using var resized = original.Resize(new SKImageInfo(newWidth, newHeight), new SKSamplingOptions(SKFilterMode.Linear, SKMipmapMode.Linear));
if (resized == null)
{
throw new InvalidOperationException("Could not resize image");
}
using var image = SKImage.FromBitmap(resized);
using var data = image.Encode(SKEncodedImageFormat.Jpeg, 90);
return await Task.FromResult(data.ToArray());
}
public static (int Width, int Height) ImageDimensionsFromData(byte[] data)
{
using var inputStream = new MemoryStream(data);
using var codec = SKCodec.Create(inputStream);
if (codec == null)
{
throw new InvalidOperationException("Could not decode image");
}
View on GitHub (pinned to 17a9f07487)
Solutions
- Clamp newWidth/newHeight to a minimum of 1 before resizing.
- Validate maxWidth/maxHeight > 0 and source dimensions > 0 up front.
- Skip resize if the source already fits within the target bounds.
- Fall back to SKImage resize or returning the original if bitmap resize fails.
Example fix
// before int newWidth = (int)(original.Width * ratio); int newHeight = (int)(original.Height * ratio); // after int newWidth = Math.Max(1, (int)(original.Width * ratio)); int newHeight = Math.Max(1, (int)(original.Height * ratio));
Defensive patterns
Strategy: validation
Validate before calling
int newWidth = Math.Max(1, (int)(original.Width * ratio));
int newHeight = Math.Max(1, (int)(original.Height * ratio));
if (newWidth < 1 || newHeight < 1) throw new ArgumentException("Target size too small"); Type guard
static bool IsValidResizeTarget(int w, int h) => w >= 1 && h >= 1;
Try / catch
try { var resized = await Helper.ResizeImageAsync(buffer, 1000, 1000); }
catch (InvalidOperationException ex) when (ex.Message == "Could not resize image") { /* fall back to original or different filter */ } Prevention
- Clamp computed dimensions to a minimum of 1.
- Validate maxWidth/maxHeight and source dimensions > 0.
- Skip resize when the source already fits.
- Fall back to SKImage-based resize on failure.
When it happens
Trigger: Resizing an image whose computed newWidth or newHeight rounds to 0 (very small source or extreme ratio), or a source bitmap whose color type SkiaSharp cannot resample. The integer cast (int)(width*ratio) can yield 0 for tiny inputs.
Common situations: A 1x1 or sub-1px source image; maxWidth/maxHeight passed as 0; a grayscale/alpha-only bitmap the resizer rejects; a SkiaSharp version-specific resize bug.
Related errors
- Could not decode image
- File type not supported
- Could not determine image dimensions
- Image dimensions are larger than 1000x1000
- Could not determine original image dimensions.
AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13).
Data as JSON: /api/errors/247e13412d71e9e6.
Report an issue: GitHub.