wp7用独立存储空间保存服务器图片和从独立存储空间读取图片
2012-02-02 15:42
537 查看
/// <summary>
/// 使用webclient下载图片
/// </summary>
/// <param name="zimgName">图片名称</param>
/// <param name="zimgURL">图片地址</param>
public void WriteZImg(string zimgName, string zimgURL)
{
try
{
//声明图片保存路径变量
string imageSavePath = string.Empty;
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())//获取从虚拟主机域调用的应用程序所使用的用户范围的独立存储
{
string dir = "Main";//保存图片的目录
if (!storageFile.DirectoryExists(dir))//判断目录是否存在
storageFile.CreateDirectory(dir);//创建目录
imageSavePath = dir + "\\" + zimgName;//拼接图片保存路径
//long maxSize = storageFile.Quota;//用来获取独立存储的最大可用空间量
//long availabelSize = storageFile.AvailableFreeSpace;//用来获取独立存储的可用空间量
}
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
//注:由于异步下载并不会按照顺序下载,所以之前一直无法知道现在下载的到底是哪一张图片,经过网上查找资料,
//终于明白如何得知当前下载的是哪一张图片,因为
//WebClient.OpenReadAsync (Uri, Object)方法的第二个对象的含义是:一个用户定义对象,此对象将被传递给完成异步操作时所调用的方法。
//所以在下载完成的事件中可以通过OpenReadCompletedEventArgs的UserState属性来得知当前下载的是那张图片
wc.OpenReadAsync(new Uri(zimgURL, UriKind.RelativeOrAbsolute), imageSavePath); //打开流向指定资源的可读流。
}
catch (Exception e) { }
}
/// <summary>
/// 图片下载完成事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled)
{
try
{
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!storageFile.FileExists(e.UserState.ToString()))
{
using (Stream storageFileStream = storageFile.OpenFile(e.UserState.ToString(), FileMode.Create, FileAccess.Write))
{
#region 保存jpg格式的图片
//BitmapImage image = new BitmapImage();
//image.SetSource(e.Result);
//WriteableBitmap wb = new WriteableBitmap(image);
//Extensions.SaveJpeg(wb, storageFileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);
////另一种保存图片的方法
////wb.SaveJpeg(storageFileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);
////以下的方法是用来读取程应用程序包中的图片
////StreamResourceInfo resourceInfo = Application.GetResourceStream(new Uri(picPath));
////BitmapImage bitmap = new BitmapImage();
////bitmap.SetSource(resourceInfo.Stream);
#endregion
#region 保存png格式的图片
byte[] _imgBytes = new byte[e.Result.Length];
e.Result.Read(_imgBytes, 0, _imgBytes.Length);
storageFileStream.Write(_imgBytes, 0, _imgBytes.Length);
#endregion
}
}
}
}
catch (Exception ex)
{
/ /Exception handle appropriately for your app
}
}
else
{
//Either cancelled or error handle appropriately for your app
}
}
/// <summary>
/// 从独立存储空间读取图片
/// </summary>
/// <param name="zimgName">图片名称</param>
/// <returns></returns>
public BitmapImage ReadZImg(string zimgName)
{
try
{
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
BitmapImage bi = new BitmapImage();
string fileDir = "Main";
string filePath = fileDir + "\\" + zimgName;
if (storageFile.FileExists(filePath))
{
using (IsolatedStorageFileStream fileStream = storageFile.OpenFile(filePath, FileMode.Open, FileAccess.Read))
{
bi.SetSource(fileStream);
return bi;
}
}
else
{
return null;
}
}
}
catch (Exception e) { return null; }
}
/// 使用webclient下载图片
/// </summary>
/// <param name="zimgName">图片名称</param>
/// <param name="zimgURL">图片地址</param>
public void WriteZImg(string zimgName, string zimgURL)
{
try
{
//声明图片保存路径变量
string imageSavePath = string.Empty;
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())//获取从虚拟主机域调用的应用程序所使用的用户范围的独立存储
{
string dir = "Main";//保存图片的目录
if (!storageFile.DirectoryExists(dir))//判断目录是否存在
storageFile.CreateDirectory(dir);//创建目录
imageSavePath = dir + "\\" + zimgName;//拼接图片保存路径
//long maxSize = storageFile.Quota;//用来获取独立存储的最大可用空间量
//long availabelSize = storageFile.AvailableFreeSpace;//用来获取独立存储的可用空间量
}
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
//注:由于异步下载并不会按照顺序下载,所以之前一直无法知道现在下载的到底是哪一张图片,经过网上查找资料,
//终于明白如何得知当前下载的是哪一张图片,因为
//WebClient.OpenReadAsync (Uri, Object)方法的第二个对象的含义是:一个用户定义对象,此对象将被传递给完成异步操作时所调用的方法。
//所以在下载完成的事件中可以通过OpenReadCompletedEventArgs的UserState属性来得知当前下载的是那张图片
wc.OpenReadAsync(new Uri(zimgURL, UriKind.RelativeOrAbsolute), imageSavePath); //打开流向指定资源的可读流。
}
catch (Exception e) { }
}
/// <summary>
/// 图片下载完成事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled)
{
try
{
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!storageFile.FileExists(e.UserState.ToString()))
{
using (Stream storageFileStream = storageFile.OpenFile(e.UserState.ToString(), FileMode.Create, FileAccess.Write))
{
#region 保存jpg格式的图片
//BitmapImage image = new BitmapImage();
//image.SetSource(e.Result);
//WriteableBitmap wb = new WriteableBitmap(image);
//Extensions.SaveJpeg(wb, storageFileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);
////另一种保存图片的方法
////wb.SaveJpeg(storageFileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);
////以下的方法是用来读取程应用程序包中的图片
////StreamResourceInfo resourceInfo = Application.GetResourceStream(new Uri(picPath));
////BitmapImage bitmap = new BitmapImage();
////bitmap.SetSource(resourceInfo.Stream);
#endregion
#region 保存png格式的图片
byte[] _imgBytes = new byte[e.Result.Length];
e.Result.Read(_imgBytes, 0, _imgBytes.Length);
storageFileStream.Write(_imgBytes, 0, _imgBytes.Length);
#endregion
}
}
}
}
catch (Exception ex)
{
/ /Exception handle appropriately for your app
}
}
else
{
//Either cancelled or error handle appropriately for your app
}
}
/// <summary>
/// 从独立存储空间读取图片
/// </summary>
/// <param name="zimgName">图片名称</param>
/// <returns></returns>
public BitmapImage ReadZImg(string zimgName)
{
try
{
using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
{
BitmapImage bi = new BitmapImage();
string fileDir = "Main";
string filePath = fileDir + "\\" + zimgName;
if (storageFile.FileExists(filePath))
{
using (IsolatedStorageFileStream fileStream = storageFile.OpenFile(filePath, FileMode.Open, FileAccess.Read))
{
bi.SetSource(fileStream);
return bi;
}
}
else
{
return null;
}
}
}
catch (Exception e) { return null; }
}
相关文章推荐
- 图片存储架构学习:独立的图片服务器,给爱一个独立的空间
- 图片存储架构学习:独立的图片服务器,给爱一个独立的空间
- silverlight 独立存储之路径、保存图片、扩展空间注意事项
- 图片存储架构学习:独立的图片服务器,给爱一个独立的空间
- WP7独立存储之IsolatedStorageFile之图片的存储和读取
- 图片存储架构学习:独立的图片服务器,给爱一个独立的空间(转)
- 图片存储架构学习:独立的图片服务器,给爱一个独立的空间
- 图片存储架构学习:独立的图片服务器,给爱一个独立的空间
- java+pgsql实现保存图片到数据库,以及读取数据库存储的图片
- Unity 场景保存,上传服务器,在下载更新场景 IO读取图片和WWW读取图片比较
- Windows Phone实用开发技巧(39):WebBrowser引用独立存储空间中的图片
- 图片存储架构学习:独立的图片服务器
- 数据库操作_连接SQL Server数据库示例;连接ACCESS数据库;连接到 Oracle 数据库示例;SqlCommand 执行SQL命令示例;SqlDataReader 读取数据示例;使用DataAdapter填充数据到DataSet;使用DataTable存储数据库表;将数据库数据填充到 XML 文件;10 使用带输入参数的存储过程;11 使用带输入、输出参数的存储过程示;12 获得数据库中表的数目和名称;13 保存图片到SQL Server数据库示例;14 获得插入记录标识号;Exce
- 图片存储架构学习:独立的图片服务器
- WP7 IsolatedStorage--读取、保存图片文件
- wp7 独立存储空间在真机和虚拟机测试的时候数据不一样
- WP7 开发工具 查看独立存储空间中数据库内容
- 图片存储架构学习:独立的图片服务器
- java+mysql实现保存图片到数据库,以及读取数据库存储的图片
- SilverLight浏览器交互之:SilverLight用户独立存储空间中文件的创建和读取