babalae/better-genshin-impact · warning · Exception
ReadImageMatWithResizeSync: 宽度和高度必须为正数
Error message
ReadImageMatWithResizeSync: 宽度和高度必须为正数
What it means
Thrown by ReadImageMatWithResizeSync when width or height is less than or equal to zero. The method resizes an image to the specified dimensions using OpenCV's Cv2.Resize, which requires positive pixel dimensions. Note: the outer try/catch in this method catches ALL exceptions (including this one) and returns an empty Mat, so the throw is logged but the exception does not propagate to the caller.
Source
Thrown at BetterGenshinImpact/Core/Script/Dependence/LimitedFile.cs:255
/// <returns>调整尺寸后的Mat图像对象</returns>
/// <remarks>
/// 支持的插值算法:
/// <list type="bullet">
/// <item><description>最近邻插值 (0)</description></item>
/// <item><description>双线性插值 (1) - 默认</description></item>
/// <item><description>双三次插值 (2)</description></item>
/// <item><description>像素区域关系重采样 (3)</description></item>
/// <item><description>Lanczos插值 (4)</description></item>
/// <item><description>精确双线性插值 (5)</description></item>
/// </list>
/// </remarks>
public Mat ReadImageMatWithResizeSync(string path, double width, double height, int interpolation = 1)
{
try
{
if (width <= 0 || height <= 0)
{
throw new Exception("ReadImageMatWithResizeSync: 宽度和高度必须为正数");
}
if (interpolation is < 0 or > 5)
{
throw new Exception($"ReadImageMatWithResizeSync: 不支持的插值算法 {interpolation}");
}
path = NormalizePath(path);
using var stream = File.OpenRead(path);
using var mat = Mat.FromStream(stream, ImreadModes.Color);
var rsz = new Mat();
Cv2.Resize(mat, rsz, new Size(width, height), 0, 0, (InterpolationFlags)interpolation);
return rsz;
}
catch (Exception ex)
{
// 记录异常并返回空的Mat
TaskControl.Logger.LogError("ReadImageMatWithResizeSync 异常: {Message}", ex.Message);View on GitHub (pinned to a7cb36712d)
Solutions
- Validate width > 0 && height > 0 before calling ReadImageMatWithResizeSync.
- Ensure any scale factor is positive: if (scale <= 0) return; before computing dimensions.
- Check the returned Mat for emptiness (mat.Empty()) after the call to detect swallowed errors.
- Use Math.Max(1, computedSize) as a floor to prevent zero/negative dimensions.
Example fix
// before
var mat = limitedFile.ReadImageMatWithResizeSync(path, srcWidth * ratio, srcHeight * ratio);
// if ratio is 0, this throws internally but returns empty Mat
// after
var targetW = Math.Max(1, (int)(srcWidth * ratio));
var targetH = Math.Max(1, (int)(srcHeight * ratio));
var mat = limitedFile.ReadImageMatWithResizeSync(path, targetW, targetH);
if (mat.Empty())
_logger.LogWarning("Image resize produced empty Mat for {Path}", path); Defensive patterns
Strategy: validation
Validate before calling
// Validate dimensions before calling ReadImageMatWithResizeSync
if (width <= 0 || height <= 0)
throw new ArgumentException("Width and height must be positive");
var mat = limitedFile.ReadImageMatWithResizeSync(path, width, height, interpolation);
// Also check the returned Mat — the internal catch swallows the exception
if (mat.Empty())
_logger.LogWarning("ReadImageMatWithResizeSync returned empty Mat for {Path}", path); Prevention
- Validate width > 0 and height > 0 before calling.
- Use Math.Max(1, computedDimension) as a floor for computed sizes.
- Always check mat.Empty() after the call — the exception is swallowed internally.
- Log the path and parameters when an empty Mat is returned to aid debugging.
When it happens
Trigger: Calling ReadImageMatWithResizeSync(path, width, height) with width <= 0 or height <= 0. This can happen when dimensions are computed from a ratio that produces zero or negative values, or when a default/uninitialized value is passed.
Common situations: Script computes target size as (originalWidth * scale) where scale is 0 or negative. Uninitialized dimension variable defaults to 0. Division-based size calculation where the divisor produces a non-positive result. The error is logged but silently swallowed — the caller receives an empty Mat and may see downstream failures from empty-image operations.
Related errors
- 图像通道数必须为3,当前为{channels}
- {fieldName} 必须是 1 到 4 个数字
- ReadImageMatWithResizeSync: 不支持的插值算法 {interpolation}
- 切换角色:角色列表队伍识别不完整,期望 {expectedTeamCount} 个,末次检测到 {lastDetecte
- milliseconds 不能小于 0
AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13).
Data as JSON: /api/errors/03e34a93abdcceaf.
Report an issue: GitHub.