Application.GetResourceStream Method (StreamResourceInfo, Uri)
Silverlight
Returns a resource file from a location in the specified zip package.
Namespace: System.Windows
Assembly: System.Windows (in System.Windows.dll)
public static StreamResourceInfo GetResourceStream( StreamResourceInfo zipPackageStreamResourceInfo, Uri uriResource )
Parameters
- zipPackageStreamResourceInfo
- Type: System.Windows.Resources.StreamResourceInfo
A StreamResourceInfo that contains the zip package stream with the desired resource file.
- uriResource
- Type: System.Uri
A relative URI that identifies the resource file to be extracted from the zip package. The URI is relative to the application package and does not need a leading forward slash.
Return Value
Type: System.Windows.Resources.StreamResourceInfoA StreamResourceInfo that contains the stream for the desired resource file.
| Exception | Condition |
|---|---|
| ArgumentException | The application class is not initialized. -or- uriResource is an absolute URI. |
| ArgumentNullException | zipPackageStreamResourceInfo is null. -or- uriResource is null. |
The GetResourceStream(StreamResourceInfo, Uri) method provides more flexibility than GetResourceStream(Uri) by allowing you to extract a resource file from an arbitrary zip package.
The following example shows how to extract an image resource file from a zip package that is located at the Silverlight application's site of origin.
<UserControl x:Class="SilverlightApplication.PageLong" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <StackPanel x:Name="stackPanel" /> </UserControl>
using System; // Uri using System.IO; // Stream using System.Windows; // Application using System.Windows.Controls; // TextBlock, Image using System.Windows.Media.Imaging; // BitmapImage using System.Net; // WebClient using System.Windows.Resources; // StreamResourceInfo namespace SilverlightApplication { public partial class PageLong : UserControl { public PageLong() { InitializeComponent(); // Load an image resource file that is included in a ZIP package // at the site of origin. // Specify the zip package with the image resource to get. Uri uri = new Uri("ZIPPackageWithImage.zip", UriKind.Relative); // Download the zip package. WebClient webClient = new WebClient(); webClient.OpenReadCompleted += new OpenReadCompletedEventHandler( webClient_OpenReadCompleted); webClient.OpenReadAsync(uri); } void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { // Extract the desired image from the zip package. Stream zipPackageStream = e.Result; Image image = LoadImageFromZipPackage( "ImageInZipPackage.png", zipPackageStream); this.stackPanel.Children.Add(image); } public Image LoadImageFromZipPackage( string relativeUriString, Stream zipPackageStream) { // Get the image stream at the specified URI that // is relative to the application package root. Uri uri = new Uri(relativeUriString, UriKind.Relative); StreamResourceInfo zipPackageSri = new StreamResourceInfo(zipPackageStream, null); StreamResourceInfo imageSri = Application.GetResourceStream(zipPackageSri, uri); // Convert the stream to an Image. BitmapImage bi = new BitmapImage(); bi.SetSource(imageSri.Stream); Image img = new Image(); img.Source = bi; return img; } } }
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.