Click here to Skip to main content
Click here to Skip to main content

Fireball Resourcer

, 29 Dec 2004
Rate this:
Please Sign up or sign in to vote.
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:

 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:

 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

Share

About the Author

dotnetfireball
Web Developer
Italy Italy
No Biography provided

Comments and Discussions

 
Generalerror Pinmemberguns_zdz@163.com19-Jun-06 20:13 
GeneralThis is Beta 2 code and should be updated to RTM PinmemberKeia13-Feb-06 23:13 
Generalquestion Pinmembertang198331-Oct-05 16:15 
Generaldon't Open Pinmemberwhiteisgem11-Sep-05 15:08 
GeneralRe: don't Open PinsussAnonymous12-Sep-05 0:37 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 29 Dec 2004
Article Copyright 2004 by dotnetfireball
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid