System.Drawing.Bitmap Icon + .NET 7.0 - Rhino 8 Add-On / Plug-In / C#

Hi, I am currently converting a few developed C# components into an add-on and would like to make this available. The components work, but unfortunately the icons are not displayed.

A test icon is 24x24px in size and has a bit depth of 32 and is exported in the PNG file format. I inserted it via “Resources.resx”.

Reference:
“protected override System.Drawing.Bitmap Icon => Properties.Resources.Test_1;”

Error CS0029:
“The type ‘byte’ cannot be implicitly converted to ‘System.Drawing.Bitmap’.”

I have read that it is due to .NET 7.0 and that it is possible with System.Drawing.Common, for example, but it is only a solution for Windows and cannot be used for Mac. What is the best solution to make it compatible for all systems?

In other words, how can icons for Grasshopper add-ons (Rhino 8 / .NET 7.0) be embedded?

Sorry for the stupid question and many thanks in advance!

1 Like

Hi Im having the same issue, did you figure it out?

I’m currently in the process of creating the icons. I’ll test it in about a week and can let you know then :slightly_smiling_face:

1 Like

Found this forum post:

“System.Drawing.Common” should therefore work on both platforms.

Hi! yes let me know please if it works, i have found a solution that worked for me:

protected override System.Drawing.Bitmap Icon
{
get
{
var iBytes = Project.Properties.Resources.image;
using (MemoryStream memS = new MemoryStream(iBytes))
{
System.Drawing.Bitmap image = new System.Drawing.Bitmap(memS);
return image;
}
}

However, I am developing the plugin for Rhino 7 targeting .Net framework so i don’t understand why i need to use this workaround which is for .Net core.
In any case, it worked. I uploaded the png as image type in the Resources folder.

I hope this is useful for you!

If only net48 is targeted and the image files are loaded into the project, then they are saved as byte[ ] and these can be converted to bitmap via MemoryStream, as you have described.

If net48 and net7 are targeted and System.Drawing.Common is not yet installed and then the image files are loaded into the project, they are also saved as byte[ ].

However, if System.Drawing.Common is installed and then image files are loaded into the project, they are saved as bitmaps, so MemoryStream is not needed, but System.Resources.Extensions-Assembly is now required.

Is it legitimate to always remove System.Drawing.Common before adding an image file to the project so that it is loaded as byte[ ] and then reinstall System.Drawing.Common? :smiley:

1 Like

Thanks for clarifying this!! i will explore these options with targetting net 48 and net7 just to get a further understanding of this :slight_smile:

Thanks, this workaround works for me! I am developing a plugin targeting Rhino7 and faced the same problem.
It would be nice if there was an easier way.
System.Drawing.Common does not work for me.

1 Like