Silverlight读取Zip文件中的图片与视频
原创作者:Ringgo.Yao
其实在我的WebClient的使用文章中的例子就是如何使用WebClient读取zip文件中的资源,这篇文章是在其基础上增加了一些功能,从而构建出一个简单但较为完整的Demo。
首先看看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
可以使用 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;
}
}
}
源代码下载
Silverlight读取Zip文件中的图片与视频相关推荐
- Python读取zip文件中的图片(无需解压)
对于包含大量图片的大型zip文件而言,解压非常费时间,同时解压过程也会需要更大的硬盘空间.这个时候,如果Python能直接读取到zip中的图片的话,就非常有用了. 本文提供PIL和cv2两种格式的图片 ...
- python直接读取tar文件中的图片
背景: tar文件过大,解压缩太慢又占空间,希望直接读取其中的图片 分析: 分析: \color{Red}{\text{分析:}} 涉及到两方面: 1. 获取其中的文件名: 2. 读取其中的文件 具体 ...
- python3中的zip_Python3实现将文件归档到zip文件及从zip文件中读取数据的方法
''''' Created on Dec 24, 2012 将文件归档到zip文件,并从zip文件中读取数据 @author: liury_lab ''' # 压缩成zip文件 from zipfil ...
- linux 读取zip文件内容,如何从linux服务器上的大(30Gb)zip文件中提取文件
1)从大型zip文件中提取 我想从linux服务器上的大型zip文件(30Gb)中提取文件.有足够的可用磁盘空间. 我试过jar xf dataset.zip.但是,按钮已满,出现错误,无法提取所有文 ...
- 如何在 R 中读取 Zip 文件
您可以使用以下基本语法将 ZIP 文件读入 R: library(readr)#import data1.csv located within my_data.zip df <- read_cs ...
- python读取字符串指定位置字符_python读取txt文件中特定位置字符的方法
python读取txt文件中特定位置字符的方法 如下所示: # -*- coding:utf-8 -*- import sys reload(sys) sys.setdefaultencoding(' ...
- java 读取Zip文件进行写入
直接读取ZIp文件读取写入到别的文件中. package jp.co.misumi.mdm.batch;import java.io.BufferedReader; import java.io.Fi ...
- python 读取zip包中的数据
最近有一个需求,是读取zip包中的内容,然后上传到s3上.用open直接打开zip包,read的时候会报错.后来看了一下zipfile,io,这2个包,思路就是把zip包中的文件中的内容写到字节流中, ...
- 向pdf文件中插入图片及文字 java实现
向pdf文件中插入图片及文字 引入itextpdf相关依赖 <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --> ...
最新文章
- andriod studio中的显式跳转和隐式跳转
- SAP WM初阶TO单据里的Source Destination 存储类型和货架
- spring4-3-AOP-面向切面编程
- 数组内容转qstring_用Qstring给char[]数组赋值(转)
- discuz数据库相关表
- 使用Node 操作MySQL数据库
- Linux技术研究-基础篇(启动和自动挂载)
- DataContractJsonSerializer 没有using 类库找不到
- “我将 20 年前开发的操作系统迁移到 .NET 6,居然成功了”
- 动态时间规整算法_如何使用动态时间规整算法进行语音识别
- Java 拆分Word文档
- CAN通讯程序C语言,AT90CAN单片机CAN通信模块介绍及软件编程
- 入网许可证_入网许可证真伪鉴别
- C语音static、const、voilate和位运算
- 罗升阳对安卓2.3系统的总结
- 设计模式——迭代器模式(遍历王者荣耀和英雄联盟英雄信息)
- redis7 Cluster模式 集群
- Nginx 出现 403 Forbidden 的解决办法
- 【伯克利马毅老师】强化学习与最优控制综述
- 早安心语优美的心情语录
热门文章
- 盒子阴影(HTML、CSS)
- JUC本质解析+进程/线程
- c++之std::distance()函数
- 那些年我们office_那些年,我们的传奇三
- mysql多数据库并发控制_什么是数据库并发控制?数据库并发控制的主要方法是?...
- mf模型 svd++_【MF】SVD
- WSL2 下的 Docker 配置,使用网易云镜像 + 更改 docker 文件系统(否则无法 apt update)
- 【李宏毅2020 ML/DL】补充:Meta Learning - Metric-based Approach Train+Test as RNN
- 【数据结构笔记33】C实现:希尔排序、增量序列
- 三维点云学习(4)5-DBSCNA python 复现-3-kd-tree radius NN 三方库 scipy 与 sklearn速度比较