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

Silverlight读取Zip文件中的图片与视频

置顶声明:本文原创版权归 博客 Ringgo.Yao 所有,如有转载,请按如下方式详细标明原文作者及出处,以示尊重!!
原创作者: Ringgo.Yao

    其实在我的 WebClient的使用文章中的例子就是如何使用WebClient读取zip文件中的资源,这篇文章是在其基础上增加了一些功能,从而构建出一个简单但较为完整的Demo。

    首先看看Demo的截图:

                     

2

3

1

   下面我将一步步展示实现这个Demo的过程,这个需求就是读出Zip文件中的图片与视频。

    Demo整体架构:

                         

捕获

   

    首先我们准备几张图片和视频,然后将其压缩至resource.zip文件中,做完之后,我们建立一个resource.xml文件记录压缩包内的资源

      <?xml version="1.0" encoding="utf-8" ?> 
      <files>
      <file type="video" name="a.wmv"/>
      <file type="image" name="1.jpg"/>
      <file type="image" name="2.jpg"/>
      <file type="image" name="3.jpg"/>
      <file type="image" name="4.jpg"/>
      </files>

    这个xml文件就记录了文件的类型和名称,完成之后我们将其压缩至resource.zip中(请注意:这一步对后面读取流会有影响)

    现在我们将UI设计好

         <Image x:Name="Image" />
         <MediaElement x:Name="Video" />
         <StackPanel HorizontalAlignment="Left" VerticalAlignment="Bottom" Opacity="0.5" Orientation="Horizontal" Margin="5,0" Name="stack">
            <Button Content="Prev" x:Name="PrevButton"  Height="30" Margin="0,5,0" Width="80" Opacity="1" Cursor="Hand" IsEnabled="False"  />
            <Button  x:Name="NextButton" Height="30" Margin="0,0" Width="80" Opacity="1" Content="Next" Cursor="Hand" IsEnabled="False"  />
         </StackPanel>

    在UI上放置了一个Image和MediaElement控件来显示读取的资源

    下面我们开始建立ResourceInfo.cs文件

       public enum ResourceType
      {
        Video,
        Image
      }
      public class ResourceInfo
        public ResourceType Type
        {
            get;  set;
        }
        public string Name
            get; set;
       }

     文件的类型以枚举的形式表示,现在我们就在MainPage.xaml.cs中以WebClient完成数据的读取工作

     先声明3个类成员变量:

       private StreamResourceInfo zip;
       private List<ResourceInfo> resourceInfo;
       private int index = 0;

     现在我们就先获取流,其完整代码

       public void Load(object sender,RoutedEventArgs e)
            Uri uri = new Uri(HtmlPage.Document.DocumentUri,"resource.zip");
            WebClient webClient = new WebClient();
            webClient.OpenReadCompleted += (obj,args) => 
            {
                if (args.Error != null)
                {
                    return;
                }
                // 这几步将读出的流信息封装到reader中,这样便于后面使用Linq To Xml操作
                zip = new StreamResourceInfo(args.Result,null);
                StreamResourceInfo maininfo = Application.GetResourceStream(zip,new Uri("resource.xml",UriKind.Relative));
                StreamReader reader = new StreamReader(maininfo.Stream);
                XDocument doc = XDocument.Load(reader);
                var file = from c in doc.Descendants("file")
                           select new ResourceInfo
                           {
                               Type = (ResourceType)Enum.Parse(typeof(ResourceType),c.Attribute("type").Value,true),monospace; font-size:11px">                               Name = c.Attribute("name").Value
                           };
                resourceInfo = new List<ResourceInfo>();
                resourceInfo.AddRange(file);
                this.PrevButton.IsEnabled = true;
                this.NextButton.IsEnabled = true;
                display(resourceInfo[0]);
            };
             webClient.OpenReadAsync(uri);
         public void display(ResourceInfo resource)
            //获取相应的流数据
            StreamResourceInfo media = Application.GetResourceStream(zip,new Uri(resource.Name,monospace; font-size:11px">            switch (resource.Type)
                case ResourceType.Image:
                    Image.Visibility = Visibility.Visible;
                    Video.Visibility = Visibility.Collapsed;
                    BitmapImage image = new BitmapImage();
                    image.SetSource(media.Stream);
                    Image.source = image;
                    break;
                case ResourceType.Video:
                    Image.Visibility = Visibility.Collapsed;
                    Video.Visibility = Visibility.Visible;
                    Video.SetSource(media.Stream);
                    Video.Play();
            }
         }

          事实上加载这段代码后,我们已经可以将xml文件中标注的第一个资源a.wmv在页面进行成功的播放了

          我们继续界面上的Button实现的循环显示上一条,下一条资源功能

         private void StopVideo()
            if (resourceInfo[index].Type == ResourceType.Video)
                Video.Stop();
        private void PrevButton_Click(object sender,monospace; font-size:11px">            StopVideo();
            if (--index < 0)
                index = resourceInfo.Count - 1;
            display(resourceInfo[index]);
        private void NextButton_Click(object sender,monospace; font-size:11px">            if (++index >=resourceInfo.Count)
                index = 0;
        }

         如此我们就完成了这个Demo

 

 

 

可以使用 StreamResourceInfo 处理碰巧为包(XAP 或 ZIP 文件)的流。如果已经返回作为 WebClient 请求结果的异步流并且该返回流确实是一个包含多个部件的包,则这个类很有用。若要获取这些部件,必须在 URI(在 GetResourceStream 调用中指定)中请求每个部件,同时在 zipPackageStreamResourceInfo 参数中将初始包指定为 StreamResourceInfo

下面一个实例讲解:

代码如下:

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.Windows.Media.Imaging; // BitmapImage
using System.Windows.Resources; // StreamResourceInfo

namespace StreamResourceInfoDemo
{
   
public partial class MainPage : UserControl
    {
       
public MainPage()
        {
            InitializeComponent();        

           
//在该解决方案中添加的另一个应用程序项目
           
//注意点:在加载的资源前加上自己的项目的名称 /StreamResourceInfoDemo;component/
            Image img1 = LoadImage(
               
"/SilverlightApplication;component/EmbeddedInApplicationAssembly.jpg");
           
this.stackPanel.Children.Add(img1);

           
// 设为起始页的Silverlight应用程序包中jpg文件资源
           
//注意点:即使设为启动项目的应用程序包也要在  /StreamResourceInfoDemo;component/
            Image img2 = LoadImage("/StreamResourceInfoDemo;component/IncludedInApplicationPackage.jpg");
           
this.stackPanel.Children.Add(img2);

          
//解决方案中添加的Silverlight类库项目
           
//注意点:(1)在加载资源前面加上类库名称
           
//        (2)在被设为起始页的应用程序中添加对该程序集的引用
            Image img3 = LoadImage(
               
"/SilverlightClassLibrary;component/EmbeddedInLibraryAssembly.jpg");
           
this.stackPanel.Children.Add(img3);
        }

       
public Image LoadImage(string relativeUriString)
        {
           
// Get the image stream at the specified URI that
           
// is relative to the application package root.
            Uri uri = new Uri(relativeUriString,UriKind.Relative);
            StreamResourceInfo sri
= Application.GetResourceStream(uri);

           
// Convert the stream to an Image object.
            BitmapImage bi = new BitmapImage();
            bi.SetSource(sri.Stream);
            Image img
= new Image();
            img.source
= bi;
           
return img;
        }

    }
}
源代码下载

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

相关推荐