• 大小: 54.54 KB
    文件类型: .rar
    金币: 1
    下载: 1 次
    发布日期: 2024-11-16
  • 语言: C#
  • 标签: C#  VS2012  ListView  源代码  

资源简介

该资源参考博客"C#系统应用之ListView实现简单图片浏览器"
http://blog./eastmount/article/details/39958829
它主要通过ListView中加载指定文件夹中所有图片并通过双击缩略图显示具体图片,是比较基础的C#图片打开代码,但是对学习C#初级同学和研究ListVie控件的同学非常有帮助.
免费资源,仅供分享与大家学习,希望对大家有所帮助,尤其是C#的初学者.By:Eastmount

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

//添加命名空间
using System.IO;                   //Directory目录
using System.Diagnostics;          //Stopwatch显示时间

namespace GetPictureList
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //打开图片
        private void button1_Click(object sender EventArgs e)
        {
            //设置打开文件控件
            OpenFileDialog openfile = new OpenFileDialog();
            openfile.Filter = “JPG(*.JPG;*.JPEG);gif文件(*.GIF);BMP文件(*.BMP);PNG文件(*.PNG)|*.jpg;*.jpeg;*.gif;*.bmp;*.png“;
            openfile.FilterIndex = 1;  //当前选定索引
            openfile.RestoreDirectory = true;
            openfile.FileName = ““;
            //对话框选择确定按钮
            if (openfile.ShowDialog() == DialogResult.OK)
            {
                //FromFile从指定的文件创建Image
                pictureBox1.Image = Image.FromFile(openfile.FileName);
                //图片被拉伸或收缩适合pictureBox大小
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;               
            }
        }

        //定义变量
        private string folderDirPath;                            //图片文件夹地址
        private string picDirPath = null;                        //图片路径
        private List imagePathList = new List(); //获取列表图片路径
        private int index;                                       //获取选中列表图片序号

        //ListView和imageList显示图片列表
        private void button2_Click(object sender EventArgs e)
        {
            try
            {
                //打开选择文件夹对话框
                FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
                DialogResult result = folderBrowserDialog.ShowDialog();
                if (result == DialogResult.OK)
                {
                    //获取用户选择的文件夹路径
                    this.folderDirPath = folderBrowserDialog.SelectedPath;
                    //调用自定义函数显示图片列表至ListView控件
                    ShowPicture();
                }
                else if (result == DialogResult.Cancel)
                {
                    MessageBox.Show(“取消显示图片列表“);
                }
            }
            catch (Exception msg)
            {
                //报错提示 未将对象引用设置到对象的实例
                throw msg;
            }     
        }

        //显示图片列表至ListView控件
        private void ShowPicture()
        {
            //提供一种方法测试运行时间 开始计算
            //参考资料:http://www.cnblogs.com/newstart/archive/2012/09/21/2696884.html
            Stopwatch sw = new Stopwatch();
            sw.Start();

            //获取目录与子目录
            DirectoryInfo dir = new DirectoryInfo(folderDirPath);
            //获取当前目录JPG文件列表 GetFiles获取指定目录中文件的名称(包括其路径)
            FileInfo[] fileInfo = dir.Get

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        187  2014-10-10 12:05  GetPictureList\GetPictureList\App.config

     文件      13312  2014-10-10 13:10  GetPictureList\GetPictureList\bin\Debug\GetPictureList.exe

     文件        187  2014-10-10 12:05  GetPictureList\GetPictureList\bin\Debug\GetPictureList.exe.config

     文件      26112  2014-10-10 13:10  GetPictureList\GetPictureList\bin\Debug\GetPictureList.pdb

     文件      22984  2014-10-10 13:34  GetPictureList\GetPictureList\bin\Debug\GetPictureList.vshost.exe

     文件        187  2014-10-10 12:05  GetPictureList\GetPictureList\bin\Debug\GetPictureList.vshost.exe.config

     文件        490  2012-06-02 22:34  GetPictureList\GetPictureList\bin\Debug\GetPictureList.vshost.exe.manifest

     文件       7530  2014-10-10 13:10  GetPictureList\GetPictureList\Form1.cs

     文件       7825  2014-10-10 13:10  GetPictureList\GetPictureList\Form1.Designer.cs

     文件       6188  2014-10-10 13:10  GetPictureList\GetPictureList\Form1.resx

     文件       3807  2014-10-10 12:10  GetPictureList\GetPictureList\GetPictureList.csproj

     文件        863  2014-10-10 12:05  GetPictureList\GetPictureList\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6935  2014-10-10 13:32  GetPictureList\GetPictureList\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        672  2014-10-10 13:34  GetPictureList\GetPictureList\obj\Debug\GetPictureList.csproj.FileListAbsolute.txt

     文件        975  2014-10-10 13:10  GetPictureList\GetPictureList\obj\Debug\GetPictureList.csproj.GenerateResource.Cache

     文件       2209  2014-10-10 12:10  GetPictureList\GetPictureList\obj\Debug\GetPictureList.csprojResolveAssemblyReference.cache

     文件      13312  2014-10-10 13:10  GetPictureList\GetPictureList\obj\Debug\GetPictureList.exe

     文件        180  2014-10-10 13:10  GetPictureList\GetPictureList\obj\Debug\GetPictureList.Form1.resources

     文件      26112  2014-10-10 13:10  GetPictureList\GetPictureList\obj\Debug\GetPictureList.pdb

     文件        180  2014-10-10 12:10  GetPictureList\GetPictureList\obj\Debug\GetPictureList.Properties.Resources.resources

     文件          0  2014-10-10 12:05  GetPictureList\GetPictureList\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2014-10-10 12:05  GetPictureList\GetPictureList\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2014-10-10 12:05  GetPictureList\GetPictureList\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件        526  2014-10-10 12:05  GetPictureList\GetPictureList\Program.cs

     文件       1348  2014-10-10 12:05  GetPictureList\GetPictureList\Properties\AssemblyInfo.cs

     文件       2880  2014-10-10 12:05  GetPictureList\GetPictureList\Properties\Resources.Designer.cs

     文件       5612  2014-10-10 12:05  GetPictureList\GetPictureList\Properties\Resources.resx

     文件       1101  2014-10-10 12:05  GetPictureList\GetPictureList\Properties\Settings.Designer.cs

     文件        249  2014-10-10 12:05  GetPictureList\GetPictureList\Properties\Settings.settings

     文件        932  2014-10-10 12:05  GetPictureList\GetPictureList.sln

............此处省略12个文件信息

评论

共有 条评论