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

Fireball Resourcer

Rate me:
Please Sign up or sign in to vote.
3.45/5 (6 votes)
29 Dec 20041 min read 37.1K   749   22   5
Embeding resources on application with base64 alghoritm.

Sample Image - FireballResourcer.png

Introduction

Fireball Resourcer is a Windows Forms application written for simple generation of resources to embed it on your application without using .resx or .resources. Fireball Resourcer uses a simple method. It converts all resource images, icons and text files to an encoded string with base64 algorithm for putting it on a static class. This class contains some methods to decode the resources to an Image object, Icon object or a String object. I have developed this system because it is very practical to use. With this, you will not have the risk of losing data and this is common with ImageList and other resource systems on .NET.

Fireball Resourcer is provided with a nice interface for inserting resources as images, sounds and icons, on menu File -> Export. You are free to choose to export all as a C# class or VB.NET or export as a compiled assembly. The compatibility for the moment is for C# 2.0, but if you are using .NET 1.1 as your development platform, you need to remove a little set of instructions that are not supported on the old framework.

The encryption and decryption classes are composed by some methods, but the most important are these:

C#
private char[] EncodeResourceToBase64(string resource,string restype)  {
     byte[] binData;
     if(restype=="System.String")
     {
          // If a resource is a string
          // trasform it as a array of bytes
          binData= StringToByteArray(resource);
      }
      else
      {
          // Load the file as a stream
          // and load bytes on the array
          FileStream sfile;
          try
          {
               sfile=new FileStream(resource,FileMode.Open,FileAccess.Read);
               binData=new byte[sfile.Length];
               long bytesRead=sfile.Read(binData,0,(int)sfile.Length);
               sfile.Close();
          }
          catch(Exception ex)
          {
               throw ex;
          }
     }

     // Convert the binary input on output as a base64 UUEncoded
     // 3 byte of the sequenze they become 4 char of the array
     long arrayLen=(long)((4.0d/3.0d)*binData.Length);
     //if the array is not divisibile for 4 pass ti the next multiple of 4
     if((arrayLen % 4)!=0)
     {
          arrayLen+=4-(arrayLen%4);
     }

     char[] base64CharArray=new char[arrayLen];
     try
     {
          Convert.ToBase64CharArray(binData,0,
                           binData.Length,base64CharArray,0);
          return base64CharArray;
     }
     catch(ArgumentNullException ex)
     {
         throw new ArgumentNullException(ex.ParamName,ex.Message);
     }
     catch(ArgumentOutOfRangeException ex)
     {
          throw new ArgumentOutOfRangeException(ex.ParamName,ex.Message);
     }
}

Another important function is decode. See this code:

C#
private object DecodeResourceFromBase64(string resource,string restype)
{
     object retobj = null;
     byte[] memBytes = Convert.FromBase64String(resource);
     IFormatter formatter =
       new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
     MemoryStream stream = new MemoryStream(memBytes);

     if(restype=="System.String")
     {
         retobj = System.Text.Encoding.UTF8.GetString(memBytes);
     }
     else
     {
          if(restype=="System.Drawing.Image")
          {
               retobj =Image.FromStream(stream);
          }
          if(restype=="System.Drawing.Icon")
          {
               retobj =new Icon(stream);
          }
          if(restype=="System.Windows.Forms.Cursor")
          {
                retobj =new System.Windows.Forms.Cursor(stream);
          }
          else
          {
               //TODO: other type
               //retobj =formatter.Deserialize(stream);
           }
       }
       stream.Close();
       return retobj;
   }

I successfully use this system on my applications for embedding icons and images on my assembly. This software is in beta, but is functioning. If you find bugs or new ideas to implement, please contact me.

It is all. If you have any question about the code, please contact me with a comment on this article or by my forum.

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
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalerror Pin
guns_zdz@163.com19-Jun-06 19:13
guns_zdz@163.com19-Jun-06 19:13 
GeneralThis is Beta 2 code and should be updated to RTM Pin
Keia13-Feb-06 22:13
professionalKeia13-Feb-06 22:13 
Generalquestion Pin
tang198331-Oct-05 15:15
tang198331-Oct-05 15:15 
Generaldon't Open Pin
whiteisgem11-Sep-05 14:08
whiteisgem11-Sep-05 14:08 
GeneralRe: don't Open Pin
Anonymous11-Sep-05 23:37
Anonymous11-Sep-05 23:37 

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.