Windows Phone 图片扩展类
using System.IO;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Controls;
using Windows.Storage; namespace System.Windows.Media.Imaging
{ /// <summary>
/// ImageExtension图片附加属性
/// </summary>
public class ImageExtension : DependencyObject
{
private const string WebImageCacheDirectoryName = "WebImageCache\\";
private static String LocalFolderPath
{
get
{
return ApplicationData.Current.LocalFolder.Path + "\\";
}
} /// <summary>
/// Source附加属性
/// </summary>
public static readonly DependencyProperty SourceProperty = DependencyProperty.RegisterAttached(
"Source",
typeof(String),
typeof(ImageExtension),
new PropertyMetadata(String.Empty, OnSourceChanged)
); /// <summary>
/// 设置图片源地址
/// </summary>
/// <param name="image">Image控件</param>
/// <param name="value">图片地址</param>
public static void SetSource(Image image, String value)
{
image.SetValue(SourceProperty, value);
} /// <summary>
/// 获取图片地址
/// </summary>
/// <param name="image">Image控件</param>
/// <returns></returns>
public static String GetSource(Image image)
{
return (String)image.GetValue(SourceProperty);
} private static void OnSourceChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
{
String value = args.NewValue as String;
Image image = target as Image;
if (String.IsNullOrEmpty(value) == true || image == null)
{
return;
}
if (value.StartsWith("http"))
{
image.Source = GetBitmapImageFromWeb(value);
}
else
{
image.Source = GetBitmapImageFromLocalFolder(value);
}
} /// <summary>
/// 获取独立存储图片
/// </summary>
/// <param name="imageRelativePath"></param>
/// <returns></returns>
public static BitmapImage GetBitmapImageFromLocalFolder(String imageRelativePath)
{
String imageAbsolutePath = LocalFolderPath + imageRelativePath;
return new BitmapImage(new Uri(imageAbsolutePath, UriKind.Absolute));
} /// <summary>
/// 获取网络图片
/// </summary>
/// <param name="imageUrl"></param>
/// <returns></returns>
public static BitmapImage GetBitmapImageFromWeb(String imageUrl)
{
String imageLoaclPath = MapLocalFilePath(imageUrl);
if (File.Exists(imageLoaclPath))
{
return new BitmapImage(new Uri(imageLoaclPath, UriKind.Absolute));
}
BitmapImage bitmapImage = new BitmapImage(new Uri(imageUrl, UriKind.Absolute));
bitmapImage.ImageFailed += bitmapImage_ImageFailed;
bitmapImage.ImageOpened += bitmapImage_ImageOpened;
return bitmapImage;
} private static void bitmapImage_ImageOpened(object sender, RoutedEventArgs e)
{
BitmapImage bitmapImage = (BitmapImage)sender;
bitmapImage.ImageOpened -= bitmapImage_ImageOpened;
String imageName = MapImageUrlToImageName(bitmapImage.UriSource.ToString());
SaveImageToLocalFolder(bitmapImage, WebImageCacheDirectoryName, imageName);
} private static void bitmapImage_ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
BitmapImage bitmapImage = (BitmapImage)sender;
bitmapImage.ImageFailed -= bitmapImage_ImageFailed;
} /// <summary>
/// 获得图片数据流
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public static byte[] ReadImageBytes(Image image)
{
if (image == null)
{
return new byte[];
}
return ReadImageBytes(image.Source as BitmapSource);
} /// <summary>
/// 获得图片数据流
/// </summary>
/// <param name="bitmapSource"></param>
/// <returns></returns>
public static byte[] ReadImageBytes(BitmapSource bitmapSource)
{
byte[] imageBytes = new byte[];
MemoryStream imageStream = ReadImageStream(bitmapSource);
if (imageStream == null)
{
return imageBytes;
}
imageBytes = new byte[imageStream.Length];
imageStream.Read(imageBytes, , imageBytes.Length);
imageStream.Dispose();
imageStream.Close();
return imageBytes;
} /// <summary>
/// 获得图片数据流
/// </summary>
/// <param name="bitmapSource">位图数据源</param>
/// <returns></returns>
public static MemoryStream ReadImageStream(BitmapSource bitmapSource)
{
if (bitmapSource == null)
{
return null;
}
MemoryStream imageStream = new MemoryStream();
WriteableBitmap writeableBitmap = new WriteableBitmap(bitmapSource);
writeableBitmap.SaveJpeg(imageStream, bitmapSource.PixelWidth, bitmapSource.PixelHeight, , );
imageStream.Seek(, SeekOrigin.Begin);
return imageStream;
} /// <summary>
/// 获得图片数据流
/// </summary>
/// <param name="image">图片控件</param>
/// <returns></returns>
public static MemoryStream ReadImageStream(Image image)
{
if (image == null)
{
return null;
}
return ReadImageStream(image.Source as BitmapSource);
} /// <summary>
/// 保存图片
/// </summary>
/// <param name="image">图片控件</param>
/// <param name="relativePath">独立存储的相对路径</param>
/// <param name="imageName">文件名</param>
/// <returns></returns>
public static Boolean SaveImageToLocalFolder(Image image, String relativePath, String imageName)
{
if (image == null)
{
return false;
}
return SaveImageToLocalFolder(image.Source as BitmapSource, relativePath, imageName);
} /// <summary>
/// 保存图片
/// </summary>
/// <param name="bitmapSource">位图数据源</param>
/// <param name="relativePath">独立存储的相对路径</param>
/// <param name="imageName">文件名</param>
/// <returns></returns>
public static Boolean SaveImageToLocalFolder(BitmapSource bitmapSource, String relativePath, String imageName)
{
return SaveImageToLocalFolder(ReadImageStream(bitmapSource), relativePath, imageName);
} /// <summary>
/// 保存图片
/// </summary>
/// <param name="imageStream">图片数据流</param>
/// <param name="relativePath">独立存储的相对路径</param>
/// <param name="imageName">文件名</param>
/// <returns></returns>
public static Boolean SaveImageToLocalFolder(Stream imageStream, String relativePath, String imageName)
{
if (String.IsNullOrEmpty(imageName))
{
return false;
}
if (imageStream == null)
{
return false;
}
if (String.IsNullOrEmpty(relativePath) == false)
{
Directory.SetCurrentDirectory(LocalFolderPath);
if (Directory.Exists(relativePath) == false)
{
Directory.CreateDirectory(relativePath);
}
}
String imageLoaclPath = LocalFolderPath + relativePath + imageName;
FileStream fileStream = File.Create(imageLoaclPath);
imageStream.CopyTo(fileStream, (Int32)imageStream.Length);
fileStream.Flush(); imageStream.Dispose();
imageStream.Close();
fileStream.Dispose();
fileStream.Close();
return true;
} private static String MapLocalFilePath(String fileName)
{
if (String.IsNullOrEmpty(fileName))
{
return String.Empty;
}
if (fileName.StartsWith("http"))
{
return LocalFolderPath + WebImageCacheDirectoryName + MapImageUrlToImageName(fileName);
}
if (fileName.StartsWith("file"))
{
return fileName.Substring();
}
return LocalFolderPath + fileName;
} private static String MapImageUrlToImageName(String imageUrl)
{
return MD5.GetMd5String(imageUrl) + ".img";
}
} }
可 自动缓存图片 自动读取缓存图片
本扩展作者为 北京-乱舞春秋 来自QQ群(
182659848
)

Windows Phone 图片扩展类的更多相关文章
- PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 && Linux下的ZipArchive配置开启压缩 &&搞个鸡巴毛,写少了个‘/’号,浪费了一天
PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有 ...
- windows的各种扩展名详解
Windows系统文件按照不同的格式和用途分很多种类,为便于管理和识别,在对文件命名时,是以扩展名加以区分的,即文件名格式为: 主文件名.扩展名.这样就可以根据文件的扩展名,判定文件的种类,从而知道其 ...
- Thinkphp编辑器扩展类kindeditor用法
一, 使用前的准备. 使用前请确认你已经建立好了一个Thinkphp站点项目. 1,Keditor.class.php和JSON.class.php 是编辑器扩展类文件,将他们拷贝到你的站点项目的Th ...
- bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...
- PHP 图片缩放类
<?php /** * 图片压缩类:通过缩放来压缩. * 如果要保持源图比例,把参数$percent保持为1即可. * 即使原比例压缩,也可大幅度缩小.数码相机4M图片.也可以缩为700KB左右 ...
- 得到windows聚焦图片(windows 10)
有些Windows聚焦图片确实很漂亮,很希望保留下来,但是Windows聚焦图片总更好,网上有得到聚焦图片的方法,每次都手动去弄真麻烦,于是自己编了一个小程序,自动得到Windows聚焦图片,下面是运 ...
- django-rest-framework框架 第二篇 之Mixin扩展类
Mixin扩展类 ['列表操作','过滤','搜索','排序'] <一>:<1>创建项目: 配置 urls 主路由 配置model文件(举个例子,就以book为模 ...
- ios开发总结:Utils常用方法等收集,添加扩展类,工具类方法,拥有很多方便快捷功能(不断更新中。。。)
BOBUtils 工具大全 本人github开源和收集功能地址:https://github.com/niexiaobo [对ios新手或者工作一年以内开发人员很有用处] 常用方法等收集.添加扩展类. ...
- 使用 Python 获取 Windows 聚焦图片
Windows 聚焦图片会定期更新,拿来做壁纸不错,它的目录是: %localappdata%\Packages\Microsoft.Windows.ContentDeliveryManager_cw ...
随机推荐
- POJ-1422 Air Raid---二分图匹配&最小路径覆盖
题目链接: https://vjudge.net/problem/POJ-1422 题目大意: 有n个点和m条有向边,现在要在点上放一些伞兵,然后伞兵沿着图走,直到不能走为止 每条边只能是一个伞兵走过 ...
- segment and section for c++ elf
http://blog.csdn.net/jiafu1115/article/details/12992497 写一个汇编程序保存成文本文件max.s. 汇编器读取这个文本文件转换成目标文件max.o ...
- Java小吐槽
简单说明,所有小吐槽都基于我的.NET经验,作为Java初学者,肯定有贻笑大方之处,欢迎之处,共同学习,共同进步. 1. The public type XXXXXXXX must be define ...
- css3之Media Queries 媒体查询
一.初步了解 Media Queries是CSS3新增加的一个模块功能,其最大的特点就是通过css3来查询媒体,然后调用对应的样式. 了解Media Queries之前需要了解媒体类型以及媒体特性: ...
- 为什么实例没有prototype属性?什么时候对象会有prototype属性呢?
为什么实例没有prototype属性?什么时候对象会有prototype属性呢? javascript loudou 1月12日提问 关注 9 关注 收藏 6 收藏,554 浏览 问题对人有帮助,内容 ...
- mysql如何查看错误代码具体释义?(基于perror)
mysql如何查看错误代码具体释义? 关键词:mysql错误代码,mysql错误号 perror 错误号
- P2096 最佳旅游线路
最大字段和加贪心 算长了个见识吧 #include<iostream> #include<cstdio> #include<algorithm> using nam ...
- Eclipse 修改默认工作空间
第一次启动Eclipse时会弹出对话框,让你进行Workspace Launcher,也就是设置Eclipse的项目存放路径.但是,当你勾选“Use this as the default and d ...
- Maven tomcat插件 远程发布【Learn】
Tomcat配置修改: ①.conf/tomcat-users.xml <role rolename="manager-gui"/> <role rolename ...
- jquery 筛选元素 (3)
.addBack() 添加堆栈中元素集合到当前集合中,一个选择性的过滤选择器. .addBack([selector]) selector 一个字符串,其中包括一个选择器表达式,匹配当前元素集合,不包 ...