在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>

    接下来是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;
        }

    }
}


       好了,今天的内容就到这里了。


本文转自 daizhenjun 51CTO博客,原文链接:http://blog.51cto.com/daizhj/82425,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值