HTML5技术

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

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

支持GIF动画图片播放的图片控件,GIF图片源GIFSource AnimatedGIF : Image{ DependencyProperty GIFSourceProperty = DependencyProperty.Register( , typeof ( string ), typeof (AnimatedGIF), new PropertyMetad

支持GIF动画图片播放的图片控件,GIF图片源GIFSource AnimatedGIF : Image { DependencyProperty GIFSourceProperty = DependencyProperty.Register( , typeof(string), typeof(AnimatedGIF), new PropertyMetadata(OnSourcePropertyChanged)); GIF图片源,支持相对路径、绝对路径 GIFSource { get { return (string)GetValue(GIFSourceProperty); } set { SetValue(GIFSourceProperty, value); } } BitmapSource BitmapSource; FrameUpdatedEventHandler(); Delete local bitmap resource (VS.85).aspx /// </summary> [DllImport(, CharSet = CharSet.Auto, SetLastError = true)] DeleteObject(IntPtr hObject); OnInitialized(EventArgs e) { base.OnInitialized(e); this.Loaded += AnimatedGIF_Loaded; this.Unloaded += AnimatedGIF_Unloaded; } void AnimatedGIF_Unloaded(object sender, RoutedEventArgs e) { this.StopAnimate(); } void AnimatedGIF_Loaded(object sender, RoutedEventArgs e) { BindSource(this); } Start animation StartAnimate() { ImageAnimator.Animate(Bitmap, OnFrameChanged); } Stop animation StopAnimate() { ImageAnimator.StopAnimate(Bitmap, OnFrameChanged); } Event handler for the frame changed OnFrameChanged(object sender, EventArgs e) { Dispatcher.BeginInvoke(DispatcherPriority.Normal, new FrameUpdatedEventHandler(FrameUpdatedCallback)); } private void FrameUpdatedCallback() { ImageAnimator.UpdateFrames(); if (BitmapSource != null) BitmapSource.Freeze(); // Convert the bitmap to BitmapSource that can be display in WPF Visual Tree BitmapSource = GetBitmapSource(this.Bitmap, this.BitmapSource); Source = BitmapSource; InvalidateVisual(); } 属性更改处理事件 OnSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { AnimatedGIF gif = sender as AnimatedGIF; if (gif == null) return; if (!gif.IsLoaded) return; BindSource(gif); } BindSource(AnimatedGIF gif) { gif.StopAnimate(); if (gif.Bitmap != null) gif.Bitmap.Dispose(); var path = gif.GIFSource; if (path.IsInvalid()) return; if (!Path.IsPathRooted(path)) { path = File.GetPhysicalPath(path); } gif.Bitmap = new Bitmap(path); gif.BitmapSource = GetBitmapSource(gif.Bitmap, gif.BitmapSource); gif.StartAnimate(); } private static BitmapSource GetBitmapSource(Bitmap bmap, BitmapSource bimg) { IntPtr handle = IntPtr.Zero; try { handle = bmap.GetHbitmap(); bimg = Imaging.CreateBitmapSourceFromHBitmap( handle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); } finally { if (handle != IntPtr.Zero) DeleteObject(handle); } return bimg; } }

View Code

 

五.图片列表样式,支持大数据量的虚拟化

  先看看效果图(gif图,有点大):

 

  用的是ListView作为列表容器,因为Listview支持灵活的扩展,为了实现上面的效果,集合容器ItemsPanel只能使用WrapPanel,样式本身并不复杂:  

BorderBrushD:\Doc\Resource ItemTemplate VirtualizingStackPanel.IsVirtualizing CanVerticallyScroll

 

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

网友点评
i