這裏保存用到一個SaveFileDialog控件,能夠獲取用戶選擇的保存文件的路徑。ide
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 { }
/// <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; }
/// <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); } }
這個獲取圖片放入到listview方法,若是圖片過大有不少的話,容易形成內存不足而報錯的問題。this
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]);//從集合中除去撤回的圖片 } }
這裏要注意,放入集合中的圖片必定要是修改完成之後獲取的圖片對象,不能是一直在被修改的圖片,不然在集合中的圖片也是被修改的。spa