c# 图片浏览器

来源:互联网 发布:java游戏引擎 编辑:程序博客网 时间:2024/05/20 15:10
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; using System.Diagnostics; using System.Management;namespace 图片浏览器 {    public partial class GetPicture : Form    {        public GetPicture()        {            InitializeComponent();        }        private string folderDirPath = null;              //图片文件夹目录        private string picDirPath = null;                 //图片路径        private List<string> imagePathList = new List<string>(); //获取列表图片路径        private int index = 0;                         //获取选中列表图片序号        Point oldCursorPosition = new Point();               //原始位置        Point Position = new Point(0,0);                     //默认打开位置        private void GetPicture_Load(object sender, EventArgs e)        {            pictureBox.SendToBack(); //防止图片放大或者缩小后挡住btn        }        /// <summary> 打开图片        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnOpenPicture_Click(object sender, EventArgs e)        {            pictureBox.Location = Position;            try            {                if (openFileDialog.ShowDialog() == DialogResult.OK)                {                    picDirPath = openFileDialog.FileName;                    pictureBox.Image = Image.FromFile(picDirPath); ;                }            }            catch (Exception msg)            {                throw msg;            }            if (openFileDialog.FileName != "")            {                Directory.SetCurrentDirectory(Directory.GetParent(picDirPath).FullName);                folderDirPath = Directory.GetCurrentDirectory();                ShowPicture();            }        }        /// <summary> 打开图片文件夹        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnOpenPicList_Click(object sender, EventArgs e)        {            pictureBox.Location = Position;            btnNext.Enabled = true;            btnPrevious.Enabled = true;            try            {                FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();                DialogResult result = folderBrowserDialog.ShowDialog();                if (result == DialogResult.OK)                {                    folderDirPath = folderBrowserDialog.SelectedPath;                    ShowPicture();                    //默认显示第一张照片                    this.pictureBox.Image = Image.FromFile(imagePathList[0]);                }            }            //错误提示            catch (Exception msg)            {                MessageBox.Show("没有找到图片");                throw msg;            }             }        /// <summary> 图片显示到 ListView 中        ///        /// </summary>        private void ShowPicture()        {            //打开图片花费时间            Stopwatch sw = new Stopwatch();            sw.Start();            //获取目录与子目录            DirectoryInfo dir = new DirectoryInfo(folderDirPath);            //获取当前目录JPG文件列表 GetFiles获取指定目录中文件的名称(包括其路径)            FileInfo[] fileInfo = dir.GetFiles("*.jpg");            //防止图片失真            this.imageList.ColorDepth = ColorDepth.Depth32Bit;            //将文件夹图片加入图片列表            for (int i = 0; i < fileInfo.Length; i++)            {                //完整路径                picDirPath = fileInfo[i].FullName;                //记录图片源路径 双击显示图片时使用                imagePathList.Add(picDirPath);                this.imageList.Images.Add(Image.FromFile(picDirPath));            }            //显示文件列表            this.listView.Items.Clear();            this.listView.LargeImageList = this.imageList;            this.listView.View = View.LargeIcon;        //大图标显示            //设置ImageList的图像大小 从imageList属性处更改            //开始绑定            this.listView.BeginUpdate();            //增加图片至ListView控件中            for (int i = 0; i < imageList.Images.Count; i++)            {                ListViewItem lvi = new ListViewItem();                lvi.ImageIndex = i;                lvi.Text = "picture " + i;                this.listView.Items.Add(lvi);                pictureBox.SizeMode = PictureBoxSizeMode.Zoom;//图像以zoom显示            }            this.listView.EndUpdate();            //显示打开图片列表所需时间            sw.Stop();            long secords = sw.ElapsedMilliseconds; //毫秒单位            LabTime.Text += '\n' + (Convert.ToDouble(secords) / 1000).ToString() + 's';  //转换为秒        }        /// <summary>双击ListView 显示图片至PictureBox        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void listView_DoubleClick(object sender, EventArgs e)        {            if (this.listView.SelectedItems.Count == 0)                return;            //采用索引方式 imagePathList记录图片真实路径            index = this.listView.SelectedItems[0].Index;            //显示图片            this.pictureBox.Image = Image.FromFile(imagePathList[index]);        }        /// <summary>下一张        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnNext_Click(object sender, EventArgs e)        {            ////防止越界            //if (index == openFileDialog.FileNames.Length - 1)            //{            //    index = 0;            //    this.pictureBox.Image = Image.FromFile(openFileDialog.FileNames[index]);            //}            //else            //{            //    index++;            //    this.pictureBox.Image = Image.FromFile(openFileDialog.FileNames[index]);            //}            if (pictureBox.Image != null)            {                if (index == imagePathList.Count - 1) //最后一张图片                {                    index = 0;                    this.pictureBox.Image = Image.FromFile(imagePathList[index]);                }                else                {                    index++;                    this.pictureBox.Image = Image.FromFile(imagePathList[index]);                }            }        }        /// <summary>上一张        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnPrevious_Click(object sender, EventArgs e)        {            if (pictureBox.Image != null)            {                if (index > 0)                {                    index--;                    this.pictureBox.Image = Image.FromFile(imagePathList[index]);                }                else if (index == 0)                {                    index = imagePathList.Count;                    index--;                    this.pictureBox.Image = Image.FromFile(imagePathList[index]);                }            }        }        /// <summary> 放大        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnGetLarger_Click(object sender, EventArgs e)        {            int width = pictureBox.Width;            int height = pictureBox.Height;            int width1 = (int)(width * 1.2);            int height1 = (int)(height * 1.2);            pictureBox.Top = pictureBox.Top + (height - height1) / 2;            pictureBox.Left = pictureBox.Left + (width - width1) / 2;            pictureBox.Height = height1;            pictureBox.Width = width1;            //Point newLocation = new Point();            //newLocation.X = pictureBox.Location.X - (width1 - width) / 2;            //newLocation.Y = pictureBox.Location.Y - (height1 - height) / 2;            //pictureBox.Location = newLocation;        }        /// <summary> 缩小        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnGetSmaller_Click(object sender, EventArgs e)        {            int width = pictureBox.Width;            int height = pictureBox.Height;            int width1 = (int)(pictureBox.Width * 0.8);            int height1 = (int)(pictureBox.Height * 0.8);            pictureBox.Width = width1;            pictureBox.Height = height1;            Point newLocation = new Point();            newLocation.X = pictureBox.Location.X + (width - width1) / 2;            newLocation.Y = pictureBox.Location.Y + (height - height1) / 2;            pictureBox.Location = newLocation;        }        /// <summary> 顺时针旋转        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnClockwise_Click(object sender, EventArgs e)        {            pictureBox.Image.RotateFlip(RotateFlipType.Rotate90FlipNone);            pictureBox.Refresh();        }        /// <summary> 逆时针旋转        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnContrarotate_Click(object sender, EventArgs e)        {            pictureBox.Image.RotateFlip(RotateFlipType.Rotate270FlipNone);            pictureBox.Refresh();        }        /// <summary> 漫游        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void btnRoam_Click(object sender, EventArgs e)        {            pictureBox.Cursor = Cursors.Hand;        }        //漫游        private void pictureBox_MouseMove(object sender, MouseEventArgs e)        {                        //label2.Text = Cursor.Position.X.ToString();            //label4.Text = Cursor.Position.Y.ToString();            statusX.Text = Cursor.Position.X.ToString();//所有用e传位置的,都可改为用cursor            statusY.Text = Cursor.Position.Y.ToString();//如果用e来传位置,反应太慢,图片会一闪一闪            //如果按下鼠标左键不放,移动鼠标,实施漫游            if (e.Button == MouseButtons.Left)            {                //开始移动图片                pictureBox.Top = pictureBox.Top + Cursor.Position.Y - oldCursorPosition.Y;                pictureBox.Left = pictureBox.Left + Cursor.Position.X - oldCursorPosition.X;                //e.X是新位置,用新位置减去老位置,等于增量                //移动结束后,鼠标的新位置变成漫游的起始位置                oldCursorPosition = Cursor.Position;            }        }        private void pictureBox_MouseDown(object sender, MouseEventArgs e)        {            if (e.Button == MouseButtons.Left)                oldCursorPosition = Cursor.Position;//e.location更改为cursor.position        }        private void btn另存为_Click(object sender, EventArgs e)        {            if (SaveFileDialog.ShowDialog() == DialogResult.OK)            {                string pictureName = SaveFileDialog.FileName;                if (pictureName == "")                {                    MessageBox.Show("命名不得为空");                }                else                {                    if (pictureBox.Image != null)                    {                        ////********************照片另存*********************************                        using (MemoryStream mem = new MemoryStream())                        {                            //这句很重要,不然不能正确保存图片或出错(关键就这一句)                            Bitmap bmp = new Bitmap(pictureBox.Image);                            //保存到内存                            //bmp.Save(mem, pictureBox1.Image.RawFormat );                            //保存到磁盘文件                            bmp.Save(@pictureName, pictureBox.Image.RawFormat);                            bmp.Dispose();                            MessageBox.Show("照片另存成功!", "系统提示");                        }                        ////********************照片另存*********************************                    }                }            }        }        private void GetPicture_KeyPress(object sender, KeyPressEventArgs e)        {        //快捷键            if (e.KeyChar == 'o' || e.KeyChar == 'O')            {                btnOpenPicture_Click(null, null);//如果按键是o或O,执行一次打开操作;            }            else if (e.KeyChar == '+')            {                btnGetLarger_Click(null, null);            }            else if (e.KeyChar == '-')            {                btnGetSmaller_Click(null, null);            }            else if (e.KeyChar == 'p' || e.KeyChar == 'P')            {                btnPrevious_Click(null, null);            }            else if (e.KeyChar == 'n' || e.KeyChar == 'N')            {                btnNext_Click(null, null);            }            else if (e.KeyChar == 's' || e.KeyChar == 'S')            {                btn另存为_Click(null, null);            }        }    } }