Книга: C# 2008 Programmer

Using the Shared Assembly

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 

Оглавление книги


Генерация: 1.057. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз