How to add resources in separate folders?

后端 未结 2 1775
醉酒成梦
醉酒成梦 2021-02-01 10:36

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

2条回答
  •  日久生厌
    2021-02-01 11:08

    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

提交回复
热议问题