WPF:图像处理(一)图像文件获取与预览

215 篇文章 2 订阅

作者:Splash

转自:http://blog.csdn.net/jhqin/article/details/7472190


[csharp] view plain copy
  1. /* ---------------------------------------------------------- 
  2. 文件名称:Folder.cs 
  3.  
  4. 作者:秦建辉 
  5.  
  6. MSN:splashcn@msn.com 
  7. QQ:36748897 
  8.  
  9. 博客:http://blog.csdn.net/jhqin 
  10.  
  11. 开发环境: 
  12.     Visual Studio V2010 
  13.     .NET Framework 4 Client Profile 
  14.  
  15. 版本历史: 
  16.     V1.0    2012年04月16日 
  17.             图像文件获取与预览 
  18. ------------------------------------------------------------ */  
  19. using System;  
  20. using System.Collections.Generic;  
  21. using System.IO;  
  22. using System.Text.RegularExpressions;  
  23. using System.Windows.Controls;  
  24. using System.Windows.Media;  
  25. using System.Windows.Media.Imaging;  
  26.   
  27. namespace Splash.Imaging  
  28. {  
  29.     public static class Folder  
  30.     {     
  31.         /// <summary>  
  32.         /// 委托声明:鼠标点击事件处理器  
  33.         /// </summary>  
  34.         /// <param name="sender">附加事件处理程序的对象</param>  
  35.         /// <param name="e">事件数据</param>  
  36.         public delegate void MyMouseDownEventHandler(object sender, System.Windows.Input.MouseButtonEventArgs e);  
  37.   
  38.         /// <summary>  
  39.         /// 返回指定目录中与指定的搜索模式匹配的文件的名称(包含它们的路径),并使用一个值以确定是否搜索子目录  
  40.         /// </summary>  
  41.         /// <param name="folderPath">将从其检索文件的目录</param>  
  42.         /// <param name="filter">搜索模式匹配</param>  
  43.         /// <param name="searchOption">指定搜索操作应包括所有子目录还是仅包括当前目录</param>  
  44.         /// <returns>包含指定目录中与指定搜索模式匹配的文件的名称列表</returns>  
  45.         public static String[] GetFiles(String folderPath, String filter, SearchOption searchOption)  
  46.         {   // 获取文件列表  
  47.             String[] FileEntries = Directory.GetFiles(folderPath, "*", searchOption);  
  48.             if (FileEntries.Length == 0)   
  49.                 return null;  
  50.             else if (String.IsNullOrEmpty(filter))   
  51.                 return FileEntries;  
  52.   
  53.             // 建立正则表达式  
  54.             Regex rx = new Regex(filter, RegexOptions.IgnoreCase);  
  55.   
  56.             // 过滤文件  
  57.             List<String> FilterFileEntries = new List<String>(FileEntries.Length);              
  58.             foreach (String FileName in FileEntries)  
  59.             {  
  60.                 if (rx.IsMatch(FileName))  
  61.                 {  
  62.                     FilterFileEntries.Add(FileName);  
  63.                 }  
  64.             }  
  65.   
  66.             return (FilterFileEntries.Count == 0) ? null : FilterFileEntries.ToArray();              
  67.         }  
  68.   
  69.         /// <summary>  
  70.         /// 返回指定目录中图像文件列表,并使用一个值以确定是否搜索子目录  
  71.         /// </summary>  
  72.         /// <param name="folderPath">将从其检索文件的目录</param>  
  73.         /// <param name="searchOption">指定搜索操作应包括所有子目录还是仅包括当前目录</param>  
  74.         /// <returns>图像文件列表</returns>  
  75.         public static String[] GetImages(String folderPath, SearchOption searchOption)  
  76.         {  
  77.             String filter = "\\.(bmp|gif|jpg|jpe|png|tiff|tif)$";  
  78.             return GetFiles(folderPath, filter, searchOption);  
  79.         }  
  80.   
  81.         /// <summary>  
  82.         /// 显示图像  
  83.         /// </summary>  
  84.         /// <param name="panel">放置图像控件的容器</param>  
  85.         /// <param name="images">要显示的图像文件集合</param>  
  86.         /// <param name="handler">图像控件点击事件处理器</param>  
  87.         public static void DisplayImages(StackPanel panel, String[] images, MyMouseDownEventHandler handler)  
  88.         {   // 参数检测  
  89.             if ((panel == null) || (images == null)) return;  
  90.   
  91.             // 清空所有图像控件  
  92.             panel.Children.Clear();  
  93.   
  94.             // 增加新的图像控件  
  95.             Double Stride = (panel.Orientation == Orientation.Horizontal) ? panel.Height : panel.Width;  
  96.             foreach (String FileName in images)  
  97.             {   // 设置边框                             
  98.                 Border border = new Border();  
  99.                 border.Margin = new System.Windows.Thickness(4);            // 边框外边距  
  100.                 border.BorderThickness = new System.Windows.Thickness(4);   // 边框厚度  
  101.                 border.BorderBrush = new SolidColorBrush(Colors.DarkGreen); // 边框颜色  
  102.   
  103.                 // 边框大小  
  104.                 if (panel.Orientation == Orientation.Horizontal)  
  105.                 {     
  106.                     border.Height = border.Width = Stride - border.Margin.Top - border.Margin.Bottom;   
  107.                 }  
  108.                 else  
  109.                 {  
  110.                     border.Height = border.Width = Stride - border.Margin.Left - border.Margin.Right;  
  111.                 }  
  112.                   
  113.                 // 设置图像控件  
  114.                 Image image = new Image();  
  115.                 image.Width = border.Width - border.BorderThickness.Left - border.BorderThickness.Right;   // 图像控件宽度  
  116.                 image.Height = border.Height - border.BorderThickness.Top - border.BorderThickness.Bottom;  // 图像控件高度  
  117.                 image.Stretch = System.Windows.Media.Stretch.Uniform;  
  118.                 image.Source = new BitmapImage(new Uri(FileName));  
  119.   
  120.                 // 图片点击事件  
  121.                 image.MouseDown += new System.Windows.Input.MouseButtonEventHandler(handler);  
  122.   
  123.                 // 设置控件布局  
  124.                 border.Child = image;  
  125.                 panel.Children.Add(border);  
  126.             }  
  127.         }  
  128.     }  
  129. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值