在Silverlight中读取指定URL图片包数据
在silverlight开发,允许我们获取指定URL压缩包(zip)并读取其中的图片文件。而实现这个功能也
非常简单。下面是运行效果图:
首先,我们需要建立一个silverligth application ,名称为:DownLoadImg.
下面就是相应的page.xaml代码:
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="250" />
<RowDefinition Height="100" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="1">
<ListBox x:Name="ImageList"/>
<Button x:Name="Download" Click="StartDownLoad" Content="下载该图片"/>
</StackPanel>
<StackPanel Grid.Row="0">
<ScrollViewer HorizontalScrollBarVisibility="Auto" >
<Image x:Name="ImgToFill" >
</Image>
</ScrollViewer>
</StackPanel>
<StackPanel Grid.Row="2">
<Canvas Canvas.Top="70" >
<Rectangle Name="progressRectangle" Height="10" Width="0" Fill="AliceBlue" />
<Rectangle Height="12" Width="202" StrokeThickness="1" Stroke="Black" />
<TextBlock x:Name="progressText" Canvas.Left="210" Text="0%" FontSize="12" />
</Canvas>
</StackPanel>
</Grid>
<Grid.RowDefinitions>
<RowDefinition Height="250" />
<RowDefinition Height="100" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="1">
<ListBox x:Name="ImageList"/>
<Button x:Name="Download" Click="StartDownLoad" Content="下载该图片"/>
</StackPanel>
<StackPanel Grid.Row="0">
<ScrollViewer HorizontalScrollBarVisibility="Auto" >
<Image x:Name="ImgToFill" >
</Image>
</ScrollViewer>
</StackPanel>
<StackPanel Grid.Row="2">
<Canvas Canvas.Top="70" >
<Rectangle Name="progressRectangle" Height="10" Width="0" Fill="AliceBlue" />
<Rectangle Height="12" Width="202" StrokeThickness="1" Stroke="Black" />
<TextBlock x:Name="progressText" Canvas.Left="210" Text="0%" FontSize="12" />
</Canvas>
</StackPanel>
</Grid>
接下来是page.xaml.cs(相关内容见注释):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Resources;
using System.Windows.Media.Imaging;
namespace DownLoadImg
{
public partial class Page : UserControl
{
WebClient wc = new WebClient();
public Page()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
}
//加载图片列表数据
void Page_Loaded(object sender, RoutedEventArgs e)
{
ImageList.Items.Add("1.jpg");
ImageList.Items.Add("2.png");
}
//实例化下载设置
void StartDownLoad(object o, EventArgs e)
{
//初始化相应控件信息
ImgToFill.Visibility = Visibility.Collapsed;
progressRectangle.Width = 0;
progressText.Text = "0%";
//绑定下载过程中处理的事件
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
//开始异步读取压缩包中的文件信息
wc.OpenReadAsync(new Uri("img.zip", UriKind.Relative), ImageList.SelectedItem);
}
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
//实例化流资源信息,准备获取其中的图片数据
StreamResourceInfo sri = new StreamResourceInfo(e.Result as Stream, null);
//要读取的图片路径信息
String sURI = e.UserState as String;
//从流资源中获取指定的URL图片流信息
StreamResourceInfo imageStream = Application.GetResourceStream(sri, new Uri(sURI, UriKind.Relative));
BitmapImage imgsrc = new BitmapImage();
//绑定该URL图片信息并进行显示
imgsrc.SetSource(imageStream.Stream);
ImgToFill.Source = imgsrc;
ImgToFill.Visibility = Visibility.Visible;
ImgToFill.Stretch = Stretch.Fill;
}
void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
//下载过程中的进度显示
progressText.Text = e.ProgressPercentage.ToString() + "%";
progressRectangle.Width = (double)e.ProgressPercentage * 2;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Resources;
using System.Windows.Media.Imaging;
namespace DownLoadImg
{
public partial class Page : UserControl
{
WebClient wc = new WebClient();
public Page()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
}
//加载图片列表数据
void Page_Loaded(object sender, RoutedEventArgs e)
{
ImageList.Items.Add("1.jpg");
ImageList.Items.Add("2.png");
}
//实例化下载设置
void StartDownLoad(object o, EventArgs e)
{
//初始化相应控件信息
ImgToFill.Visibility = Visibility.Collapsed;
progressRectangle.Width = 0;
progressText.Text = "0%";
//绑定下载过程中处理的事件
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
//开始异步读取压缩包中的文件信息
wc.OpenReadAsync(new Uri("img.zip", UriKind.Relative), ImageList.SelectedItem);
}
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
//实例化流资源信息,准备获取其中的图片数据
StreamResourceInfo sri = new StreamResourceInfo(e.Result as Stream, null);
//要读取的图片路径信息
String sURI = e.UserState as String;
//从流资源中获取指定的URL图片流信息
StreamResourceInfo imageStream = Application.GetResourceStream(sri, new Uri(sURI, UriKind.Relative));
BitmapImage imgsrc = new BitmapImage();
//绑定该URL图片信息并进行显示
imgsrc.SetSource(imageStream.Stream);
ImgToFill.Source = imgsrc;
ImgToFill.Visibility = Visibility.Visible;
ImgToFill.Stretch = Stretch.Fill;
}
void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
//下载过程中的进度显示
progressText.Text = e.ProgressPercentage.ToString() + "%";
progressRectangle.Width = (double)e.ProgressPercentage * 2;
}
}
}
好了,今天的内容就到这里了。
源码下载链接,请点击这里:)
TAG: silverlight, image, zip, daizhj,代震军