Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not find any resources appropriate for the specified culture (C# WinForm Images in Resource File)

Tags:

c#

winforms

I added three images to a file called Resource1.resx. I also added one string just for test purposes. I'm getting this error on either the GetString or the GetObject(image-name).

{"Could not find any resources appropriate for the specified culture or the neutral culture. Make sure \"Resource1.resources\" was correctly embedded or linked into assembly \"TFBIC.RCT.Monitor\" at compile time, or that all the satellite assemblies required are loadable and fully signed."}

    // get initial images 
ResourceManager rm = new ResourceManager(
    "Resource1",System.Reflection.Assembly.GetExecutingAssembly());
CultureInfo ci = Thread.CurrentThread.CurrentCulture;


string strTest = rm.GetString("String1");  // just testing 

Bitmap bmCircleGreen = (Bitmap)rm.GetObject("circleGreen");      
Bitmap bmCircleYellow = (Bitmap)rm.GetObject("circleYellow");      
Bitmap bmCircleRed = (Bitmap)rm.GetObject("circleRed");  

My form is the first class in my project (I've already seen that error).
I assigned a strong-key to my project to no avail.
Not sure what else to try.

like image 815
NealWalters Avatar asked Dec 17 '09 19:12

NealWalters


4 Answers

Have you remembered to include the default namespace/folder when you reference the resource?

ResourceManager rm = new ResourceManager("DefaultNamespace.Folder.ResourceName");

If you are unsure of the correct name, load the assembly in Reflector and browse down to see what it is.

like image 97
Mikael Svenson Avatar answered Nov 07 '22 11:11

Mikael Svenson


The 1st argument is wrong. But, there is already a ResourceManager created for you. You can see its code: in the Solution Explorer window open the Properties node, open the Resources.resx node and double-click the Resources.Designer.cs file.

You'll get its instance with Properties.Resources.ResourceManager. If you added the bitmaps with Project + Properties, Resources tab (strongly recommended), you can just refer to the property by the name you gave it. Like Properties.Resources.circleGreen. Do beware that you get a new image object each time you use the property, you may need to copy it to a variable if you use it more than once.

like image 36
Hans Passant Avatar answered Nov 07 '22 09:11

Hans Passant


I also had the same problem. adding "DefaultNameSpace." ("BusinessLogic" is the default namespace in my case) before resourcefileName in following line resolved my problem

 public static ResourceManager rm = new ResourceManager(string.Concat("BusinessLogic." , Constants.Common.ResourceFileName), Assembly.GetExecutingAssembly());
like image 29
Shalini Avatar answered Nov 07 '22 09:11

Shalini


I had the same problem today on a VS-designer-built winform. I had added an image to an item of a menuStrip:

this->menuitemFileSettings->Image = (cli::safe_cast<System::Drawing::Image^>
  (resources->GetObject(L"menuitemFileSettings.Image")));

Since then the same error came up. Testing with images on other controls was the same.

  • Based on MS KB, the Form class must be the first in the code. This was was fine in my code.
  • The resouces file also has the same name (frmMain.resx) as the winforms class (frmMain).
  • Checking the created exe using ILSpy, I even found the image resource in the file. Something I retrospectively don't really understand.
  • I am not using any localization.
  • I created a new winform, copied all relevant parts there, same failure.

Somehow I finally stumbled over the "Managed Resources" entry in the project settings. The setting "resource file name" has the (default) value $(IntDir)\$(RootNamespace).$(InputName).resources... which is correct, but when I realized that the path contains the name of the namespace, I checked the namespace and found, that I had changed it (from NS_Winform to NS_assemblies) for easier import of the assemblies.

Changed it back, working fine now. :-)
But I still don't understand, how the resources content
1) was included into the exe despite the wrong namespace, and
2) not found then although included.

like image 1
Tobias Knauss Avatar answered Nov 07 '22 11:11

Tobias Knauss