{"record":{"id":"0775afcd53a4021a","repo":"mxgmn/WaveFunctionCollapse","slug":"error-wrong-image-width-height-width-hei","errorCode":null,"errorMessage":"ERROR: wrong image width * height = {width} * {height}","messagePattern":"ERROR: wrong image width \\* height = (.+?) \\* (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Helper.cs","lineNumber":58,"sourceCode":"    }\n\n    public static IEnumerable<XElement> Elements(this XElement xelement, params string[] names) => xelement.Elements().Where(e => names.Any(n => n == e.Name));\n}\n\nstatic class BitmapHelper\n{\n    public static (int[], int, int) LoadBitmap(string filename)\n    {\n        using var image = Image.Load<Bgra32>(filename);\n        int width = image.Width, height = image.Height;\n        int[] result = new int[width * height];\n        image.CopyPixelDataTo(MemoryMarshal.Cast<int, Bgra32>(result.AsSpan()));\n        return (result, width, height);\n    }\n\n    unsafe public static void SaveBitmap(int[] data, int width, int height, string filename)\n    {\n        if (width <= 0 || height <= 0 || data.Length != width * height) throw new Exception($\"ERROR: wrong image width * height = {width} * {height}\");\n\n        Span<Bgra32> pixelSpan = MemoryMarshal.Cast<int, Bgra32>(data.AsSpan());\n        using var image = Image.LoadPixelData<Bgra32>(pixelSpan.ToArray(), width, height);\n        image.SaveAsPng(filename);\n    }\n}\n","sourceCodeStart":40,"sourceCodeEnd":65,"githubUrl":"https://github.com/mxgmn/WaveFunctionCollapse/blob/de7d22e705e816b62b4d613199d0463820fcaef3/Helper.cs#L40-L65","documentation":"SaveBitmap validates its inputs before encoding the pixel buffer to a PNG via ImageSharp. This Exception is thrown when the dimensions are non-positive or when the data array length does not exactly equal width * height, i.e. the buffer cannot represent a complete rectangular image of the stated size. It is a fail-fast guard against writing a corrupt or truncated PNG.","triggerScenarios":"Calling Helper.SaveBitmap with width <= 0 or height <= 0, or with a data array whose Length != width * height (e.g. a cropped/padded buffer, a row-stride mismatch where the buffer includes stride padding, or dimensions read from a different source than the pixel data).","commonSituations":"Computing width/height from a bitmap header while the copied pixel region was clipped or scaled; including row-alignment padding bytes in the array; off-by-one or integer-division errors when deriving dimensions; passing a partially filled buffer after a failed CopyPixelDataTo; passing 0 dimensions for an empty/hidden window or zero-sized render target.","solutions":["Log data.Length, width, and height at the call site and verify data.Length == width * height before calling SaveBitmap; fix whichever value is wrong.","Check whether the source buffer includes stride/row padding; if so, copy only the actual pixel bytes row by row into a tightly packed int[] of width*height.","Verify width/height come from the same image/region as the pixel data (e.g. re-read them from the same Bitmap object used for CopyPixelDataTo).","If the image can legitimately be empty, guard the call site and skip saving instead of calling SaveBitmap with 0 dimensions.","Increase the destination buffer size if a partial copy left it short (result array must be width*height*4 bytes)."],"exampleFix":"// before\nHelper.SaveBitmap(pixels, headerWidth, data.Length / headerHeight, \"out.png\");\n// after\nint width = bitmap.PixelSize.Width;\nint height = bitmap.PixelSize.Height;\nif (pixels.Length == width * height)\n    Helper.SaveBitmap(pixels, width, height, \"out.png\");","handlingStrategy":"validation","validationCode":"bool CanSave(int[] data, int width, int height) => width > 0 && height > 0 && data != null && data.Length == checked(width * height);\nif (!CanSave(data, width, height)) throw new ArgumentException($\"data.Length={data?.Length ?? -1} != {width}*{height}\");","typeGuard":"static bool IsValidBitmapArgs(int[] data, int width, int height) => data is not null && width > 0 && height > 0 && data.Length == width * height;","tryCatchPattern":"try\n{\n    Helper.SaveBitmap(data, width, height, path);\n}\ncatch (Exception ex) when (ex.Message.StartsWith(\"ERROR: wrong image width\"))\n{\n    // log width/height/data.Length and fix the buffer/dimension mismatch\n}","preventionTips":["Always derive width and height from the same object that produced the pixel data.","Keep pixel buffers tightly packed (no stride padding) when handing them to SaveBitmap.","Assert data.Length == width * height in debug builds before saving.","Validate dimensions are positive before allocating or copying pixel buffers.","Wrap save logic in a helper that checks IsValidBitmapArgs first."],"tags":["argument-validation","image","bitmap","csharp","dimensions-mismatch"],"backgroundTag":"invalid-image-dimensions","analyzedSha":"de7d22e705e816b62b4d613199d0463820fcaef3","analyzedAt":"2026-08-30T23:12:44.027Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}