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

6.4k views Asked by At

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.

2

There are 2 answers

1
TyCobb On BEST ANSWER
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.

0
majixin On

When writing a plugin (VS extension), it is possible to get this problem without changing code. I cannot find any examples of what I am going to answer here for any other commentary on this particular issue.

Here is some build output I received (literally today):

>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\VisualStudio\v16.0\VSSDK\Microsoft.VisualStudio.Sdk.Common.targets(68,5): error : Parameter must be positive and < Height.
>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\VisualStudio\v16.0\VSSDK\Microsoft.VisualStudio.Sdk.Common.targets(68,5): error : Parameter name: y
========== Build: 8 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

and the Microsoft.VisualStudio.Sdk.Common.targets file is unmodified. There is no file in the usual source code that is doing anything graphical as many answers online allude to.

So what did I change to have this start happening?

First, I was updating a VS extension and adding some image files to one of the contained project's icons folder in order to be able to use them within the custom menus.

To do that, I correctly made the required changes within the VSCT file, so that some of these images would display along side custom menu items at runtime.

But the above build error kept getting spewed out.

My original images were provided to me in rather large sizes as SVG files. I resized these using Gimp and exported them to PNG format. (This is important, as will be revealed later).

To debug, I then commented out changes in the VSCT file until the extension built correctly again.

I then re-introduced my changes one by one, starting with <GuidSymbol> entries, until the build failed with the same issue.

When uncommenting one of the <Bitmap> entries, the build failed again. I then changed the filename for that entry to a known 'good' image file being used elsewhere in the extension. And voila! It built again :)

I concluded something must therefore be wrong with the image file.

So I went back to Gimp, loaded the large SVG, resized the image and re-exported as a 16x16 PNG a second time. The same problem persisted.

So I went back to Gimp, loaded the large SVG, resized the image and noted that there is also an option to "Resize Canvas..." and I used this to ensure it was the intended 16x16 size, then re-exported a third time to PNG format.

This time the image was OK when referenced in the VS extension and permitted a successful build.

So if you find yourself having made no changes like all the other answers online suggest, and you are writing/updating a VS extension, then the problem is likely with one or more images you are introducing to your project. I used Gimp and I assume some other image editors will equally embed data in the PNG that may not be acceptable to the MS Build tools. In my particular case, the 'image' size and 'canvas' size both had to be 16x16 to permit a successful build when the VSCT referenced the image files.