Retrieve image resource using string variable in foreach loop

前端 未结 2 1806
悲&欢浪女
悲&欢浪女 2021-01-15 21:13

I have a string array \"abc\" I put this in a for each loop. I want to retrieve an image from resources using the value in the foreach loop and put it into a picture box.

2条回答
  •  难免孤独
    2021-01-15 21:30

    Here is a simple method you can use

    Add to your code and replace XXXAPPNAMEXXX with the name of you application.

        public Bitmap GetImageResourceByName(string name)
        {
            Bitmap MethodResult = null;
            try
            {
                MethodResult = (Bitmap)XXXAPPNAMEXXX.Properties.Resources.ResourceManager.GetObject(name, XXXAPPNAMEXXX.Properties.Resources.resourceCulture);
    
            }
            catch //(Exception ex)
            {
                //ex.HandleException();
            }
            return MethodResult;
        }
    

    Note: Go into Resources.Designer.cs and make the private attribute resourceCulture public.

    I have commented out my error handling (ex.HandleException) as yours may differ.

提交回复
热议问题