HTML5技术

WPF自定义控件与样式(12)-缩略图ThumbnailImage /gif动画图/图片列表 - /*梦里花落知多少*/(2)

字号+ 作者:H5之家 来源:H5之家 2015-12-02 08:36 我要评论( )

缩略图创建服务接口 IThumbnailProvider{ 创建缩略图。fileName:文件路径;width:图片宽度;height:高度 /// /summary ImageSource GenereateThumbnail( object fileSource, double width, double height);} 如上面

缩略图创建服务接口 IThumbnailProvider { 创建缩略图。fileName:文件路径;width:图片宽度;height:高度 /// </summary> ImageSource GenereateThumbnail(object fileSource, double width, double height); }

  如上面的代码视图,有三个实现,视频缩略图VedioThumbnailProvider没有实现完成,基本方法是利用一个第三方工具ffmpeg来获取第一帧图像然后创建ImageSource。

  ImageThumbnailProvider:普通图片缩略图实现(调用的2.2方法):

本地图片缩略图创建服务 ImageThumbnailProvider : IThumbnailProvider { 创建缩略图。fileName:文件路径;width:图片宽度;height:高度 ImageSource GenereateThumbnail(object fileName, double width, double height) { try { var path = fileName.ToSafeString(); if (path.IsInvalid()) return null; return System.Utility.Helper.Images.CreateImageSourceThumbnia(path, width, height); } catch { return null; } } }

  WebImageThumbnailProvider:网络图片缩略图实现(下载图片数据后调用2.1方法):  

网络图片缩略图创建服务 WebImageThumbnailProvider : IThumbnailProvider { 创建缩略图。fileName:文件路径;width:图片宽度;height:高度 ImageSource GenereateThumbnail(object fileName, double width, double height) { try { var path = fileName.ToSafeString(); if (path.IsInvalid()) return null; var request = WebRequest.Create(path); request.Timeout = 20000; var stream = request.GetResponse().GetResponseStream(); var img = System.Drawing.Image.FromStream(stream); return System.Utility.Helper.Images.CreateImageSourceThumbnia(img, width, height); } catch { return null; } } }

  简单工厂ThumbnailProviderFactory实现:  

缩略图创建服务简单工厂 ThumbnailProviderFactory : System.Utility.Patterns.ISimpleFactory<EnumThumbnail, IThumbnailProvider> { 根据key获取实例 IThumbnailProvider GetInstance(EnumThumbnail key) { switch (key) { case EnumThumbnail.Image: return Singleton<ImageThumbnailProvider>.GetInstance(); case EnumThumbnail.Vedio: return Singleton<VedioThumbnailProvider>.GetInstance(); case EnumThumbnail.WebImage: return Singleton<WebImageThumbnailProvider>.GetInstance(); } return null; } }

 

3.2 缩略图控件ThumbnailImage

  先看看效果图吧,下面三张图片,图1是本地图片,图2是网络图片,图3也是网络图片,为什么没显示呢,这张图片用的是国外的图片链接地址,异步加载(加载比较慢,还没出来的!)

  ThumbnailImage实际是继承在微软的图片控件Image,因此没有样式代码,继承之后,主要的目的就是重写Imagesource的处理过程,详细代码:

/* * 较大的图片,视频,网络图片要做缓存处理:缓存缩略图为本地文件,或内存缩略图对象。 缩略图图片显示控件,同时支持图片和视频缩略图 ThumbnailImage : Image { 是否启用缓存,默认false不启用 CacheEnable { get { return (bool)GetValue(CacheEnableProperty); } set { SetValue(CacheEnableProperty, value); } } 是否启用缓存,默认false不启用.默认缓存时间是180秒 DependencyProperty CacheEnableProperty = DependencyProperty.Register(, typeof(bool), typeof(ThumbnailImage), new PropertyMetadata(false)); 缓存时间,单位秒。默认180秒 CacheTime { get { return (int)GetValue(CacheTimeProperty); } set { SetValue(CacheTimeProperty, value); } } DependencyProperty CacheTimeProperty = DependencyProperty.Register(, typeof(int), typeof(ThumbnailImage), new PropertyMetadata(180)); 是否启用异步加载,网络图片建议启用,本地图可以不需要。默认不起用异步 AsyncEnable { get { return (bool)GetValue(AsyncEnableProperty); } set { SetValue(AsyncEnableProperty, value); } } DependencyProperty AsyncEnableProperty = DependencyProperty.Register(, typeof(bool), typeof(ThumbnailImage), new PropertyMetadata(false)); 缩略图类型,默认Image图片 EnumThumbnail ThumbnailType { get { return (EnumThumbnail)GetValue(ThumbnailTypeProperty); } set { SetValue(ThumbnailTypeProperty, value); } } DependencyProperty ThumbnailTypeProperty = DependencyProperty.Register(, typeof(EnumThumbnail), typeof(ThumbnailImage), new PropertyMetadata(EnumThumbnail.Image)); 缩略图数据源:文件物理路径 ThumbnailSource { get { return GetValue(ThumbnailSourceProperty); } set { SetValue(ThumbnailSourceProperty, value); } } DependencyProperty ThumbnailSourceProperty = DependencyProperty.Register(, typeof(object), typeof(ThumbnailImage), new PropertyMetadata(OnSourcePropertyChanged)); 缩略图 ThumbnailProviderFactory ThumbnailProviderFactory = new ThumbnailProviderFactory(); OnInitialized(EventArgs e) { base.OnInitialized(e); this.Loaded += ThumbnailImage_Loaded; } void ThumbnailImage_Loaded(object sender, RoutedEventArgs e) { BindSource(this); } 属性更改处理事件 OnSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { ThumbnailImage img = sender as ThumbnailImage; if (img == null) return; if (!img.IsLoaded) return; BindSource(img); } BindSource(ThumbnailImage image) { var w = image.Width; var h = image.Height; object source = image.ThumbnailSource; (image.AsyncEnable) { BindThumbnialAync(image, source, w, h); } else { BindThumbnial(image, source, w, h); } } 绑定缩略图 BindThumbnial(ThumbnailImage image, object fileSource, double w, double h) { IThumbnailProvider thumbnailProvider = ThumbnailProviderFactory.GetInstance(image.ThumbnailType); image.Dispatcher.BeginInvoke(new Action(() => { var cache = image.CacheEnable; var time = image.CacheTime; ImageSource img = null; if (cache) { img = CacheManager.GetCache<ImageSource>(fileSource.GetHashCode().ToString(), time, () => { return thumbnailProvider.GenereateThumbnail(fileSource, w, h); }); } else img = thumbnailProvider.GenereateThumbnail(fileSource, w, h); image.Source = img; }), DispatcherPriority.ApplicationIdle); } 异步线程池绑定缩略图 BindThumbnialAync(ThumbnailImage image, object fileSource, double w, double h) { IThumbnailProvider thumbnailProvider = ThumbnailProviderFactory.GetInstance(image.ThumbnailType); var cache = image.CacheEnable; var time = image.CacheTime; System.Utility.Executer.TryRunByThreadPool(() => { ImageSource img = null; if (cache) { img = CacheManager.GetCache<ImageSource>(fileSource.GetHashCode().ToString(), time, () => { return thumbnailProvider.GenereateThumbnail(fileSource, w, h); }); } else img = thumbnailProvider.GenereateThumbnail(fileSource, w, h); image.Dispatcher.BeginInvoke(new Action(() => { image.Source = img; }), DispatcherPriority.ApplicationIdle); }); } }

View Code

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Android 代码库(自定义一套 Dialog通用提示框 ) - 小小情意

    Android 代码库(自定义一套 Dialog通用提示框 ) - 小小情意

    2017-04-21 11:01

  • 自定义input默认placeholder样式 - 小碎步

    自定义input默认placeholder样式 - 小碎步

    2017-04-20 13:00

  • 移动端默认样式重置 - ^.GTR

    移动端默认样式重置 - ^.GTR

    2017-03-27 17:00

  • 从Visual Studio看微软20年技术变迁 - 葡萄城控件技术团队

    从Visual Studio看微软20年技术变迁 - 葡萄城控件技术团队

    2017-03-17 11:00

网友点评