|
|
Resources in C# / Alex Zak When I programmed games in Java, I used to add images and sounds simply by packing them into the same Jar. It was quite messy, as I had to make sure that the IDE would pack them in the right location, or they will not be found by the code. Now that I started working with C-sharp in Microsoft Visual Studio 2005, I found the usefulness of resources. Resources are files that keep your images and sounds (and can keep any sort of file). When you build the project, they will be written into the .exe file. It is both an advantage and a disadvantage: on the one hand, you will have only one file, but on the other hand, to change some image or sound you will have to rebuild the project. For several hours I tried to find a way to use resources in the infamous documentation of the Microsoft Visual Studio, but failed, so I turned to the Internet, and now wrote down how to use resources easily. It was written for Microsoft Visual Studio 2005, but will probably work for every version. Using an image resourceFirst, lets create a resource file, and add an image to it: In the solution explorer (on the right), right click your project and choose "properties". Go to the Resources tab, and click to create a new resource file. Then click "Add Resource - > Add Existing File" and choose any image file on your computer. Great, now you can use it in your code(if it's a bitmat, an inbuilt bitmap editor can be used to change it). To use resources, create a ResourceManager: using System.Drawing; //I despise tutorials that do not mention what libraries to import The first argument in the constructor is the resource file, prefixed with the namespace. The second argument is the current assembly - to get it ise GetType().Assembly On any object you want. Now all you have to do is extract your image from the resource file: Bitmap image = (Bitmap)Resources.GetObject("image1");
The argument is the name of the image in the resource file (you can change it by double clicking the resource file in the solution explorer, finding the image, right clicking it and choosing "rename"). Congratulations! Now you can paint your image file or do with it whatever you like. Culture Resource FilesGenerally, one resource file can store all the resources you will need. When do you need more? When your program should have a different interface, depending on the language the user speaks. Then you create a resource file for each culture you want to support, and set the ResourceManager to work with the resource file of the user's choice. Then you will find it useful to have String resources, so that the program will speak the language of the user. In a "French" resource file, you can have this configuarion: In a "English" resource file, change only the values: Now, once you load the needed resource file (you can let the user choose a language), if you write: Resources.GetString("goodDay"); You will get the goodDay string in the language of the user. Did you find the article useful? Please donate here to support mysuperiorgames.com Questions, complaints and requests? Send them to admin@mysuperiorgames.com |