zoukankan      html  css  js  c++  java
  • winfrom保存图片,将文件夹中图片放入listview,与撤回操作

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

    这里保存用到一个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]);//从集合中除去撤回的图片
                    }
                }

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

  • 相关阅读:
    zookeeper与activemq最新存储replicatedLevelDB整合
    MySQL分表的3种方法
    mycat 从入门到放弃 (转)
    centos 7 忘记密码
    java内存溢出的情况解决方法
    跨域问题的产生及解决方法
    一次jvm调优过程
    2019年总结
    Jenkinsfile与Json的转换
    DevOps平台
  • 原文地址:https://www.cnblogs.com/big-lll/p/7753384.html
Copyright © 2011-2022 走看看