views:

480

answers:

2

Hello,

When I try to add a resource at the resource designer by clicking "Add an existing item",the item is placed in the folder "Resource".

The problem is that if I create a new directory in the Resource directory and place the resources there,I get a compiler error that the files cannot be found.

I can't put all resources in one folder,because I have to add 2500 images and some of them match their names.

+2  A: 

You do not need to add the images under the Resources folder. You can add the images to any folder you wish, and then set the build action for the images to "Embedded Resource". That way they will be compiled into the assembly as resources. I don't know if there are performance issues coming into play when it is a large number of images though...

Update: more in detail:

  1. Add the folders and image files as project items to the project (so that you can see each folder and the images within it in the Solution Explorer)
  2. Set the Build Action property of each of the image files to "Embedded Resource" (you can do this for multiple files at the same time; just select all the image files in the solution explorer).

This will cause the image files to be compiled into the assembly as resources. Each file will be assigned a resource name following this pattern: <root namespace for the assembly>.<folder name>.<image file name>. You can load an image using this code: using(Stream stream =

Assembly.GetExecutingAssembly().GetManifestResourceStream("<root namespace for the assembly>.<folder name>.<image file name>"))
{
    pictureBox1.Image = Image.FromStream(stream);
}
Fredrik Mörk
How can I sent a build action on a folder? I have set that to the resouce I created for the images.I can't add items to the resource I created if I manually add them in the folder,I have to add them via the resource designer.If I do so they aren't in seperate folders.
John
@John; see my update; hope it answers your questions.
Fredrik Mörk
I didn't create any resource files/projects/solutions.All I did was a folder with images as Embedded Resource.Thanks though!I used this to see the resources "string[] str = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();"\n\n\n and this to setup the picture "pnlInventory.BackgroundImage = Image.FromStream(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Celebrity.Resources.all_potion_01.jpg"));".
John
A: 

Create a new resource file (in following example I called it Images01 in folder resx) Create a custom resource manager class and initialize it to to point to this file just created

ResourceManager rm = new ResourceManager("ROOTNAMESPACE.resx.Images01", 
                                     System.Reflection.Assembly.GetExecutingAssembly());

Implement the method to GetImage

    public static Image GetImage(string fileName)
    {
        Stream stream = GetResourceStream(fileName);

        Image image = null;
        if (stream != null)
        {
            image = Image.FromStream(stream);
        }

        return image;
    }

Add images to this resx file

And then you can use it in your code as follows

this.picProject.Image = Resources.GetImage("ImageName.png");

Hope it helps

Suneet
Imagine I have two pictures with one name,for example "A.jpg".I want to implement them in two folders so they dont get replaced ,for example Folder C and Folder D.That's the problem.
John
There is no limit on number of custom resource managers.Since you put it in separate folders / resx files (you can create resx01, resx02 and so on)You will keep a track on your logical separation of the resource managersHTH
Suneet