同系列文章:
/* -------------------------------------------------------- * 文件名称:Folder.cs * 作者:秦建辉 * * QQ:36748897 * * 博客:http://www.firstsolver.com/wordpress/ * * 开发环境: * Visual Studio V2012 * .NET Framework 4.5 * * 版本历史: * V1.1 2013年05月17日 * 将WPF版本修改为WinForm版本 * * V1.0 2012年04月16日 * 图像文件获取与预览 --------------------------------------------------------- */ using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Text.RegularExpressions; using System.Windows.Forms; namespace Splash.Imaging { /// <summary> /// WinForm:图像文件获取与预览 /// </summary> public static class Folder { /// <summary> /// 返回指定目录中与指定的搜索模式匹配的文件的名称(包含它们的路径),并使用一个值以确定是否搜索子目录 /// </summary> /// <param name="folderPath">将从其检索文件的目录</param> /// <param name="filter">搜索模式匹配</param> /// <param name="searchOption">指定搜索操作应包括所有子目录还是仅包括当前目录</param> /// <returns>包含指定目录中与指定搜索模式匹配的文件的名称列表</returns> public static String[] GetFiles(String folderPath, String filter, SearchOption searchOption) { // 获取文件列表 String[] FileEntries = Directory.GetFiles(folderPath, "*", searchOption); if (FileEntries.Length == 0) return null; else if (String.IsNullOrEmpty(filter)) return FileEntries; // 建立正则表达式 Regex rx = new Regex(filter, RegexOptions.IgnoreCase); // 过滤文件 List<String> FilterFileEntries = new List<String>(FileEntries.Length); foreach (String FileName in FileEntries) { if (rx.IsMatch(FileName)) { FilterFileEntries.Add(FileName); } } return (FilterFileEntries.Count == 0) ? null : FilterFileEntries.ToArray(); } /// <summary> /// 返回指定目录中图像文件列表,并使用一个值以确定是否搜索子目录 /// </summary> /// <param name="folderPath">将从其检索文件的目录</param> /// <param name="searchOption">指定搜索操作应包括所有子目录还是仅包括当前目录</param> /// <returns>图像文件列表</returns> public static String[] GetImages(String folderPath, SearchOption searchOption) { String filter = "\\.(bmp|gif|jpg|jpe|png|tiff|tif)$"; return GetFiles(folderPath, filter, searchOption); } /// <summary> /// 显示图像 /// </summary> /// <param name="panel">放置图片框控件的容器</param> /// <param name="images">要显示的图像文件集合</param> /// <param name="handler">图像控件点击事件处理器</param> /// <remarks> /// 设置控件属性 /// AutoScroll = true /// AutoSize = false /// WrapContents = false /// </remarks> public static void DisplayImages(FlowLayoutPanel panel, String[] images, MouseEventHandler handler) { // 参数检测 if ((panel == null) || (images == null)) return; // 清空所有图像控件 panel.Controls.Clear(); // 增加新的图像控件 Int32 Stride; if (panel.FlowDirection == FlowDirection.LeftToRight || panel.FlowDirection == FlowDirection.RightToLeft) { // 如只显示水平滚动条,注意设置panel.Padding.Bottom = 20 Stride = panel.Height - panel.Padding.Top - panel.Padding.Bottom; } else { // 如只显示垂直滚动条,注意设置panel.Padding.Right = 20 Stride = panel.Width - panel.Padding.Left - panel.Padding.Right; } foreach (String FileName in images) { // 图片框控件 PictureBox box = new PictureBox(); box.Margin = new Padding(0); box.SizeMode = PictureBoxSizeMode.Zoom; // 保持图像比列缩放 box.Height = box.Width = Stride; // 关联图像文件 box.Image = new Bitmap(FileName, true); // 设置图片框点击事件 box.MouseDown += handler; // 添加图片框控件到容器 panel.Controls.Add(box); } } } }