How to populate an ImageList from a Resource File

13,251

Solution 1

Here is an example of reading all images in a resource into an ImageList.

var dynamicImageList = new ImageList();
var resourceSet = MyResourceClass.ResourceManager.GetResourceSet(CultureInfo.InvariantCulture, true, false);
if (resourceSet != null)
{
   foreach (DictionaryEntry entry in resourceSet)
   {
      var value = entry.Value as Bitmap; //only get images
      if (value != null)
      {
          dynamicImageList.Images.Add((string) entry.Key, value);
      }
   }
}

Solution 2

try this one

 Private m_clsImageList as ImageList

Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs)
    m_clsImageList = New ImageList()
    m_clsImageList.Images.Add("add", My.Resources.add) 
    m_clsImageList.Images.Add("cut", My.Resources.cut) 
End Sub

or

resources = new ResourceManager("Icons", assemby-containing-icons.resx);
imageList.Images.Add((Image)resources.GetObject("image-resource-name");

Solution 3

Have you tried looping this? i know its an old post but why would you ever type out 100 lines of the same code +- 1 variable?

Share:
13,251
Qu1nncunxIV
Author by

Qu1nncunxIV

Updated on June 04, 2022

Comments

  • Qu1nncunxIV
    Qu1nncunxIV almost 2 years

    Just wondering if there is a way to populate an ImageList from a Resource file. I have looked around on the web, but everything seems to have been from back in 2003/2005.

    Any advice would be appreciated thanks in advance.

  • Qu1nncunxIV
    Qu1nncunxIV over 11 years
    Sorry, I should of been more specific in my question, I meant how to add a large number of images from a res file. I have all the images stored in a separate .resx file. I was hoping to not have to type out 100 lines of imagelist.Images.Add()
  • Qu1nncunxIV
    Qu1nncunxIV over 11 years
    Yeah, I thought I had tried that one already, but it does not seem to work, I think it is for C# not VB.Net
  • Qu1nncunxIV
    Qu1nncunxIV almost 11 years
    Did try looping it at the time. Didn't work. The eventual solution was to add in the images as a single sheet, then get the images using an array of rects. Thanks for your thought though.
  • Pakk
    Pakk almost 11 years
    What kinda resource file are you using if i knew i may be able to come up with an answer with / for you ( Some code may be in need here if you don't mind sharing )
  • James Shaw
    James Shaw over 8 years
    Don't know why I didn't think of this. Worked like a charm!