winfrom保存图片,将文件夹中图片放入listview,与撤回操作
生活随笔
收集整理的這篇文章主要介紹了
winfrom保存图片,将文件夹中图片放入listview,与撤回操作
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
之前那些操作完成對圖片的修改之后,就是要保存圖片了。
這里保存用到一個SaveFileDialog控件,可以獲取用戶選擇的保存文件的路徑。
if (pictureBox1.Image.Width > 0){SaveFileDialog saveImageDialog = new SaveFileDialog();//這里實例化一個SaveFileDialog控件,并設(shè)置一些屬性saveImageDialog.Title = "圖片保存";saveImageDialog.Filter = @"jpeg|*.jpg|bmp|*.bmp";saveImageDialog.FileName = getgv();//獲取DataGridView控件的內(nèi)容作文文件名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);//保存之前判斷文件夾里的圖片數(shù)量,如果大于256張就要刪除第一張string dpath = fileName.Substring(0, fileName.LastIndexOf("\\"));//去除掉文件名DirectoryInfo dti = new DirectoryInfo(dpath);//根據(jù)路徑獲取目錄對象FileInfo[] file = dti.GetFiles();//獲取當(dāng)前目錄的文件列表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("注釋內(nèi)容不符合命名規(guī)則 "+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(); }}//初始化設(shè)置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方法,如果圖片過大有很多的話,容易造成內(nèi)存不足而報錯的問題。
撤回
if (bitlist.Count == 0)//判斷供撤回的集合中是否有數(shù)據(jù){return;}for (int i = 0; i < bitlist.Count; i++){if (i == bitlist.Count-1){if (i == 0)//如果已經(jīng)是最后一張,清空picturebox內(nèi)的圖片{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]);//從集合中除去撤回的圖片 }}這里要注意,放入集合中的圖片一定要是修改完成以后獲取的圖片對象,不能是一直在被修改的圖片,否則在集合中的圖片也是被修改的。
?
轉(zhuǎn)載于:https://www.cnblogs.com/big-lll/p/7753384.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的winfrom保存图片,将文件夹中图片放入listview,与撤回操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql递归
- 下一篇: sphinx conf 文件模板