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 ...
随机推荐
- android+nutz后台如何上传和下载图片
android+nutz后台如何上传和下载图片 发布于 588天前 作者 yummy222 428 次浏览 复制 上一个帖子 下一个帖子 标签: 无 最近在做一个基于android的ap ...
- 2017.11.17 C++系列---用malloc动态给c++二维数组的申请与释放操作
方法一:利用二级指针申请一个二维数组. #include<stdio.h> #include<stdlib.h> int main() { int **a; //用二级指针动态 ...
- c#转载的
C#做项目时的一些经验分享 1.对于公用的类型定义,要单独抽取出来,放到单独的DLL中. 2.通过大量定义interface接口,来提高模块化程度,不同功能之间通过实现接口来面向接口编程. 3.如果项 ...
- Spring boot 集成三种拦截方式
三种拦截方式分别为: javax.servlet.Filter org.springframework.web.servlet.HandlerInterceptor org.aspectj.lang. ...
- JS将unicode码转中文方法
原理,将unicode的 \u 先转为 %u,然后使用unescape方法转换为中文. ? 1 2 3 4 <script type="text/javascript"> ...
- JavaEE权限管理系统的搭建(二)--------聚合工程项目的创建和依赖关系
本项目是一个聚合工程,所以要先搭建一个聚合工程的框架 搭建完成的项目结构图如下: 首先创建父项目:pom类型 子模块:web层的搭建,war类型 把这个两个目录标记为对应的类型 其他子模块:和serv ...
- Ipad连接电脑超时问题
同时按 HOME+POWER 硬件重启 就会在ipad上弹出信任or不信任选项了 选择信任即可
- loss 和accuracy的关系梳理
最近打算总结一下这部分东西,先记录留个脚印.
- 关于使用js下载图片
使用js进行图片下载是很常见的需求,但是解决起来却不是那么顺利. 服务器返回了一个图片地址,网上一搜基本都是用a标签的download属性,但是兼容性实在是很差.这里推荐使用blob. 上代码: va ...
- 用servlet设计OA管理系统时遇到问题
如果不加单引号会使得除变量和int类型的值不能传递 转发和重定向的区别 转发需要填写完整路径,重定向只需要写相对路径.原因是重定向是一次请求之内已经定位到了服务器端,转发则需要两次请求每次都需要完整的 ...