码迷,mamicode.com
首页 > Windows程序 > 详细

winfrom保存图片,将文件夹中图片放入listview,与撤回操作

时间:2017-10-30 14:32:36      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:大小   添加文件   数据   清空   提示   去除   time   closed   als   

之前那些操作完成对图片的修改之后,就是要保存图片了。

这里保存用到一个SaveFileDialog控件,可以获取用户选择的保存文件的路径。

 if (pictureBox1.Image.Width > 0)
                {
                    SaveFileDialog saveImageDialog = new SaveFileDialog();//这里实例化一个SaveFileDialog控件,并设置一些属性
                    saveImageDialog.Title = "图片保存";
                    saveImageDialog.Filter = @"jpeg|*.jpg|bmp|*.bmp";
                    saveImageDialog.FileName = getgv();//获取DataGridView控件的内容作文文件名
                    if (saveImageDialog.ShowDialog() == DialogResult.OK)//弹出对话框
                    {
                        string fileName = saveImageDialog.FileName.ToString();//获取文件路径
                        if (fileName != "" && fileName != null)
                        {
                            string fileExtName = fileName.Substring(fileName.LastIndexOf(".") + 1).ToString();//截取文件的后缀名
                            System.Drawing.Imaging.ImageFormat imgformat = null;
                            if (fileExtName != "")
                            {
                                switch (fileExtName)
                                {
                                    case "jpg":
                                        imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                                        break;
                                    case "bmp":
                                        imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
                                        break;
                                    default:
                                        imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;
                                        break;
                                }
                                try
                                {
                                    //Bitmap bit = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                                    //保存之前判断文件夹里的图片数量,如果大于256张就要删除第一张
                                    string dpath = fileName.Substring(0, fileName.LastIndexOf("\\"));//去除掉文件名
                                    DirectoryInfo dti = new DirectoryInfo(dpath);//根据路径获取目录对象
                                    FileInfo[] file = dti.GetFiles();//获取当前目录的文件列表
                                    if (file.Count() >= 256)
                                    {
                                        int a = (file.Count()-256);
                                        for (int i = 0; i <= a; i++)
                                        {
                                            file[i].Delete();
                                        }                                        
                                    }
                                    pictureBox1.Image.Save(fileName, imgformat);//保存文件
                                    MessageBox.Show("保存成功", "提示");
                                    //pictureBox1.Image.Save(saveImageDialog.FileName);
                                    fName = fileName;
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show("保存异常 " + ex.Message, "提示");
                                }
                            }
                        }
                    }
                    xulielv(fName);//在listview中显示图片

                }
            }
            catch
            {

            }
技术分享 getgv
/// <summary>
        /// 获取dataGridView注释作为图片名
        /// </summary>
        /// <returns></returns>
        private string getgv()
        {
            string txt = "";
            try
            {
                txt += DateTime.Now.ToString("yyyy年MM月dd日HH.mm.ss");
                for (int i = 0; i < 4; i++)
                {
                    txt += ",";
                    txt += dataGridView1.Rows[0].Cells[i].Value.ToString();                    
                }                
            }
            catch(Exception ex)
            {
                MessageBox.Show("注释内容不符合命名规则 "+ex.Message);
            }            
            return txt;
        }
获取dataGridView注释作为图片名
技术分享
/// <summary>
        /// 生成序列中的图片
        /// </summary>
        private void xulielv(string path)
        {
            try
            {
                if (path != "")
                {
                    imageList1.Images.Clear();
                    string dpath = path.Substring(0, path.LastIndexOf("\\"));//去除掉文件名
                    DirectoryInfo TheFolder = new DirectoryInfo(dpath);//文件路径
                    List<string> tifNames = new List<string>();
                    for (int i = 0; i < TheFolder.GetFiles().Length; i++)//遍历文件夹
                    {
                        if (TheFolder.GetFiles()[i].Length > 0 && TheFolder.GetFiles()[i].Extension == ".jpg" || TheFolder.GetFiles()[i].Extension == ".png")//或者jpg,png 文件大小要大于0且是图片文件
                        {
                            Image image = Image.FromFile(TheFolder.GetFiles()[i].DirectoryName + "\\" + TheFolder.GetFiles()[i].Name);    //获取文件                
                            tifNames.Add(TheFolder.GetFiles()[i].Name);//添加文件名
                            imageList1.Images.Add(image);//添加图片    
                            //image.Dispose();
                        }
                        
                    }
                    //初始化设置
                    this.listView1.Items.Clear();
                    //this.listView1.View = View.LargeIcon;
                    this.listView1.View = View.Tile;
                    this.listView1.LargeImageList = this.imageList1;
                    //开始绑定
                    this.listView1.BeginUpdate();
                    for (int i = 0; i < tifNames.Count; i++)
                    {
                        ListViewItem lvi = new ListViewItem();
                        lvi.ImageIndex = i;
                        lvi.Text = tifNames[i];
                        this.listView1.Items.Add(lvi);
                    }
                    this.listView1.EndUpdate();    
                }
                else
                {
                    MessageBox.Show("未提供有效路径");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("生成序列失败 "+ex.Message);
            }
            
        }
xulielv

这个获取图片放入到listview方法,如果图片过大有很多的话,容易造成内存不足而报错的问题。

撤回

 if (bitlist.Count == 0)//判断供撤回的集合中是否有数据
            {
                return;
            }
            for (int i = 0; i < bitlist.Count; i++)
            {
                if (i == bitlist.Count-1)
                {
                    if (i == 0)//如果已经是最后一张,清空picturebox内的图片
                    {
                        this.pictureBox1.Image = null;
                        this.pictureBox1.Refresh();
                        txt_tjsz.Visible = false;
                        txt_tjsz.Text = "";
                        txt_tjwz.Visible = false;
                        txt_tjwz.Text = "";
                        return;
                    }
                    pic = bitlist[i - 1];//绘制图片时的图片对象
                    this.pictureBox1.Image = pic;//控件显示的图片
                    this.pictureBox1.Refresh();
                    bitlist.Remove(bitlist[i]);//从集合中除去撤回的图片
                }
            }

这里要注意,放入集合中的图片一定要是修改完成以后获取的图片对象,不能是一直在被修改的图片,否则在集合中的图片也是被修改的。

 

winfrom保存图片,将文件夹中图片放入listview,与撤回操作

标签:大小   添加文件   数据   清空   提示   去除   time   closed   als   

原文地址:http://www.cnblogs.com/big-lll/p/7753384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!