Icons corrupt, lose resolution, or quality in Winforms (Windows Forms) ListViews and TreeViews when using the ImageList control.

WinForms is still alive and well, perhaps not too well given the number of issues that crop up in these rather old controls.

Whilst most user interface work is now carried out on a web platform you may still have applications written in Windows Forms for the .NET Framework.


One issue we've recently encountered was with the ListView and TreeView controls displaying icons in a substandard fashion either corrupting the icons or generally lowering the quality.




Whilst this can be seen in the ListView or TreeView controls the issue is actually with the ImageList control which can lower the quality of images when serializing them.


Zooming in to a corrupted image and the exact same image that hasn't been corrupted by the image list the difference is clear.



The workaround to the issue is to store the images in a resource file





You can then load the images into the image list from the resource file dynamically when the form is loaded so that the ImageList never has to serialize them.

You can use an extension to make it easier.

namespace CENTREL.Windows.UI
{


    /// <summary>
    /// Provides extension functions for image lists.
    /// </summary>
    public static class ImageListExtensions
    { 

        /// <summary>
        /// Adds images to the image list from the specified resource manager.
        namespace CENTREL.Windows.UI
{

 
    /// <summary>
    /// Provides extension functions for image lists.
    /// </summary>
    public static class ImageListExtensions
    {

        /// <summary>
        /// Adds images to the image list from the specified resource manager.
        /// </summary>
        /// <param name="imageList">The image list to which the images are to be added.</param>
        /// <param name="resourceManager">The resource manager from which to add the images.</param>
        /// <remarks>This is a workaround to the image degradation caused by the image list control.</remarks>
        public static void AddImagesFromResourceManager(this ImageList imageList, ResourceManager resourceManager )
        {

            foreach (DictionaryEntry entry in resourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, true))
            {
                if (!(entry.Value is Image)) { continue; }
                imageList.Images.Add(Convert.ToString(entry.Key), (Image)entry.Value);
            }
        }

     }

}















Comments

Popular posts from this blog

Windows Server 2016, 2019, 2022, Windows 10 and Windows 11: Date and time "Some settings are managed by your organization".

TFTPD32 or TFTPD64 reports Bind error 10013 An attempt was made to access a socket in a way forbidden by its access permissions.

Enable function lock for F1-F12 on HP ZBook mobile workstations