Skip to content

'Constructor on type 'System.Drawing.Image' not found' crash occurs when trying to add Items in ImageCollection Editor in .NET Core project #10549

Closed

Description

@BagavathiPerumalA

Environment

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.

ImageCollectionEdit

Steps to reproduce

  1. Run the sample.
  2. Add the ImageListExt Control (UserControl) from the Toolbox to the Form.
  3. On the Form designer page, right-click on the control to open the properties window.
  4. Click on the MyItems ImageCollection property to edit the ImageCollection.
  5. Then, click on the "Add" button to add an item to the "Image Collection Editor."
  6. 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."

ImageListExt-NetCoreIssue

Diagnostics

No response

Activity

  1. added
    untriaged
    on Dec 29, 2023
  2. elachlan commented on Dec 30, 2023

    @elachlan
    Contributor

    @Olina-Zhang can your team please test this?

  3. added
    🪲 bug
    area-VSDesigner
    area-System.Drawing
    on Dec 30, 2023
  4. Olina-Zhang commented on Jan 2, 2024

    @Olina-Zhang
    Member

    @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:
    image
    .Net framework 4.8.1:
    image

    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

    image

  5. removed
    🪲 bug
    area-VSDesigner
    area-System.Drawing
    on Jan 2, 2024
  6. elachlan commented on Jan 2, 2024

    @elachlan
    Contributor

    Closing this issue as @Olina-Zhang has provided the appropriate solution.

  7. ghost removed
    untriaged
    on Jan 2, 2024
  8. BagavathiPerumal commented on Jan 24, 2024

    @BagavathiPerumal

    We are still experiencing the same issue while using below code in your shared sample. How to resolve this issue.

    public class ImageCollection : List<MyItem>
    {
        public Image this[string key]
        {
            get
            {
                Image imageInfo = null;
                IEnumerator enumerator = GetEnumerator();
                try
                {
                    while (enumerator.MoveNext())
                    {
                        Image imageInfo2 = (Image)enumerator.Current;
                    }
                }
                finally
                {
                    IDisposable disposable = enumerator as IDisposable;
                    if (disposable != null)
                    {
                        disposable.Dispose();
                    }
                }
    
                Image result = null;
                if (imageInfo != null)
                {
                    result = imageInfo.Clone() as Image;
                }
    
                return result;
            }
            set
            {
                Image imageInfo = null;
                IEnumerator enumerator = GetEnumerator();
                try
                {
                    while (enumerator.MoveNext())
                    {
                        Image imageInfo2 = (Image)enumerator.Current;
                    }
                }
                finally
                {
                    IDisposable disposable = enumerator as IDisposable;
                    if (disposable != null)
                    {
                        disposable.Dispose();
                    }
                }
    
                if (imageInfo != null)
                {
                    imageInfo = value;
                }
                else
                {
                    //Add(key, value);
                }
            }
        }
    }
    

    WinFormsApp-ImageCollection.zip

  9. RaghavendraSudhakar commented on Feb 7, 2024

    @RaghavendraSudhakar

    @elachlan We are still facing issue after using your code in our sample

    WinFormsApp-ImageCollection.zip

  10. elachlan commented on Feb 7, 2024

    @elachlan
    Contributor

    @lonitra can you please reopen this? I don't have correct permissions.

  11. reopened this on Feb 7, 2024
  12. added
    untriaged
    🪲 bug
    on Feb 7, 2024
  13. added this to the VS release milestone on Feb 14, 2024
  14. removed
    untriaged
    on Feb 14, 2024
  15. Tanya-Solyanik commented on Feb 20, 2024

    @Tanya-Solyanik
    Contributor

    @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)

    ImageCollection-Net9.zip

  16. locked and limited conversation to collaborators on Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

🪲 bugarea-DesignerSupport

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions