在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 ;
}
}
}
好了,今天的内容就到这里了。
源码下载链接,请点击这里:) TAG: silverlight,image,zip,daizhj,代震军
以上是内存溢出为你收集整理的在Silverlight中读取指定URL图片包数据全部内容,希望文章能够帮你解决在Silverlight中读取指定URL图片包数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)