Search code examples
c#arraysgrasshopper

"Parameter must be positive and < Height. Parameter name: y" error


I am developing a component for Grasshopper 3D, which is a Rhino (architecture) plugin.

Using the Render() method, this draws a heatmap image onto the canvas. Isolating my other methods and constructors, I highly believe this method is causing my issue.

protected override void Render(Grasshopper.GUI.Canvas.GH_Canvas canvas, Graphics graphics, Grasshopper.GUI.Canvas.GH_CanvasChannel channel) {
    // Render the default component.
    base.Render(canvas, graphics, channel);

    // Now render our bitmap if it exists.
    if (channel == Grasshopper.GUI.Canvas.GH_CanvasChannel.Wires) {
        KT_HeatmapComponent comp = Owner as KT_HeatmapComponent;
        if (comp == null)
            return;

        List<HeatMap> maps = comp.CachedHeatmaps;
        if (maps == null)
            return;

        if (maps.Count == 0)
            return;

        int x = Convert.ToInt32(Bounds.X + Bounds.Width / 2);
        int y = Convert.ToInt32(Bounds.Bottom + 10);

        for (int i = 0; i < maps.Count; i++) {
            Bitmap image = maps[i].Image;
            if (image == null)
                continue;

            Rectangle mapBounds = new Rectangle(x, y, maps[i].Width * 10, maps[i].Height * 10);
            mapBounds.X -= mapBounds.Width / 2;

            Rectangle edgeBounds = mapBounds;
            edgeBounds.Inflate(4, 4);

            GH_Capsule capsule = GH_Capsule.CreateCapsule(edgeBounds, GH_Palette.Normal);
            capsule.Render(graphics, Selected, false, false);
            capsule.Dispose();

            // Unnecessary graphics.xxxxxx methods and parameters

            y = edgeBounds.Bottom + 10;
        }
    }
}

The error I receive when I attempt to render things onto my canvas is:

1. Solution exception:Parameter must be positive and < Height.
Parameter name: y

From my research, it appears to happen the most when you encounter array overflows.

My research links:

  1. http://www.codeproject.com/Questions/158055/Help-in-subtraction-of-two-images

  2. Exception on traveling through pixels BMP C#

  3. http://www.c-sharpcorner.com/Forums/Thread/64792/

However, the above examples apply mostly to multi-dimensional arrays, while I have a one-dimensional.

I was wondering if anyone else has had this issue before, and could give me some pointers and guidance?

Thanks.


Solution

  • int x = Convert.ToInt32(Bounds.X + Bounds.Width / 2);
    int y = Convert.ToInt32(Bounds.Bottom + 10);
    

    Your error is telling you that y must be less than the height, but you are setting it to 10 more than your height because you are adding to the Bounds.Bottom.

    maps[i].Height * 10
    

    You also need to make sure that your calculated Height is what you think it is and compare that y is valid against it.