You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
'Constructor on type 'System.Drawing.Image' not found' crash occurs when trying to add Items in ImageCollection Editor in .NET Core project #10549
Microsoft Visual Studio Professional 2022 (64-bit) - Current
Version 17.6.2
.NET version
Net 7.0
Did this work in a previous version of Visual Studio and/or previous .NET release?
No
Issue description
When attempting to add an item as an image in the ImageCollection Editor through the Properties window in the .NET Core project, a 'Constructor on type 'System.Drawing.Image' not found' crash occurs. However, this issue is not present in the .NET Framework project. Is there any way to resolve this.
Steps to reproduce
Run the sample.
Add the ImageListExt Control (UserControl) from the Toolbox to the Form.
On the Form designer page, right-click on the control to open the properties window.
Click on the MyItems ImageCollection property to edit the ImageCollection.
Then, click on the "Add" button to add an item to the "Image Collection Editor."
Note that the "Constructor on type 'System.Drawing.Image' not found" crash will occur when attempting to add the item in the "Image Collection Editor."
@BagavathiPerumalA I tested the application you provided, it can repro this issue, which also reproduce in Winforms .Net framework application.
.NET 7.0 or higher version:
.Net framework 4.8.1:
And I did some search: make your ImageCollection editable in the Visual Studio Properties window, you need to implement a custom TypeConverter for the ImageCollection class.
Here is a sample application, please refer to. WinFormsApp23.zip
@RaghavendraSudhakar -
I modified your sample program a little because it wasn't working when retargetting to .NET Framework 4.8.1 either. The main change is that I added a CreateInstance
override to your custom collection editor, because Image type indeed can't be created automatically because it's abstract, as the error in the 4.8.1 case shows. I also changed the collection type to a standard one, but that is not critical to this scenario. Here is the modified sample - ImageCollection-Net481.zip
The same CollectionEditor code will not work with the .NET 8 or 9 designer because .NET designer runs in 2 processes. Collection editor form runs in the Visual Studio process, but images, that it creates, are instantiated in the server process in CollectionEditor responsible for the business logic. See Custom Controls for WinForm's Out-Of-Process Designer - .NET Blog (microsoft.com) for details.
To support designer that runs across 2 processes, you must separate your collection editor into parts that run in the VisualStudio and parts that run the DesignToolsserver process, and then package this collection editor with your custom control into a nuget package. Here is what I did:
1. Move your control into a control library
2. Add designer component(binary) that loads into the VisualStudio process, add the client part of Collection editor to the new binary and re-route editor type. This binary should reference https://www.nuget.org/packages/Microsoft.WinForms.Designer.SDK and use CollectionEditor base class from there.
3. Add designer component (binary) that loads into the DesignToolsServer, It contains part on your collection editor that creates new item instances. Reference Microsoft.DotNet.DesignTools.Editors; namespace instead of System.Component.Model.
4. Package the design time binaries(dlls) and the control library into a nuget package; reference that package in the test app; and set up nuget.config to point to the output directory where that package is built.
5. Each time you rebuild your package, its version is updated. However becasue nuge caches the previous version, you have to manually update references in your test application (right click in the solution exploded on packages node and select "update" option)
Activity
elachlan commentedon Dec 30, 2023
@Olina-Zhang can your team please test this?
Olina-Zhang commentedon Jan 2, 2024
@BagavathiPerumalA I tested the application you provided, it can repro this issue, which also reproduce in Winforms .Net framework application.


.NET 7.0 or higher version:
.Net framework 4.8.1:
And I did some search: make your ImageCollection editable in the Visual Studio Properties window, you need to implement a custom TypeConverter for the ImageCollection class.
Here is a sample application, please refer to.
WinFormsApp23.zip
elachlan commentedon Jan 2, 2024
Closing this issue as @Olina-Zhang has provided the appropriate solution.
BagavathiPerumal commentedon Jan 24, 2024
We are still experiencing the same issue while using below code in your shared sample. How to resolve this issue.
WinFormsApp-ImageCollection.zip
RaghavendraSudhakar commentedon Feb 7, 2024
@elachlan We are still facing issue after using your code in our sample
WinFormsApp-ImageCollection.zip
elachlan commentedon Feb 7, 2024
@lonitra can you please reopen this? I don't have correct permissions.
Tanya-Solyanik commentedon Feb 20, 2024
@RaghavendraSudhakar -
I modified your sample program a little because it wasn't working when retargetting to .NET Framework 4.8.1 either. The main change is that I added a CreateInstance
override to your custom collection editor, because
Imagetype indeed can't be created automatically because it's abstract, as the error in the 4.8.1 case shows. I also changed the collection type to a standard one, but that is not critical to this scenario. Here is the modified sample - ImageCollection-Net481.zipThe same CollectionEditor code will not work with the .NET 8 or 9 designer because .NET designer runs in 2 processes. Collection editor form runs in the Visual Studio process, but images, that it creates, are instantiated in the server process in CollectionEditor responsible for the business logic. See Custom Controls for WinForm's Out-Of-Process Designer - .NET Blog (microsoft.com) for details.
To support designer that runs across 2 processes, you must separate your collection editor into parts that run in the VisualStudio and parts that run the DesignToolsserver process, and then package this collection editor with your custom control into a nuget package. Here is what I did:
1. Move your control into a control library
2. Add designer component(binary) that loads into the VisualStudio process, add the client part of Collection editor to the new binary and re-route editor type. This binary should reference https://www.nuget.org/packages/Microsoft.WinForms.Designer.SDK and use CollectionEditor base class from there.
3. Add designer component (binary) that loads into the DesignToolsServer, It contains part on your collection editor that creates new item instances. Reference Microsoft.DotNet.DesignTools.Editors; namespace instead of System.Component.Model.
4. Package the design time binaries(dlls) and the control library into a nuget package; reference that package in the test app; and set up
nuget.configto point to the output directory where that package is built.5. Each time you rebuild your package, its version is updated. However becasue nuge caches the previous version, you have to manually update references in your test application (right click in the solution exploded on packages node and select "update" option)
ImageCollection-Net9.zip