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[0];
            }
            return ReadImageBytes(image.Source as BitmapSource);
        }

        /// <summary>
        /// 获得图片数据流
        /// </summary>
        /// <param name="bitmapSource"></param>
        /// <returns></returns>
        public static byte[] ReadImageBytes(BitmapSource bitmapSource)
        {
            byte[] imageBytes = new byte[0];
            MemoryStream imageStream = ReadImageStream(bitmapSource);
            if (imageStream == null)
            {
                return imageBytes;
            }
            imageBytes = new byte[imageStream.Length];
            imageStream.Read(imageBytes, 0, 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, 0, 100);
            imageStream.Seek(0, 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(8);
            }
            return LocalFolderPath + fileName;
        }

        private static String MapImageUrlToImageName(String imageUrl)
        {
            return MD5.GetMd5String(imageUrl) + ".img";
        }
    }

}

可  自动缓存图片 自动读取缓存图片

 

本扩展作者为 北京-乱舞春秋 来自QQ群(

WP8 Coding4Fun 
 182659848
 

posted on 2013-11-25 11:12  蔚蓝海  阅读(530)  评论(1编辑  收藏  举报