WPF Cover Flow Tutorial : Part 4

15 篇文章 0 订阅
We can improve the HCI from Part 3 with some mouse handling.

We add a MouseDown handler method to our Viewport3D :
  1. private void OnViewportMouseDown(object sender, MouseButtonEventArgs e)  
  2. {  
  3.     var rayMeshResult = (RayMeshGeometry3DHitTestResult)VisualTreeHelper.HitTest(viewPort, e.GetPosition(viewPort));  
  4.     if (rayMeshResult != null)  
  5.     {  
  6.         for (int i = 0; i < coverList.Count; i++)  
  7.         {  
  8.             if (coverList[i].Matches(rayMeshResult.MeshHit))  
  9.             {  
  10.                 UpdateIndex(i);  
  11.                 break;  
  12.             }  
  13.         }  
  14.     }  
  15. }  
This method uses the VisualTreeHelper to find which mesh was hitten by the mouse click. If there is a match, we update the index like we did before. Matches are found with this new Cover method :
  1. public bool Matches(MeshGeometry3D mesh)  
  2. {  
  3.     foreach (GeometryModel3D part in modelGroup.Children)  
  4.        if (part.Geometry == mesh)  
  5.            return true;  
  6.     return false;  
  7. }  
We also modify the TestWindow constructor to load all the pictures from a sample folder :
  1. public TestWindow()  
  2. {  
  3.     InitializeComponent();  
  4.     var imageDir = new DirectoryInfo(@"c:\_covers");  
  5.     int doneImages = 0;  
  6.     foreach (FileInfo image in imageDir.GetFiles("*.jpg"))  
  7.     {  
  8.         var cover = new Cover(image.FullName, doneImages++);  
  9.         coverList.Add(cover);  
  10.         visualModel.Children.Add(cover);  
  11.     }  
  12. }  
Mouse click may increment or decrement the index by more than one cover. That's wy we need to fix theUpdateIndex method :
  1. private void UpdateIndex(int newIndex)  
  2. {  
  3.     if (index != newIndex)  
  4.     {  
  5.         int oldIndex = index;  
  6.         index = newIndex;  
  7.         if (index > oldIndex)  
  8.             for (int i = oldIndex; i <= index; i++)  
  9.                 RotateCover(i);  
  10.         else  
  11.             for (int i = oldIndex; i >= index; i--)  
  12.                 RotateCover(i);  
  13.         camera.Position = new Point3D(.2 * index, camera.Position.Y, camera.Position.Z);  
  14.     }  
  15. }  
It is now time to play with more covers. Let's try the current application with a sample folder containing 500+ covers. These covers are usually between 400x400 and 500x500 pixels. The next Process Explorer capture shows that the application needs a lot of cpu and memory resources :
  • Loading all covers brings the cpu to 100%.
  • Browing all covers with the Right key pressed also needs a lot of cpu. Moreover, this rises the cache size from 300 MB to more than 1 GB.
It is possible to improve performance if we use thumbnails for all covers. We will store thumbnails in a subfolder.
  1. private ImageSource LoadImageSource(string imagePath)  
  2. {  
  3.     var imageFile = new FileInfo(imagePath);  
  4.     var thumbnailDir = new DirectoryInfo(Path.Combine(imageFile.Directory.FullName, "tn"));  
  5.     if (!thumbnailDir.Exists)  
  6.         thumbnailDir.Create();  
  7.     var thumbnail = new FileInfo(Path.Combine(thumbnailDir.FullName, imageFile.Name));  
  8.     if (!thumbnail.Exists)  
  9.     {  
  10.         Image source = Image.FromFile(imageFile.FullName);  
  11.         int height = source.Height;  
  12.         int width = source.Width;  
  13.         int factor = (height - 1) / 250 + 1;  
  14.         int smallHeight = height / factor;  
  15.         int smallWidth = width / factor;  
  16.         Image thumb = source.GetThumbnailImage(smallWidth, smallHeight, null, IntPtr.Zero);  
  17.         thumb.Save(thumbnail.FullName);  
  18.     }  
  19.     return new BitmapImage(new Uri(thumbnail.FullName, UriKind.RelativeOrAbsolute));  
  20. }  
Thumbnails improve performance while browsing, but it is still slow at startup. We will try to improve this later.

Continue with Part 5. Download source.

转载自:http://d3dal3.blogspot.com/2008/10/wpf-cover-flow-tutorial-part-4.html

版权归原作者所有。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值