微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Silverlight中读取指定URL图片包数据

在silverlight开发,允许我们获取指定URL压缩包(zip)并读取其中的图片文件。而实现这个功能也非常简单。下面是运行效果图:
              

     
    
      首先,我们需要建立一个silverligth application ,名称为:DownLoadImg.
    
      下面就是相应的page.xaml代码:    
< Grid  x:Name ="LayoutRoot"  Background ="White" >
    
Grid.RowDeFinitions
        
RowDeFinitio Height ="250"   /> ="100" ="50" </ StackPanel  Grid.Row ="1" ListBox  ="ImageList" Button  ="Download"  Click ="StartDownLoad"  Content ="下载该图片" StackPanel

    
="0"             
        
ScrollViewer  HorizontalScrollBarVisibility ="Auto"
            
Image  ="ImgToFill"            
            
Image ScrollViewer ="2" Canvas  Canvas.Top ="70" Rectangle   Name ="progressRectangle"  Height ="10"  Width ="0"  Fill ="AliceBlue" Rectangle  ="12"   Width ="202"  strokeThickness ="1"  stroke ="Black" TextBlock  ="progresstext"  Canvas.Left ="210"  Text ="0%"  FontSize Canvas
Grid

    接下来是 page.xaml.cs(相关内容见注释):
using  System;
 System.Collections.Generic;
 System.Linq;
 System.Net;
 System.Windows;
 System.Windows.Controls;
 System.Windows.Documents;
 System.Windows.Input;
 System.Windows.Media;
 System.Windows.Media.Animation;
 System.Windows.Shapes;

 System.IO;
 System.Windows.Resources;
 System.Windows.Media.Imaging;

namespace  DownLoadImg
{
    
public partial class  Page : UserControl
    {
        WebClient wc 
= new  WebClient();

        
 Page()
        {
            InitializeComponent();
            
this .Loaded  +=  RoutedEventHandler(Page_Loaded);
        }

        
// 加载图片列表数据          void  Page_Loaded( object  sender, RoutedEventArgs e)
        {
            ImageList.Items.Add(
" 1.jpg " );
            ImageList.Items.Add(
2.png );
        }
        
实例化下载设置  StartDownLoad(  o, EventArgs e)
        {
            
初始化相应控件信息             ImgToFill.Visibility   Visibility.Collapsed;
            progressRectangle.Width 
  0 ;
            progresstext.Text 
0% ;
            
绑定下载过程中处理的事件             wc.OpenReadCompleted   OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            wc.DownloadProgressChanged 
 DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
            
开始异步读取压缩包中的文件信息             wc.OpenReadAsync(  Uri( img.zip , UriKind.Relative), ImageList.SelectedItem);
        }
        
        
 wc_OpenReadCompleted( ottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; color:rgb(0, OpenReadCompletedEventArgs e)
        {
           
实例化流资源信息,准备获取其中的图片数据             StreamResourceInfo sri   StreamResourceInfo(e.Result  as  Stream,  null );
            
要读取的图片路径信息             String sURI   e.UserState   String;
            
从流资源中获取指定的URL图片流信息             StreamResourceInfo imagestream   Application.GetResourceStream(sri,0)"> Uri(sURI, UriKind.Relative));
            BitmapImage imgsrc 
 BitmapImage();
            
绑定该URL图片信息并进行显示             imgsrc.SetSource(imagestream.Stream);
            ImgToFill.source 
 imgsrc;
            ImgToFill.Visibility 
 Visibility.Visible;
            ImgToFill.Stretch 
 Stretch.Fill;
        }

        
 wc_DownloadProgressChanged( ottom:0px; padding-left:0px; margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; color:rgb(0, DownloadProgressChangedEventArgs e)
        {
            
下载过程中的进度显示             progresstext.Text   e.Progresspercentage.ToString()  + % ;
            progressRectangle.Width 
 ( double )e.Progresspercentage  * 2 ;         }     } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐