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++ new new[]详解

    精髓: operator new()完成的操作一般只是分配内存:而构造函数的调用(如果需要)是在new运算符中完成的. operator new和new 运算符是不同的,operator new只分配 ...

  2. C++学习之显式类型转换与运行时类型识别RTTI

    static_cast const_cast reinterpret_cast 运行时类型识别(RTTI) dynamic_cast 哪种情况下dynamic_cast和static_cast使用的情 ...

  3. BestCoder Round #91 1001 Lotus and Characters

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6011 题意: Lotus有nn种字母,给出每种字母的价值以及每种字母的个数限制,她想构造一个任意长度的 ...

  4. 解决SurfaceView调用setZOrderOnTop(true)遮挡其他控件的问题

    SurfaceView遮挡其他控件的项目背景: 最近在做播放器项目,由于底层实现是用Surface和OpenGL切换渲染,所以在布局里面同时使用了GLSurfaceView和SurfaceView,同 ...

  5. R 多线程和多节点并行计算

    一:R本身是单线程的,如何让其多线程跑起来,提高运算速度? 用Parallel和foreach包玩转并行计算 看完上面这篇文章就会了.说白了,要加载parallel包,再改写一下自己的代码就ok了. ...

  6. OpenCV自带dnn的Example研究(5)— segmentation

    这个博客系列,简单来说,今天我们就是要研究 https://docs.opencv.org/master/examples.html下的 6个文件,看看在最新的OpenCV中,它们是如何发挥作用的. ...

  7. 【洛谷P3390】矩阵快速幂

    矩阵快速幂 题目描述 矩阵乘法: A[n*m]*B[m*k]=C[n*k]; C[i][j]=sum(A[i][1~n]+B[1~n][j]) 为了便于赋值和定义,我们定义一个结构体储存矩阵: str ...

  8. Git配置和常用命令

    Git配置 git config --global user.name "hunng" git config --global user.email "huangthin ...

  9. 第44章 MPU6050传感器—姿态检测—零死角玩转STM32-F429系列

    第44章     MPU6050传感器—姿态检测 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.co ...

  10. Java关键字transient和volatile小结

    转自:http://heaven-arch.iteye.com/blog/1160693 transient和volatile两个关键字一个用于对象序列化,一个用于线程同步,都是Java中比较高阶的话 ...