{"record":{"id":"2c5b62fb391b73ad","repo":"microsoft/ailab","slug":"invalid-image-dimensions","errorCode":null,"errorMessage":"Invalid image dimensions","messagePattern":"Invalid image dimensions","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Sketch2Code/Sketch2Code.Core/Services/ObjectDetectionAppService.cs","lineNumber":191,"sourceCode":"            return predictedObject;\n        }\n\n        private Image buildAndVerifyImage(byte[] data)\n        {\n            double imageWidth = 0;\n            double imageHeight = 0;\n            Image img;\n\n            using (var ms = new MemoryStream(data))\n            {\n                img = Image.FromStream(ms);\n\n                imageWidth = img.Width;\n                imageHeight = img.Height;\n\n                if ((imageWidth == 0) || (imageHeight == 0))\n                {\n                    throw new InvalidOperationException(\"Invalid image dimensions\");\n                }\n            }\n\n            return img;\n        }\n\n        public async Task SaveResults(IList<PredictedObject> predictedObjects, string id)\n        {\n            if (_cloudBlobClient == null) throw new InvalidOperationException(\"blobClient is null\");\n            var slices_container = $\"{id}/slices\";\n\n            for (int i = 0; i < predictedObjects.Count; i++)\n            {\n                PredictedObject result = (PredictedObject)predictedObjects[i];\n                await this.SaveResults(result.SlicedImage, slices_container, $\"{result.Name}.png\");\n            }\n        }\n        public async Task SaveResults(byte[] file, string container, string fileName)","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/microsoft/ailab/blob/89fe2fc62081145ec5408d42059cbcb7c4a033c8/Sketch2Code/Sketch2Code.Core/Services/ObjectDetectionAppService.cs#L173-L209","documentation":"buildAndVerifyImage validates an image before object detection and throws InvalidOperationException when the decoded image reports a width or height of 0. A bitmap with zero dimensions cannot be processed by the detection pipeline, so the service rejects it. This normally indicates the image bytes did not decode into a valid bitmap.","triggerScenarios":"Calling the analysis API (image flow) with a byte array that decodes to a System.Drawing.Image whose Width or Height is 0 — e.g. corrupted/truncated image data or a degenerate bitmap.","commonSituations":"Uploading a partially downloaded/corrupt file; saving an image stream incorrectly (stream position not reset, producing a 0x0 bitmap); passing non-image bytes that a loose decoder path still accepted; generating images programmatically with uninitialized dimensions.","solutions":["Verify the uploaded file is a valid, complete image and re-encode/re-upload it","Check the code that produces the image bytes (reset MemoryStream.Position = 0 before creating the bitmap)","Add a caller-side dimension check and reject zero-dimension images with a friendly message before calling the service","Open the image locally (System.Drawing or an image viewer) to confirm it decodes with non-zero dimensions"],"exampleFix":"// before\nvar result = await service.GetPredictionAsync(imageBytes);\n// after\nusing var check = new System.Drawing.Bitmap(new MemoryStream(imageBytes));\nif (check.Width == 0 || check.Height == 0) throw new ArgumentException(\"Image has zero dimensions\");\nvar result = await service.GetPredictionAsync(imageBytes);","handlingStrategy":"try-catch","validationCode":"using var probe = System.Drawing.Image.FromStream(new MemoryStream(imageBytes));\nif (probe.Width == 0 || probe.Height == 0)\n    throw new ArgumentException(\"Uploaded image has zero dimensions\");","typeGuard":"bool HasValidDimensions(System.Drawing.Image img) => img != null && img.Width > 0 && img.Height > 0;","tryCatchPattern":"try\n{\n    var result = await service.GetPredictionAsync(imageBytes);\n}\ncatch (InvalidOperationException ex) when (ex.Message == \"Invalid image dimensions\")\n{\n    return BadRequest(\"The uploaded file is not a valid image with non-zero dimensions.\");\n}","preventionTips":["Validate image files (size, magic bytes, decodability) at upload time","Reset MemoryStream.Position = 0 after writing before constructing a Bitmap","Reject truncated uploads with checksum/length checks","Log the image metadata (width/height) to diagnose corrupt inputs"],"tags":["csharp","image-processing","validation","sketch2code"],"backgroundTag":"invalid-argument-value","analyzedSha":"89fe2fc62081145ec5408d42059cbcb7c4a033c8","analyzedAt":"2026-09-13T20:10:53.835Z","contentChangedAt":"2026-09-13T20:10:53.835Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}