If you are using Windows Vista, make sure to run regedit as Administrator.
Navigate to the HKEY_LOCAL_MACHINESOFTWAREMicrosoft.NETFramework AssemblyFolders
key. Right-click on the AssemblyFolders key and select New→Key (see Figure 15 -39).

Figure 15-39
Name the new key Base64Codec
. Double-click on the key's (Default) value, and enter the full path of the shared assembly (for example, C:Documents and SettingsWei-Meng LeeMy Documents Visual Studio 2008ProjectsBase64CodecinDebug
; see Figure 15-40).

Figure 15-40
Then restart Visual Studio 2008, and the assembly should appear in the Add Reference dialog.
Using the Shared Assembly
Let's now create a new Windows application project to use the shared assembly stored in the GAC. Name the project WinBase64
.
To use the shared assembly, add a reference to the DLL. In the Add Reference dialog, select the Base64Codec
assembly, as shown in Figure 15-41, and click OK.

Figure 15-41
Note in the Properties window that the Copy Local property of the Base64Codec is set to False (see Figure 15-42), indicating that the assembly is in the GAC.

Figure 15-42
Populate the default Form1
with the controls shown in Figure 15-43 (load the pictureBox1
with a JPG image).

Figure 15-43
In the code-behind of Form1
, define the two helper functions as follows:
Remember to import the System.IO
namespace for these two helper functions.
public byte[] ImageToByteArray(Image img) {
MemoryStream ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
public Image ByteArrayToImage(byte[] data) {
MemoryStream ms = new MemoryStream(data);
Image img = new Bitmap(ms);
return img;
}
Code the Test button as follows:
private void btnTest_Click(object sender, EventArgs e) {
//---create an instance of the Helper class---
Base64Codec.Helper codec = new Base64Codec.Helper();
//---convert the image in pictureBox1 to base64---
string base64string =
codec.Encode(ImageToByteArray(pictureBox1.Image));
//---decode the base64 to binary and display in pictureBox2---
pictureBox2.Image = ByteArrayToImage(codec.Decode(base64string));
}
Here you are creating an instance of the Helper
class defined in the shared assembly. To test that the methods defined in the Helper
class are working correctly, encode the image displayed in pictureBox1
to base64, decode it back to binary, and then display the image in pictureBox2
.
Press F5 to test the application. When you click the Test button, an identical image should appear on the right (see Figure 15-44).

Figure 15-44
Examine the manifest of the WinBase64.exe
assembly to see the reference to the Base64Codec assembly (see Figure 15-45). Observe the public key token stored in the manifest — it is the public key token of the shared assembly.

Figure 15-45
Summary
This chapter explained the parts that make up a .NET assembly. Splitting your application into multiple assemblies and modules will make your application easier to manage and update. At the same time, the CLR will only load the required assembly and modules, thereby making your application more efficient. If you have a shared assembly that can be used by other applications, consider deploying it into the Global Assembly Cache (GAC).
Part II
Application Development Using C#
Chapter 16
Developing Windows Applications
Chapters 16-19 show how you can use the C# language to create a different type of application. This chapter tackles Windows application development. The best way to learn a language is to actually work on a real project from the beginning to deployment. So, this chapter leads you through creating a Windows application that performs some useful tasks and then shows you how to deploy it using a technique in Visual Studio known as