Marko Apfel - Afghanistan/Belgium/Germany

Management, Architecture, Programming, QA, Coach, GIS, EAI

  Home  |   Contact  |   Syndication    |   Login
  187 Posts | 2 Stories | 201 Comments | 4 Trackbacks


Twitter | LinkedIn | Xing


Post Categories

Image Galleries



Enterprise Library


SQL Server


Often i see code-constructs like

   1: try
   2: {
   3:     string bitmapResourceName = GetType().Name + ".bmp";
   4:     Bitmap bitmap = new Bitmap(GetType(), bitmapResourceName);
   5: }
   6: catch (Exception ex)
   7: {
   8:     System.Diagnostics.Trace.WriteLine("Invalid bitmap.");
   9: }

The corresponding codetree has the structure:

This works as so long as the bitmap is near the calling component and the names are similar. If the name of Form2 is changed (maybe through refactoring) the developer must remember, that there is a corresponding bitmap. The problems are preassigned!

And nobody gets this information during compiletime, only during runtime this break is affected.

So a better way is to create a simple image resource and move/link the bitmap in this resource.

To access this bitmap the following very simple line is used:

   1: Bitmap bitmap = new Bitmap(Images.Form2);

If somebody moves the bitmap or changes names an error is thrown:

   1: Resource 'Form2' could not be loaded because the file to which it is linked could not be found:


The analog way for cursors is to put the *.cur file in a ressource – i use the same Images.resx for that and then via a memorystream the cursor can be created:

   1: Cursor cursor = new Cursor(new MemoryStream(Images.MyNewCursor));

Marginal investment with big benefit!

posted on Saturday, November 1, 2008 12:30 PM