Click here to Skip to main content
15,880,608 members
Articles / Programming Languages / C#
Article

Resource Editor .NET

Rate me:
Please Sign up or sign in to vote.
4.96/5 (23 votes)
17 Nov 20025 min read 263.8K   15.5K   103   31
A utility to easily refresh embedded graphic resources in a .NET assembly.

Sample Image - MainScreen.jpg

Introduction

I am sure Microsoft thought they were being progressive when they replaced the good ol' .rc file with the XML-based .resX file at the release of Visual Studio.NET. Unlike its predecessor, when you add a resource such as a bitmap to a .NET project through the properties of a Windows form, the actual bitmap binary data is embedded in the associated .resX file. This does not allow update or modification of the bitmap without removing and then adding the bitmap again to the form. Also, you can no longer easily add resources to the assembly not associated with an object managed by the designers. Microsoft includes a sample application which provides a rudimentary resource editor function with Visual Studio.NET, but it also only supports embedding the binary data and does not facilitate easy update of the resources.

The Resource Editor.NET utility is built off this sample application. It generates a .resources or .resX file which can be embedded in any .NET assembly project, and it also saves a .resourcesConfig file which saves the file locations for the resources you added. It currently supports bitmap, icon and string resources. It has saved countless hours during usability testing and language adaptation efforts.

Using the Tool

The Resource Editor .NET allows you to add bitmaps, icons and resource strings to use in your assemblies by making use of the classes in the System.Resources namespace. When you add a named resource, the full path information for the object is retained and saved to a .resourcesConfig XML file with the same name as the .resources or .resX file you specify when saving your work in the editor.  You can then add both files to your project in the Project Explorer. 

XML
<?xml version="1.0"?>
<Resources Extension=".resources">
  <Resource Name="ButtonEnglishNormal" 
Type="System.Drawing.Bitmap" 
FileName="C:\CodeSubmissions\ResourceEditor.NET\images\bt-english.bmp" Value="" />
  <Resource Name="ButtonEnglishDisabled" 
Type="System.Drawing.Bitmap" 
FileName="C:\CodeSubmissions\ResourceEditor.NET\images\bt-english-na.bmp" Value="" />
  <Resource Name="ButtonEnglishPressed" 
Type="System.Drawing.Bitmap" 
FileName="C:\CodeSubmissions\ResourceEditor.NET\images\bt-english-on.bmp" Value="" />
  <Resource Name="ButtonEnglishOver" 
Type="System.Drawing.Bitmap" 
FileName="C:\CodeSubmissions\ResourceEditor.NET\images\bt-english-over.bmp" Value="" />
  <Resource Name="ButtonEspanolNormal" 
Type="System.Drawing.Bitmap" 
FileName="C:\CodeSubmissions\ResourceEditor.NET\images\bt-espanol.bmp" Value="" />
  <Resource Name="ButtonEspanolDisabled" 
Type="System.Drawing.Bitmap" 
FileName="C:\CodeSubmissions\ResourceEditor.NET\images\bt-espanol-na.bmp" Value="" />
  <Resource Name="ButtonEspanolPressed" 
Type="System.Drawing.Bitmap" 
FileName="C:\CodeSubmissions\ResourceEditor.NET\images\bt-espanol-on.bmp" Value="" />
  <Resource Name="ButtonEspanolOver" 
Type="System.Drawing.Bitmap" 
FileName="C:\CodeSubmissions\ResourceEditor.NET\images\bt-espanol-over.bmp" Value="" />
  <Resource Name="ButtonRoundGlassNormal" 
Type="System.Drawing.Bitmap" 
FileName="C:\CodeSubmissions\ResourceEditor.NET\images\bt-roundglass.bmp" Value="" />
  <Resource Name="ButtonRoundGlassDisabled" 
Type="System.Drawing.Bitmap" 
FileName="C:\CodeSubmissions\ResourceEditor.NET\images\bt-roundglass-na.bmp" Value="" />
  <Resource Name="ButtonRoundGlassPressed" 
Type="System.Drawing.Bitmap" 
FileName="C:\CodeSubmissions\ResourceEditor.NET\images\bt-roundglass-on.bmp" Value="" />
  <Resource Name="ButtonRoundGlassOver" 
Type="System.Drawing.Bitmap" 
FileName="C:\CodeSubmissions\ResourceEditor.NET\images\bt-roundglass-over.bmp" Value="" />
</Resources>

You can freely rename the resources or change the file specified. When you press Save, the XML file and the chosen .NET resource binary format file is generated. You can also just reopen the file to cause any changes to the referenced bitmap or icon files in the existing locations as specified in the .resourcesConfig have been modified and you want the changes included in the assembly during the next build. All you have to do is add the resulting .resources or .resX file to the project for the assembly you want to embed the resources in.

Using the Tool with Resources Added Through the Forms Designer

Although I don't use this tool for this purpose, you could use it for enabling refresh for bitmaps or icons associated to objects using the forms designer built into Visual Studio .NET.  This requires one extra step.

  1. First open the default .resX file created with the form designer to get the resource names into the editor. 
  2. Then go through each resource in the editor and re-associate the original file with the resource name assigned by the forms designer.
  3. Save the resulting file on top of the original .resX file.

Unfortunately, if you add or remove bitmaps or icons afterwards using the forms designer, you would need to repeat the process to incorporate those changes.  However, you can manipulate the .resourcesConfig XML file with a text editor to get around this.

Using with Visual Studio .NET

The Resource Editor .NET has two modes. The first is the standard UI mode, where you open the application to an empty resource set or with an existing .resourcesConfig file as its first parameter. The second adds the command-line switch /Refresh, which supresses the GUI but will regenerate the .resources or .resX file associated with the .resourcesConfig file. The easiest way to use the tool is to add it as an external tool to Visual Studio .NET. Then when you select the .resourcesConfig file in the Project Explorer, you can either refresh or edit the resources from within the Visual Studio environment. The following two snapshots show how to set the tool up in the IDE.

Adding the Tool for Resource Editing

Adding External Tool Resource Editor.NET for Resource Editing

Adding the Tool for Resource Refresh

Adding External Tool Resource Editor.NET for Resource REfresh

Additional Information

  • As I mentioned briefly before, you can edit the .resourcesConfig XML file directly in any text editor and then use the Resource Editor .NET to apply the changes. I make extensive use of this in global renames or duplication and rename or resources to speed up the process. In fact, I rarely open the GUI any more unless I am adding one or two resources.
  • You can use an graphic image file format supported by the .NET Framework System.Drawing.Bitmap class, you are not restricted to bitmaps.  However, I only tested JPEG and BMP.
  • The C# source code is provided, but is not necessary to make use of this tool. However, it does use a PropertyGrid control and shows one way to implement a custom property editor used to capture and save the file's file name. Or perhaps you will want to add some additional functionality on your own.
  • You can use a text editor to make minor modifications to the XML file and use it to easily manage culture-specific resources when creating localized applications, such as changing the subdirectory used from ..\en to ..\es to add an English and Spanish culture to the application.
  • Due to some quirk the internal resource file name used for the ResourceManager class turns out to be Filename.Resources.AssemblyName to pull resources out of the resource files you add to the assembly. 
  • Only string resources use the XML attribute Value in the Resource objects.

History

Initial Release.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I have been developing C/C++ applications for Windows and UNIX platforms for 15 years, specializing in database and N-tier application architectures, including 7 years in hands-on project management (ugh!!). I also had a part in the OS/2 (what?) 2.1 development where I really got into multi-tasking, threads, synchronization and related technologies. Drank the Microsoft Kool-Aid long ago, though.

Comments and Discussions

 
GeneralMy vote of 5 Pin
steve stokes30-Nov-11 6:27
steve stokes30-Nov-11 6:27 
GeneralMy vote of 5 Pin
Kubin15-Nov-10 2:10
Kubin15-Nov-10 2:10 
GeneralRemove from ResX File Pin
Neelam Sanjeev8-Aug-07 3:09
Neelam Sanjeev8-Aug-07 3:09 
QuestionUsage for file types? Pin
Yaron Shkop27-Feb-06 1:34
Yaron Shkop27-Feb-06 1:34 
Questionmodifying resource files on accident? Pin
20XT622-Dec-05 6:49
20XT622-Dec-05 6:49 
Generalbad image link in above page Pin
idearn6-Apr-05 2:15
idearn6-Apr-05 2:15 
GeneralAdding Icons to VB.NET application Pin
MstrControl15-Feb-05 19:24
MstrControl15-Feb-05 19:24 
GeneralRe: Adding Icons to VB.NET application Pin
DrewManFu5-Mar-05 9:07
DrewManFu5-Mar-05 9:07 
GeneralInvalid file name for image Pin
Antonio Barros4-Sep-04 3:04
professionalAntonio Barros4-Sep-04 3:04 
GeneralRe: Invalid file name for image Pin
nicwilliams1-Feb-05 1:31
nicwilliams1-Feb-05 1:31 
GeneralEasy way to add binary resources Pin
rasw3-Sep-04 13:20
rasw3-Sep-04 13:20 
GeneralGreat stuff... Pin
olivierv28-May-04 2:15
olivierv28-May-04 2:15 
GeneralAnother editor Pin
Gaston3-Mar-04 3:42
Gaston3-Mar-04 3:42 
General.NET Resourcer Pin
Andre Seibel13-Sep-03 12:47
Andre Seibel13-Sep-03 12:47 
GeneralRe: .NET Resourcer Pin
krass18-Sep-03 3:37
krass18-Sep-03 3:37 
GeneralRe: .NET Resourcer Pin
Bill Sayles18-Sep-03 5:43
Bill Sayles18-Sep-03 5:43 
Good for them. I threw this one together because at the time I found nothing and my graphics department was changing the "look and feel" daily. I appreciate the feedback, but it probably would be some time until I could correct the problems with icons or make any other improvements due to my current workload. This little tool will probably be "unsupported" for now. If another tool supercedes it, I am all for it.
GeneralRe: .NET Resourcer Pin
MtnBiknGuy24-Nov-03 6:31
MtnBiknGuy24-Nov-03 6:31 
Questionexcellent tool - icon not really supported? Pin
wurzelchen9-Sep-03 21:55
wurzelchen9-Sep-03 21:55 
GeneralGreat article! Pin
mattiasnylen26-Aug-03 23:50
mattiasnylen26-Aug-03 23:50 
QuestionSaving images? Pin
balpo6-Jan-03 21:48
balpo6-Jan-03 21:48 
AnswerRe: Saving images? Pin
Bill Sayles23-Jan-03 8:49
Bill Sayles23-Jan-03 8:49 
GeneralRe: Saving images? Pin
raygilbert14-Jul-04 16:56
raygilbert14-Jul-04 16:56 
GeneralRe: Saving images? Pin
run149225-Oct-05 22:47
run149225-Oct-05 22:47 
GeneralRe: Saving images? Pin
cplotts1-Nov-05 7:15
cplotts1-Nov-05 7:15 
GeneralThanks! Pin
Chad Busche5-Dec-02 4:57
Chad Busche5-Dec-02 4:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.