79595011

Date: 2025-04-27 12:26:18
Score: 0.5
Natty:
Report link

An expanded version of the GetImage function in C#, that allows for bitmaps and icons in the resource file.

public Bitmap GetImage(string imageId)

{
    Bitmap ErrorBitmap = Properties.Resources.Exclamation.ToBitmap();
    //'see https://stackoverflow.com/questions/4656883/how-enumerate-resources-inside-a-resx-file-programmatically
     
    // Convert the ResourceSet to IEnumerable and iterate over its entries
    ResourceSet resourceSet = Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
    foreach (DictionaryEntry entry in resourceSet)
    {
        string resName = entry.Key as string;
        if (resName == imageId)
        {
            object temp  = Properties.Resources.ResourceManager.GetObject(resName) as Icon;
            if (temp != null)  
                if (temp is Icon)
                {
                    ErrorBitmap = ((Icon)temp).ToBitmap();
                }
                else if (temp is Bitmap)
                {
                    ErrorBitmap = (Bitmap)temp;
                }
            
            break;
        }
    }

    // return the bitmap
    return ErrorBitmap;
}
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Doug Taylor