{"record":{"id":"03e34a93abdcceaf","repo":"babalae/better-genshin-impact","slug":"readimagematwithresizesync","errorCode":null,"errorMessage":"ReadImageMatWithResizeSync: 宽度和高度必须为正数","messagePattern":"ReadImageMatWithResizeSync: 宽度和高度必须为正数","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"warning","filePath":"BetterGenshinImpact/Core/Script/Dependence/LimitedFile.cs","lineNumber":255,"sourceCode":"    /// <returns>调整尺寸后的Mat图像对象</returns>\n    /// <remarks>\n    /// 支持的插值算法：\n    /// <list type=\"bullet\">\n    /// <item><description>最近邻插值 (0)</description></item>\n    /// <item><description>双线性插值 (1) - 默认</description></item>\n    /// <item><description>双三次插值 (2)</description></item>\n    /// <item><description>像素区域关系重采样 (3)</description></item>\n    /// <item><description>Lanczos插值 (4)</description></item>\n    /// <item><description>精确双线性插值 (5)</description></item>\n    /// </list>\n    /// </remarks>\n    public Mat ReadImageMatWithResizeSync(string path, double width, double height, int interpolation = 1)\n    {\n        try\n        {\n            if (width <= 0 || height <= 0)\n            {\n                throw new Exception(\"ReadImageMatWithResizeSync: 宽度和高度必须为正数\");\n            }\n\n            if (interpolation is < 0 or > 5)\n            {\n                throw new Exception($\"ReadImageMatWithResizeSync: 不支持的插值算法 {interpolation}\");\n            }\n\n            path = NormalizePath(path);\n            using var stream = File.OpenRead(path);\n            using var mat = Mat.FromStream(stream, ImreadModes.Color);\n            var rsz = new Mat();\n            Cv2.Resize(mat, rsz, new Size(width, height), 0, 0, (InterpolationFlags)interpolation);\n            return rsz;\n        }\n        catch (Exception ex)\n        {\n            // 记录异常并返回空的Mat\n            TaskControl.Logger.LogError(\"ReadImageMatWithResizeSync 异常: {Message}\", ex.Message);","sourceCodeStart":237,"sourceCodeEnd":273,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/Core/Script/Dependence/LimitedFile.cs#L237-L273","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar mat = limitedFile.ReadImageMatWithResizeSync(path, srcWidth * ratio, srcHeight * ratio);\n// if ratio is 0, this throws internally but returns empty Mat\n\n// after\nvar targetW = Math.Max(1, (int)(srcWidth * ratio));\nvar targetH = Math.Max(1, (int)(srcHeight * ratio));\nvar mat = limitedFile.ReadImageMatWithResizeSync(path, targetW, targetH);\nif (mat.Empty())\n    _logger.LogWarning(\"Image resize produced empty Mat for {Path}\", path);","handlingStrategy":"validation","validationCode":"// Validate dimensions before calling ReadImageMatWithResizeSync\nif (width <= 0 || height <= 0)\n    throw new ArgumentException(\"Width and height must be positive\");\nvar mat = limitedFile.ReadImageMatWithResizeSync(path, width, height, interpolation);\n\n// Also check the returned Mat — the internal catch swallows the exception\nif (mat.Empty())\n    _logger.LogWarning(\"ReadImageMatWithResizeSync returned empty Mat for {Path}\", path);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["validation","image-processing","opencv","resize","swallowed-exception"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}