Fireball Resourcer






3.45/5 (6 votes)
Dec 29, 2004
1 min read

37610

749
Embeding resources on application with base64 alghoritm.
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.