A bunch of Errors in Visual Studio

Hi, I’m a newbie in scripting in Visual Studio. I’ve done a very basic Centeroid Plugin but I can’t add the plugin icon. I wrote the resource.$(imageName) then i get error CS0029 Severity Code Description Project File Line Suppression State Error CS0029 Cannot implicitly convert type ‘byte[]’ to ‘System.Drawing.Bitmap’ Att1_S (net48) C:\Users\szeng\source\repos\Att1_S\Att1_SComponent.cs 86 Active".

In addition to this, I cannot debug any code with .net 7. I could only debug with .net 4.8 Framework. Every time I debugged the project I always got an error and .gha file dropped in a “net48” folder in my projects debug folder.

I have attached the full list of errors and warnings below

Error_List.txt (3.0 KB)

PS: I don’t know what file format or information I need to include to help me, so please write if I need to include anything extra.

using Grasshopper;
using Grasshopper.Kernel;
using Rhino.Geometry;
using System;
using System.Collections.Generic;

namespace Att1_S
{
    public class Att1_SComponent : GH_Component
    {
        /// <summary>
        /// Each implementation of GH_Component must provide a public 
        /// constructor without any arguments.
        /// Category represents the Tab in which the component will appear, 
        /// Subcategory the panel. If you use non-existing tab or panel names, 
        /// new tabs/panels will automatically be created.
        /// </summary>
        public Att1_SComponent()
          : base("Centerroid", "Cntrd",
              "Centerroid",
              "Workshop", "Geometry")
        {
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddPointParameter("Points", "P", "Points", GH_ParamAccess.list);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {
            pManager.AddPointParameter("Centerroid", "C", "Centerroid", GH_ParamAccess.item);
            pManager.AddNumberParameter("Distances", "D", "Distances", GH_ParamAccess.list);
        }

        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            List<Point3d> points = new List<Point3d>();
            DA.GetDataList(0, points);

            Point3d centerroid = new Point3d(0, 0, 0);

            for (int i = 0; i < points.Count; i++)
            {
                centerroid += points[i];
            }

            centerroid /= points.Count;

            List<double> distances = new List<double>();

            for (int i = 0; i < points.Count; i++)
            {
                distances.Add(centerroid.DistanceTo(points[i]));
            }

            DA.SetData(0, centerroid);
            DA.SetDataList(1, distances);
        }

        /// <summary>
        /// The Exposure property controls where in the panel a component icon 
        /// will appear. There are seven possible locations (primary to septenary), 
        /// each of which can be combined with the GH_Exposure.obscure flag, which 
        /// ensures the component will only be visible on panel dropdowns.
        /// </summary>
        public override GH_Exposure Exposure => GH_Exposure.primary;

        /// <summary>
        /// Provides an Icon for every component that will be visible in the User Interface.
        /// Icons need to be 24x24 pixels.
        /// You can add image files to your project resources and access them like this:
        /// return Resources.IconForThisComponent;
        /// </summary>
        /// 
        protected override System.Drawing.Bitmap Icon { get { return Properties.Resources.denme; } }

        /// <summary>
        /// Each component must have a unique Guid to identify it. 
        /// It is vital this Guid doesn't change otherwise old ghx files 
        /// that use the old ID will partially fail during loading.
        /// </summary>
        public override Guid ComponentGuid => new Guid("1600ff72-ff0d-46c6-8213-ec0b046d08c8");
    }
}

Did you add the Bitmap Icon to the Resources in Visual Studio?

In our project we have two icons, because we build our code for both Rhino 7 and Rhino 8, so in the code we switch between these two resource images:

image

This is the logic for our Skin for Rhino, but it will work the same for your plugin.

image

Look in your Solution Explorer for a Resource file:
image

Double-Click it to open the Resource file. Then drag your denme image onto the Resource window, and it will be added to the project.

Then you can refer to the resource from your code as you are above.

Yes, I’ve already added my 24*24 icon to my resources, but even so it doesn’t work.

A red line appears under the Bitmap and Icon and says not found in System.Drawing.

If you would like, I can send you the project file as a zip file for you to download.

You can send it to me here or at jason@Jewelbeetle3d.com and I will debug your issue

what happens when you add the Nuget Package
System.Drawing.Common
as described here ?
(a missing package would explain the error message and the different error for Framework 4.8 vs .Net 7)

Hi,

I added “System.Drawing.Common” on your suggestion, but this time a red line appeared under the icon I returned instead of Bitmap and Icon.

-----------------------------------------------------------------------------------------------------------------------------------------

I’ve discovered that by selecting Rhino 7 (with .Net Framework 4.8) in the Debug section during project creation, I can successfully debug and place my icon without the issues I was facing. Unfortunately, choosing Rhino 8 initially triggers these problems.

In the meantime, I am attaching the zip files I sent to Mr Stevenson. Perhaps there is something I have not explained or overlooked.

Att1_S (Rhino 8 Debug)
GHPlugin2S (Rhino 7 Debug)

Sorry not on a computer with VS at the moment.
…you might need to add the icon as icon (ico File) not as Image (png in your case).

see this topic
and this tutorial

good luck, good night - tom

Thank you very much, it works when I add the .ico file but I still can’t understand why I can add my image in the rhino 7 debug project but not in the rhino 8 debug project.

@Senih .Net Core handles the storing of images (apparently, I just learned this) as byte[] instead of as Bitmap type. Which you can see if we look at the generated code-behind file of the Resources for each of your two projects (.Net Framework/.Net Core):

.Net Framework returns Bitmap:
image

.Net Core returns Byte[]
image

So you will have to handle the image retrieval differently in the component class, here was your code that wasn’t working:

image

So if you instead modify the code as follows, it should work as expected:

image

NOTE: I had to modify the build step that copied the .gha so that it would work on my file system. I also recommend you wrap the build output parameter in double quotes, in case you run the solution from a path that contains spaces in the file path.

image

Here was the result after starting Rhino 8 with your component:

Thank you very much for your detailed response and getting back to me. Since I still cannot understand the logic of Rhino 8 with .Net Core (I am not very experienced in scripting), I will probably continue to improve with Rhino 7 and .Net Framework. I also want to thank you for the additional information in the note.

1 Like

if you want it as a bitmap instead of byte array, just change it in the Designer.cs
you will need System.Drawing.Common

I remember you can set the default preference somewhere, as all the current projects i’m working on defaults to bitmap, but i don’t rember how i did that

1 Like

Hi!
Sorry for the late reply, but I’ll definitely take a look at it. However, I have found the solution to the problem I mentioned the other day by moving forward with the .Net Framework. I would be happy to work with .Net Core for Rhino 8 if that would work in your direction.