在Silverlight中读取指定URL图片包数据
生活随笔
收集整理的這篇文章主要介紹了
在Silverlight中读取指定URL图片包数据
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
????? 在silverlight開發(fā),允許我們獲取指定URL壓縮包(zip)并讀取其中的圖片文件。而實現(xiàn)這個功能也
非常簡單。下面是運行效果圖:
????????????? ?????
???
????? 首先,我們需要建立一個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);
????????}
????????//加載圖片列表數(shù)據(jù)
????????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)
????????{
???????????//實例化流資源信息,準備獲取其中的圖片數(shù)據(jù)
????????????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,代震軍
轉載于:https://www.cnblogs.com/daizhj/archive/2008/06/17/1222845.html
總結
以上是生活随笔為你收集整理的在Silverlight中读取指定URL图片包数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于企业的信息化建设
- 下一篇: 访问IIS元数据库失败