1. 程式人生 > >winfrom保存圖片,將文件夾中圖片放入listview,與撤回操作

winfrom保存圖片,將文件夾中圖片放入listview,與撤回操作

pat ctu index update ast height 分享 private 遍歷

之前那些操作完成對圖片的修改之後,就是要保存圖片了。

這裏保存用到一個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,與撤回操作