Loading Images from resources to an array?

Unanswered Loading Images from resources to an array?

  • Monday, August 20, 2012 7:33 AM
     
     

    I have 52 card images(.jpg) stored in Images folder shown in solution explorer. I have build action set to Resources.

    Now I want to load each image from the folder to an array of Image.

    How do I do that?

All Replies

  • Monday, August 20, 2012 7:38 AM
     
     
  • Monday, August 20, 2012 7:55 AM
     
      Has Code

    Hi, you can do it something like:

    Image[] cards = System.IO.Directory.GetFiles("imagesPath").Select(f => Image.FromFile(f)).ToArray();


    Mitja

  • Monday, August 20, 2012 4:40 PM
     
     

    Thanks for reply but I don't need a way to store pictures in an array but what I need is a way/method to extract resources form Images folder.

    Your help would be highly appreciated....

  • Monday, August 20, 2012 4:42 PM
     
     

    Hi,

    extract where?


    Mitja

  • Monday, August 20, 2012 7:09 PM
     
     
    why dont you load them into a imagelist?
  • Tuesday, August 21, 2012 4:28 AM
     
     

    from resources to array..

    you specified ImagePath I want to know what is imagePath for Images folder that is built as Resources. I mean to say if I had stored card images in external folder than I could have been able to use simple path to directory.But my images are in resources so how can I extract them from resources to my array

    your help would be appreciated

  • Tuesday, August 21, 2012 6:46 AM
    Moderator
     
     

    Hi Bipin,

      Do you think the replies posted by Karthik9 is suitable for your requirement? He recommend the way that is using GetManifestResourceStream to get stream from image resource ,and convert this Stream into byte array if needed.

      Please feel free to mark his reply as answer if his reply really meet your requirement.

      Sincerely,

      Jason Wang


    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

  • Tuesday, August 21, 2012 8:02 AM
     
      Has Code

    I know what you mean, its easy to get a single image from Resources, but not so easy to get a bunch of them, like an array. 

    Thats why I did an example code, which gets you all the images from Resources. And I use List<T>, where T is a Bitmap class, instead of an Array (its betters, since for array you have to explicitly define the length of it (even if you can Resize it), when List<t> is resizing "automatically").

    Here is how you can get the images into a List<T>:

      System.Resources.ResourceManager res = Properties.Resources.ResourceManager;
      System.Resources.ResourceSet resSet = res.GetResourceSet(System.Globalization.CultureInfo.CurrentUICulture, true, true);
      System.Collections.IDictionaryEnumerator en = resSet.GetEnumerator();
      List<Bitmap> images = new List<Bitmap>();
      // Goes through the enumerator, printing out the key and value pairs.
      while (en.MoveNext())
      {
        images.Add(en.Value as Bitmap);
        //en.Key which is the name of the resource, en.Value is the resource
      }

    Hope it helps, 

    bye


    Mitja

    • Marked As Answer by Bipin Bhandari Tuesday, August 21, 2012 11:07 AM
    • Unmarked As Answer by Bipin Bhandari Tuesday, August 21, 2012 11:08 AM
    •  
  • Tuesday, August 21, 2012 11:33 AM
     
     

    Mitza Bonca I am thankful for your reply but I still didn't get images into List<T>. I don't think there is any link to Images folder in resources in your code or am I missing anything?

    Kindly reply please. Your help would be appreciated!