效果图
1.上传
/// <summary> /// 上传 /// </summary> private void Upload() { if (imgListPhoto.Images.Count > 12) { MsgBox.Show("最多上传12张照片"); return; } if (openCertDialog.ShowDialog() == DialogResult.OK) { FileInfo fileInfo = new FileInfo(openCertDialog.FileName); try { if (imgListPhoto.Images.Count + openCertDialog.FileNames.Length > 12) { MsgBox.Show("最多上传12张照片"); return; } string[] nameArr = openCertDialog.FileNames; this.lvImg.View = View.LargeIcon; this.lvImg.LargeImageList = this.imgListPhoto; this.lvImg.BeginUpdate(); foreach (var item in nameArr) { Image.FromFile(item); //获取文件 Image image = Image.FromFile(item); //添加图片 imgListPhoto.Images.Add(image); ListViewItem lvi = new ListViewItem(); lvi.ImageIndex = imgListPhoto.Images.Count - 1; lvi.Text = imgListPhoto.Images.Count.ToString(); this.lvImg.Items.Add(lvi); Thread.Sleep(200); } this.lvImg.EndUpdate(); } catch { MsgBox.Show("请上传正确的图片格式"); } if (openCertDialog.FileNames.Length > 0) { StringBuilder strSb = new StringBuilder(); foreach (var item in openCertDialog.FileNames) { strSb.Append(item + ","); } string names = strSb.ToString().Substring(0, strSb.ToString().Length - 1); if (!string.IsNullOrEmpty(visitEmergencyModel.VtRlatpicidLst)) { visitEmergencyModel.VtRlatpicidLst = visitEmergencyModel.VtRlatpicidLst + "," + names; } else { visitEmergencyModel.VtRlatpicidLst = names; } } SetBtnSaveEnable(true); } }
2.选中照片删除
//选中图片
selectImage = lvImg.SelectedItems[0];
/// <summary> /// 删除图片 /// </summary> private void DeletePt() { if (selectImage != null) { if (this.lvImg.Items.Contains(selectImage)) { this.lvImg.Items.Remove(selectImage); } } }