TheAlgorithms/C-Sharp · error

should be greater than zero

Error message

{nameof(bitmapHeight)} should be greater than zero

What it means

Mandelbrot.GetBitmap validates bitmapHeight after bitmapWidth and throws ArgumentOutOfRangeException when bitmapHeight <= 0. A non-positive height cannot back an SKBitmap, so the render is aborted before allocation.

Solutions

  1. Pass a bitmapHeight > 0 (e.g. 600)
  2. Validate height before the call and substitute a default
  3. Check that any aspect-ratio/computed height cannot round or evaluate to 0 or below

Example fix

// before
var bmp = Mandelbrot.GetBitmap(800, computedHeight);
// after
var bmp = Mandelbrot.GetBitmap(800, Math.Max(1, computedHeight));
Defensive patterns

Strategy: validation

Validate before calling

if (bitmapHeight <= 0) throw new ArgumentException("bitmapHeight must be positive", nameof(bitmapHeight));

Type guard

static bool IsValidDimension(int d) => d > 0;

Try / catch

try { var bmp = Mandelbrot.GetBitmap(width, height); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "bitmapHeight")
{
    // retry with default height
}

Prevention

When it happens

Trigger: Calling Algorithms.Other.Mandelbrot.GetBitmap with bitmapHeight <= 0, e.g. GetBitmap(800, 0) or GetBitmap(800, -50).

Common situations: Height computed from aspect-ratio math that divided by or multiplied into 0; UI layout reporting zero height at startup; config value missing and defaulting to 0.

Related errors


AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13). Data as JSON: /api/errors/3a167849b547eb71. Report an issue: GitHub.

Appendix: source

Thrown at Algorithms/Other/Mandelbrot.cs:61

    public static SKBitmap GetBitmap(
        int bitmapWidth = 800,
        int bitmapHeight = 600,
        double figureCenterX = -0.6,
        double figureCenterY = 0,
        double figureWidth = 3.2,
        int maxStep = 50,
        bool useDistanceColorCoding = true)
    {
        if (bitmapWidth <= 0)
        {
            throw new ArgumentOutOfRangeException(
                nameof(bitmapWidth),
                $"{nameof(bitmapWidth)} should be greater than zero");
        }

        if (bitmapHeight <= 0)
        {
            throw new ArgumentOutOfRangeException(
                nameof(bitmapHeight),
                $"{nameof(bitmapHeight)} should be greater than zero");
        }

        if (maxStep <= 0)
        {
            throw new ArgumentOutOfRangeException(
                nameof(maxStep),
                $"{nameof(maxStep)} should be greater than zero");
        }

        var bitmap = new SKBitmap(bitmapWidth, bitmapHeight);
        var figureHeight = figureWidth / bitmapWidth * bitmapHeight;

        // loop through the bitmap-coordinates
        for (var bitmapX = 0; bitmapX < bitmapWidth; bitmapX++)
        {
            for (var bitmapY = 0; bitmapY < bitmapHeight; bitmapY++)

View on GitHub (pinned to 96e2905cab)