Use resources to hold images, text, and other data, and load it at run time in C#

Resources are images, strings, text files, and other pieces of data that you can build into an application. Your program can load the resources at run time to display new pictures, text, or whatever.

To add resources to the project, open the Project menu and select the Properties command at the very bottom. On the Properties page, click the Resources tab. Now you can use the Add Resource dropdown menu to add existing or new files to the program’s resources.

When you add a resource to the program, C# creates a typed variable representing it named namespace.Properties.Resources.resourcename where namespace is the program's namespace and resourcename is the resource's name. For instance, in this example I added a jpg file named Earth.jpg, so the program can use the Bitmap variable Properties.Resources.Earth.

The example program displays a group of RadioButtons labeled Mercury, Venus, Earth, and so forth. When you click one, code similar to the following executes to display the corresponding resource.

private void radMercury_CheckedChanged(object sender, EventArgs e)
    picPlanet.Image = Properties.Resources.Mercury;

This entry was posted in controls, graphics, strings and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.