Packaging Images and Media
While you can download images and media file by file, sometimes an application requires a collection of related images or other media before it can start. One example of this is a game that might need a number of images to display scenes, characters, and the background. You can package these resources into a single ZIP archive and download it. After downloading the ZIP file, using the WebClient class perhaps, you can save its stream. Let's revisit the image browser from earlier in the chapter and alter it to download the images stored in a ZIP file. The image-browsing interface is essentially the same, but there's a download button that initiates the download of the image archive.
private StreamResourceInfo imageArchiveStream;
private void downloadButton_Click(object sender, RoutedEventArgs e) {
WebClient wc = new WebClient(); wc.OpenReadCompleted +=
new OpenReadCompletedEventHandler(wc_OpenReadCompleted); wc.OpenReadAsync(
new Uri("/chapter6Web/HubbleImageArchive.zip", UriKind.Relative));
The OpenReadCompleted event handler is where the ZIP archive is processed. First, the stream is saved, and then we get a reference to a custom XML file stored within the archive.
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {
imageArchiveStream = new StreamResourceInfo(e.Result, null); StreamResourcelnfo manifestStream =
Application.GetResourceStream(imageArchiveStream, new Uri("manifest.xml", UriKind.Relative));
Post a comment