///<summary>

    /// 独立存储缓存的图片源

    /// 用法:item.img = new StorageCachedImage(newUri(http://www.baidu.com/12.jpg));

    ///</summary>

    public sealed class StorageCachedImage : BitmapSource

    {

        private readonly Uri uriSource;

        private readonly string filePath;

        private const string CacheDirectory = "CachedImages";

        static StorageCachedImage()

        { 

            //创建缓存目录

            using (var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())

            {

                if (!isolatedStorageFile.DirectoryExists(CacheDirectory))

                {

                    isolatedStorageFile.CreateDirectory(CacheDirectory);

                }

            }

        }

        ///<summary>

        /// 创建一个独立存储缓存的图片源

        ///</summary>

        ///<param name="uriSource"></param>

        public StorageCachedImage(Uri uriSource)

        {

            this.uriSource = uriSource;

            string sUrl = uriSource.AbsolutePath;

            //文件路径

            filePath = Path.Combine(CacheDirectory, sUrl.Substring(sUrl.LastIndexOf("/") + 1, sUrl.Length - sUrl.LastIndexOf("/") - 1));

            OpenCatchSource();

        }

        ///<summary>

        /// 打开缓存源

        ///</summary>

        private void OpenCatchSource()

        {

            //网络可用时,下载图片(网络不可用时读取本地缓存)

            if (CommonConst.CheckNetWorking())

            {

                SetWebStreamSource();

            }

            else

            {

                bool exist;

                using (var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())

                {

                    exist = isolatedStorageFile.FileExists(filePath);

                }

                if (exist)

                {

                    SetCacheStreamSource();

                }

                else

                {

                    //SetWebStreamSource();

                }

            }

        }

        ///<summary>

        /// 设置缓存流到图片

        ///</summary>

        private void SetCacheStreamSource()

        {

            using (var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())

            using (var stream = isolatedStorageFile.OpenFile(filePath, FileMode.Open, FileAccess.Read))

            {

                SetSource(stream);

            }

        }

        ///<summary>

        /// 下载Uri中的图片

        ///</summary>

        private void SetWebStreamSource()

        {

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(uriSource);

            httpWebRequest.AllowReadStreamBuffering = true;

            httpWebRequest.BeginGetResponse(ResponseCallBack, httpWebRequest);

        }

        ///<summary>

        /// 下载回调

        ///</summary>

        ///<param name="asyncResult"></param>

        private void ResponseCallBack(IAsyncResult asyncResult)

        {

            var httpWebRequest = asyncResult.AsyncState as HttpWebRequest;

            if (httpWebRequest == null) return;

            try

            {

                var response = httpWebRequest.EndGetResponse(asyncResult);

                using (var stream = response.GetResponseStream())

                using (var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())

                using (var fileStream = isolatedStorageFile.OpenFile

                (filePath, FileMode.OpenOrCreate, FileAccess.Write))

                {

                    CopyStream(stream, fileStream);

                }

                Dispatcher.BeginInvoke(SetCacheStreamSource);

            }

            catch (Exception err)

            {

                //Debug.WriteLine(err.Message);

            }

        }

        private static void CopyStream(System.IO.Stream input, IsolatedStorageFileStream output)

        {

            byte[] buffer = new byte[32768];

            long TempPos = input.Position;

            int readCount;

            do

            {

                readCount = input.Read(buffer, 0, buffer.Length);

                if (readCount > 0)

                {

                    output.Write(buffer, 0, readCount);

                }

            } while (readCount > 0);

            input.Position = TempPos;

        }

    }

WP_从独立存储区读取缓存的图片的更多相关文章

  1. C#从证书存储区读取证书

    using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptograph ...

  2. WP开发图片保存到独立存储并从独立存储中读取

    需要添加引用命名空间 using System.IO; using System.IO.IsolatedStorage; 1.将图片保存到独立存储空间 using (IsolatedStorageFi ...

  3. 调用EF的存储过程报“存储区数据提供程序返回的数据读取器所具有的列数对于所请求的查询不够”问题

    在运用Entity Framework调用存储过程的时候,遇到"调用EF的存储过程报"调用EF的存储过程报“存储区数据提供程序返回的数据读取器所具有的列数对于所请求的查询不够”问题 ...

  4. 第21章 DMA—直接存储区访问

    本章参考资料:<STM32F76xxx参考手册>DMA控制器章节. 学习本章时,配合<STM32F76xxx参考手册>DMA控制器章节一起阅读,效果会更佳,特别是涉及到寄存器说 ...

  5. 第21章 DMA—直接存储区访问—零死角玩转STM32-F429系列

    第21章     DMA—直接存储区访问 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fi ...

  6. DMA—直接存储区访问

    本章参考资料:< STM32F4xx 中文参考手册> DMA 控制器章节.学习本章时,配合< STM32F4xx 中文参考手册> DMA 控制器章节一起阅读,效果会更佳,特别是 ...

  7. PHP 开发 APP 接口 学习笔记与总结 - APP 接口实例 [3] 首页 APP 接口开发方案 ② 读取缓存方式

    以静态缓存为例. 修改 file.php line:11 去掉 path 参数(方便),加上缓存时间参数: public function cacheData($k,$v = '',$cacheTim ...

  8. 转:内存区划分、内存分配、常量存储区、堆、栈、自由存储区、全局区[C++][内存管理][转载]

    内存区划分.内存分配.常量存储区.堆.栈.自由存储区.全局区[C++][内存管理][转载] 一. 在c中分为这几个存储区1.栈 - 由编译器自动分配释放2.堆 - 一般由程序员分配释放,若程序员不释放 ...

  9. WP8 独立存储 总结3(应用设置)

    •可在独立存储中使用ApplicationSettings对象•在独立存储中存储键/值对的Dictionary方式存储 •存储的对象将永久保存 在应用设置中保存数据 void saveString(s ...

随机推荐

  1. BIP_开发案例06_以RB.RDF为数据源BIP.RTF为模板的简单例子(案例)

    2014-05-31 Created By BaoXinjian

  2. codeforces 557 D. Vitaly and Cycle 组合数学 + 判断二分图

    D. Vitaly and Cycle       time limit per test 1 second memory limit per test 256 megabytes input sta ...

  3. POJ 2393 贪心 简单题

    有一家生产酸奶的公司,连续n周,每周需要出货numi的单位,已经知道每一周生产单位酸奶的价格ci,并且,酸奶可以提前生产,但是存储费用是一周一单位s费用,问最少的花费. 对于要出货的酸奶,要不这一周生 ...

  4. 数据库还原总提示空间不够,磁盘卷 'D:\' 上的可用空间不足,无法创建数据库

    从数据库上备份下来bak格式的数据库文件之后,在本地数据库欢迎的时候总是提示空间不够. 这种情况一般在从64位电脑上面备份的数据库文件,还原到32位的sqlsever上面. System.Data.S ...

  5. JAVA 根据用户输入数据求某年到某年有多少天

    实例: import java.util.*; //求某年到某年有多少天 public class Test{ public static void main(String[] args){ Scan ...

  6. JAVA 99乘法表实例

    实例: public class Test{ public static void main(String[] args){ for(int i=1;i<=9;i++){ for(int j=1 ...

  7. 帝国CMS 6.0功能解密之新版结合项功能,帝国结合项使用

    可以用来做A-Z信息检索    某字段等于多少,输出  等等 帝国CMS6.0在继承以往版本结合项功能的基础上又新增很多特性,更强大.今天我们就专门来讲解6.0的结合项改进. 回顾下以往版本的结合项语 ...

  8. .Net调用非托管代码数据类型不一致的问题

    什么是Net互操作?.Net不能直接操作非托管代码,这时就需要互操作了.   c#中调用非托管c++函数,此函数又包含指向某个结构的指针,譬如指向c#中的byte数组.对于这样的参数,考虑到非托管变量 ...

  9. MongoDB Tool

    robomongo MongoBooster: [推薦]MongoChef:http://3t.io/mongochef/download/ MongoVUE 是个比较好用的MongoDB客户端,不过 ...

  10. C++学习44 格式化输出,C++输出格式控制

    在输出数据时,为简便起见,往往不指定输出的格式,由系统根据数据的类型采取默认的格式,但有时希望数据按指定的格式输出,如要求以十六进制或八进制形式输出一个 整数,对输出的小数只保留两位小数等.有两种方法 ...