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群(

WP8 Coding4Fun 

182659848

 

Windows Phone 图片扩展类的更多相关文章

  1. PHP扩展类ZipArchive实现压缩解压Zip文件和文件打包下载 && Linux下的ZipArchive配置开启压缩 &&搞个鸡巴毛,写少了个‘/’号,浪费了一天

    PHP ZipArchive 是PHP自带的扩展类,可以轻松实现ZIP文件的压缩和解压,使用前首先要确保PHP ZIP 扩展已经开启,具体开启方法就不说了,不同的平台开启PHP扩增的方法网上都有,如有 ...

  2. windows的各种扩展名详解

    Windows系统文件按照不同的格式和用途分很多种类,为便于管理和识别,在对文件命名时,是以扩展名加以区分的,即文件名格式为: 主文件名.扩展名.这样就可以根据文件的扩展名,判定文件的种类,从而知道其 ...

  3. Thinkphp编辑器扩展类kindeditor用法

    一, 使用前的准备. 使用前请确认你已经建立好了一个Thinkphp站点项目. 1,Keditor.class.php和JSON.class.php 是编辑器扩展类文件,将他们拷贝到你的站点项目的Th ...

  4. bootstrap-wysiwyg 结合 base64 解码 .net bbs 图片操作类

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...

  5. PHP 图片缩放类

    <?php /** * 图片压缩类:通过缩放来压缩. * 如果要保持源图比例,把参数$percent保持为1即可. * 即使原比例压缩,也可大幅度缩小.数码相机4M图片.也可以缩为700KB左右 ...

  6. 得到windows聚焦图片(windows 10)

    有些Windows聚焦图片确实很漂亮,很希望保留下来,但是Windows聚焦图片总更好,网上有得到聚焦图片的方法,每次都手动去弄真麻烦,于是自己编了一个小程序,自动得到Windows聚焦图片,下面是运 ...

  7. django-rest-framework框架 第二篇 之Mixin扩展类

    Mixin扩展类     ['列表操作','过滤','搜索','排序'] <一>:<1>创建项目: 配置 urls 主路由    配置model文件(举个例子,就以book为模 ...

  8. ios开发总结:Utils常用方法等收集,添加扩展类,工具类方法,拥有很多方便快捷功能(不断更新中。。。)

    BOBUtils 工具大全 本人github开源和收集功能地址:https://github.com/niexiaobo [对ios新手或者工作一年以内开发人员很有用处] 常用方法等收集.添加扩展类. ...

  9. 使用 Python 获取 Windows 聚焦图片

    Windows 聚焦图片会定期更新,拿来做壁纸不错,它的目录是: %localappdata%\Packages\Microsoft.Windows.ContentDeliveryManager_cw ...

随机推荐

  1. 显示C++ vector中的数据

    C++ 中的vector是一个容器数据类型,不能使用cout直接显示容器中的值. 以下程序中,myvector 是一个vector数据类型.将myvector替换为需要输出的vector. for(i ...

  2. 关于(void**)及其相关的理解

    #define LOADBASSFUNCTION (f) *((void **)&f)=(void*)GetProcAddress (hBass,# f) 这一句话使用*((void**)&a ...

  3. pat甲级1107

    1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your ...

  4. python pip安装报错python setup.py egg_info failed with error code 1

    安装locust遇到点问题折腾了好一会儿,记录一下. 使用命令pip install locustio提示python setup.py egg_info  failed with error cod ...

  5. 模仿ArcGIS用Graphics重绘的直方图分级调节器

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  6. IOS 打开照相机 打开相册

    /** * 打开照相机 */ - (void)openCamera { if (![UIImagePickerController isSourceTypeAvailable:UIImagePicke ...

  7. 位图算法-hash算法的后继应用

    判断集合中存在重复是常见编程任务之一,当集合中数据量比较大时我们通常希望少进行几次扫描,这时双重循环法就不可取了.位图法比较适合于这种情况,它的做法是按照集合中最大元素max创建一个长度为max+1的 ...

  8. ELF文件中section与segment的区别

    http://blog.csdn.net/joker0910/article/details/7655606 1. ELF中的section主要提供给Linker使用, 而segment提供给Load ...

  9. webpack-dev-middleware插件的使用

    我们在使用webpack 编译文件时,每次改动文件都要去重新编译,是不是很麻烦,这时候我们就用到了webpack-dev-middleware 插件,该插件对更改的文件进行监控,编译, 一般和 web ...

  10. js循环读取json数据,将读取到的数据用js写成表格

    ①js循环读取json数据的方式: var data=[{"uid":"2688","uname":"*江苏省南菁高级中学 022 ...