Silverlight讀取Zip檔案中的圖片與視頻

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   使用   io   檔案   

  首先看看Demo的:

                     

   下面我將一步步展示實現這個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,0,0" Name="stack">
            <Button Content="Prev" x:Name="PrevButton" Height="30" Margin="0,0,5,0" Width="80" Opacity="1" Cursor="Hand" IsEnabled="False" />
            <Button  x:Name="NextButton" Height="30" Margin="0,0,5,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),
                               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,UriKind.Relative));
            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();
                    break;
            }
         }

          事實上載入這段代碼後,我們已經可以將xml檔案中標註的第一個資源a.wmv在頁面進行成功的播放了

          我們繼續介面上的Button實現的迴圈顯示上一條,下一條資源功能

         private void StopVideo()
        {
            if (resourceInfo[index].Type == ResourceType.Video)
            {
                Video.Stop();
            }
        }
        private void PrevButton_Click(object sender, RoutedEventArgs e)
        {
            StopVideo();
            if (--index < 0)
            {
                index = resourceInfo.Count - 1;
            }
            Display(resourceInfo[index]);
        }
        private void NextButton_Click(object sender, RoutedEventArgs e)
        {
            StopVideo();
            if (++index >=resourceInfo.Count)
            {
                index = 0;
            }
            Display(resourceInfo[index]);
        }

         如此我們就完成了這個Demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.