SIGN UP MEMBER LOGIN:    
ARTICLE

Creating and Using Resources

Posted by casper boekhoudt Articles | Windows Forms C# November 16, 2001
In this article, I will give you a small example of how to add and use resources.
Reader Level:
 

Introduction

In this article, I will give you a small example of how to add and use resources. This tutorial consists of two classes; one to create the resource and one that uses it.

The System.Resources is a very useful namespace that allows you to create and store culture-specific resources used in an application (for example, you can have Spanish and English resources). Resources allows you to place culture-specific items inside satellite files, rather than directly in your main application. This aspect can dramatically improve reusability of your code. One does not need to recompile the code in order to change the resource.

Recently, I have found another useful way to use resources. Unless an images is already loaded into memory, whenever you do a mouseover you need to read the image from file.

For example: myLabel.Image = Image.FromFile("image.jpg");

If you have a big application, the system is slow, or you need to load big images, the image can take a few moments to load up, and will give you a delay. However, if the images is in a resource file, the change will be almost instantaneous.

Creating the Resource File:

To create a resource file, create an instance of ResourceWriter and pass it the name of the resource file you want to create as a string.

ResourceWriter myResource =new ResourceWriter("Images.resources");

Now add your image to the resource file by using the AddResource method and pass it the name of the image and the image as the parameters.

myResource.AddResource(fileName, new Bitmap(files[i]));

Alternately, you can use the overloaded AddResource method to pass it a string as the value.

myResource.AddResource("MyResourceName", "My Resource Value");

Once you have added everything you want to the resource file, call the Close() method. This will implicitly call the Generate() method (which will save the resources to the output stream) and then closes the file.

myResource.Close();

Using Resources:

The first thing you need to do in order to use the Images.resources file in your code is add it to your project. In VS.NET, right-click on your project, select Add Existing Item and add your resource file. This will add the file as an Embedded Resource.

Use ResourceManager to access and control the resources.

private System.Resources.ResourceManager rm = new System.Resources.ResourceManager("ResourceClient.Images",System.Reflection.Assembly.GetExecutingAssembly());

Now you can easily retrieve your specific objects and strings by using the GetObject and GetString methods.

lblCancel.Image = (Image)rm.GetObject("cancel-on.png");
this.lblResource.Text = rm.GetString("MyResourceName");

Login to add your contents and source code to this article
share this article :
post comment
 

Hi Casper,

I was trying to implement your code with my project, but it is somewhat different. I've been looking for something similar but with no luck
Let me ask directly and see if you can help me

I need to draw an image in a picturebox which I do this way:


Bitmap bit0 = (Bitmap)Properties.Resources._0_0;

pictureBox1.Image = bit0;
pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;

the problem comes when I try to draw a second resource file in the same picture box without erasing the first one, and if possible not shifting the original tile since it was first centered.


Another problem is to rotate the tile appropriately to locate the correct value in the right position, but I will search this after I solve the first one.

I appreciate your help and if by any chance you don't know how to play domino, here is the link

http://www.domino-games.com/domino-rules/domino-basics.html


Thank you very much

Cota

Posted by L A Nov 23, 2009

this blog >>

http://blogs.msdn.com/msbuild/archive/2007/10/19/manifest-resource-names-changed-for-resources-files.aspx

 will explain the issue with resources files in vs 2008 and the solution.

 

Calvin.

Posted by Calvin Charles Sep 20, 2008

I can't read .resource file I use vs.net 2003. I want to Why? thanks !

Posted by weigp weigp Dec 25, 2007

Great solution, however not working anymore with visual studio 2008 (it was working with 2005)

Posted by peli peli Nov 24, 2007

I struggled for hours last night looking for information on how to get images out of a .resources file, with nothing to show for it but crashing. Your article was the one that showed me how easy it was. Thank you!

Posted by Daniel Hennis Jul 17, 2007
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor