using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Windows.Forms; using Core.Mes.Client.Comm.Tool; using Core.StlMes.Client.LgResMgt.Mcms.entity; using CoreFS.CA06; using Infragistics.Win.UltraWinGrid; using System.Net; using System.Reflection; using System.IO; using System.Threading; namespace Core.StlMes.Client.LgResMgt.Mcms { public partial class frmLocalTruckImageShow : FrmBase { public frmLocalTruckImageShow(OpeBase _ob) { InitializeComponent(); ob = _ob; } FtpWebRequest reqFTP; private static object sync = new object(); private static frmLocalTruckImageShow instance; private static readonly string _path = Environment.CurrentDirectory + "\\imgShort\\picture"; // 有参数 private CmmWeightResultEntity parame; private CmmBaseSpotInfoEntity spot; Dictionary> dic = new Dictionary>(); Dictionary dicPic; int ipic = 0; public static frmLocalTruckImageShow GetSingleton(OpeBase _ob, CmmWeightResultEntity parame, CmmBaseSpotInfoEntity spot) { lock (sync) { ; if (instance == null || instance.IsDisposed) instance = new frmLocalTruckImageShow(_ob); instance.parame = parame; instance.spot = spot; } return instance; } void ftpDown( string picName) { string[] files = picName.Split(new char[] { '/' }); try { //①读取FTP上的文件 // FileStream outputStream = new FileStream(LocalDir, FileMode.Create);//建立FTP链接 if (File.Exists(_path + "\\" + files[files.Length - 1])) { File.Delete(_path + "\\" + files[files.Length - 1]); } FileStream outputStream = new FileStream(_path + "\\" + files[files.Length-1], FileMode.Create); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://"+spot.FtpIp + spot.Ftppath + picName));//读取FTP上的文件 reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;//FTP下载协议 reqFTP.UseBinary = true;//指定文件传输类型 reqFTP.Credentials = new NetworkCredential(spot.FtpUserName, spot.FtpPassword);//FTP通信凭证(即登录ftp) FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();//登录成功 //②将FTP上的文件内容转化成数据流 Stream ftpStream = response.GetResponseStream();//将FTP上的文件转化为数据流 int bufferSize = 2048;//缓冲大小,单位byte byte[] buffer = new byte[bufferSize];//数据包 int readCount;//循环次数 readCount = ftpStream.Read(buffer, 0, bufferSize);//计算循环次数 //③将FTP文件内容写入到本地空文件中去 while (readCount > 0) { outputStream.Write(buffer, 0, readCount);//写入每一次读取的数据流 readCount = ftpStream.Read(buffer, 0, bufferSize);//重新计算次数 } //④关闭IO ftpStream.Close(); outputStream.Close(); response.Close(); setPicBoxSet(_path + "\\" + files[files.Length - 1]); } catch (Exception ex) { } } /// /// 加载 /// /// /// private void frmLocalTruckImageShow_Load(object sender, EventArgs e) { if (!Directory.Exists(_path)) { Directory.CreateDirectory(_path); } try { DirectoryInfo dir = new DirectoryInfo(_path); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录 foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) //判断是否文件夹 { DirectoryInfo subdir = new DirectoryInfo(i.FullName); subdir.Delete(true); //删除子目录和文件 } else { File.Delete(i.FullName); //删除指定文件 } } cmbRecordNo.Items.Add(parame.RecordNo1); cmbRecordNo.Items.Add(parame.RecordNo2); GetImgeNme(); cmbRecordNo.SelectedIndex = 0; textBox1.Text = _path; } catch(Exception ee) { } } /// /// 获取图片名称 /// void GetImgeNme() { foreach (var pp in cmbRecordNo.Items) { string ss = pp.ToString(); if (ss != null) { string no = pp.ToString(); var loadinfo = EntityHelper.GetData( "com.steering.Mcms.CmmWorkImageServer.doQuery", new object[] { no }, ob); foreach (var p in loadinfo) { List aa = new List(); string num = null; foreach (PropertyInfo t in p.GetType().GetProperties()) { string name = t.Name.ToString();//字段名称 var value = t.GetValue(p, null);//字段值 var type = t.PropertyType.Name;//字段类型 if (name.Contains("ImageFile") && value != null) { aa.Add(value.ToString()); } if (name == "ActualFirstNo") { num = value.ToString(); } } dic.Add(num, aa); } } } } private void cmbRecordNo_SelectedIndexChanged(object sender, EventArgs e) { dicPic = new Dictionary(); try { setPanelRemoveMsg(pnlpic2); ipic = 0; foreach (var p in dic[cmbRecordNo.Text]) { ftpDown(p); } } catch { } } private void setPicBoxSet(string path) { PictureBox pb = new PictureBox(); pb.Name = "pb" + ipic; pb.Dock = DockStyle.Top; pb.Height = 134; pb.DoubleClick += new EventHandler(PictureBoxDoubleClick); pb.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; pnlpic2.Controls.Add(pb); pb.ImageLocation = path; pb.SizeMode = PictureBoxSizeMode.Zoom; dicPic.Add(pb.Name, path); ipic++; } private void PictureBoxDoubleClick(object sender, EventArgs e) { PictureBox picture = (PictureBox)sender; picMax.ImageLocation = dicPic[picture.Name]; pnlpicMax.Visible = true; } private int iPic = 0; /// /// 不显示大图 /// private void pictureShow_DoubleClick(object sender, EventArgs e) { pnlpicMax.Visible = false; } private void setPanelRemoveMsg(Panel p) { if (p.InvokeRequired) { Action action = new Action(setPanelRemovenfo); Invoke(action, new object[] { p }); } else { for (int i = p.Controls.Count - 1; i >= 0; i--) { p.Controls.RemoveAt(i); } } } public void setPanelRemovenfo(Panel control) { setPanelRemoveMsg(control); } } }