frmLocalTruckImageShow.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Windows.Forms;
  6. using Core.Mes.Client.Comm.Tool;
  7. using Core.StlMes.Client.LgResMgt.Mcms.entity;
  8. using CoreFS.CA06;
  9. using Infragistics.Win.UltraWinGrid;
  10. using System.Net;
  11. using System.Reflection;
  12. using System.IO;
  13. using System.Threading;
  14. namespace Core.StlMes.Client.LgResMgt.Mcms
  15. {
  16. public partial class frmLocalTruckImageShow : FrmBase
  17. {
  18. public frmLocalTruckImageShow(OpeBase _ob)
  19. {
  20. InitializeComponent();
  21. ob = _ob;
  22. }
  23. FtpWebRequest reqFTP;
  24. private static object sync = new object();
  25. private static frmLocalTruckImageShow instance;
  26. private static readonly string _path = Environment.CurrentDirectory + "\\imgShort\\picture";
  27. // 有参数
  28. private CmmWeightResultEntity parame;
  29. private CmmBaseSpotInfoEntity spot;
  30. Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();
  31. Dictionary<string, string> dicPic;
  32. int ipic = 0;
  33. public static frmLocalTruckImageShow GetSingleton(OpeBase _ob, CmmWeightResultEntity parame, CmmBaseSpotInfoEntity spot)
  34. {
  35. lock (sync)
  36. {
  37. ;
  38. if (instance == null || instance.IsDisposed)
  39. instance = new frmLocalTruckImageShow(_ob);
  40. instance.parame = parame;
  41. instance.spot = spot;
  42. }
  43. return instance;
  44. }
  45. void ftpDown( string picName)
  46. {
  47. string[] files = picName.Split(new char[] { '/' });
  48. try
  49. {
  50. //①读取FTP上的文件
  51. // FileStream outputStream = new FileStream(LocalDir, FileMode.Create);//建立FTP链接
  52. if (File.Exists(_path + "\\" + files[files.Length - 1]))
  53. {
  54. File.Delete(_path + "\\" + files[files.Length - 1]);
  55. }
  56. FileStream outputStream = new FileStream(_path + "\\" + files[files.Length-1], FileMode.Create);
  57. reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://"+spot.FtpIp + spot.Ftppath + picName));//读取FTP上的文件
  58. reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;//FTP下载协议
  59. reqFTP.UseBinary = true;//指定文件传输类型
  60. reqFTP.Credentials = new NetworkCredential(spot.FtpUserName, spot.FtpPassword);//FTP通信凭证(即登录ftp)
  61. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();//登录成功
  62. //②将FTP上的文件内容转化成数据流
  63. Stream ftpStream = response.GetResponseStream();//将FTP上的文件转化为数据流
  64. int bufferSize = 2048;//缓冲大小,单位byte
  65. byte[] buffer = new byte[bufferSize];//数据包
  66. int readCount;//循环次数
  67. readCount = ftpStream.Read(buffer, 0, bufferSize);//计算循环次数
  68. //③将FTP文件内容写入到本地空文件中去
  69. while (readCount > 0)
  70. {
  71. outputStream.Write(buffer, 0, readCount);//写入每一次读取的数据流
  72. readCount = ftpStream.Read(buffer, 0, bufferSize);//重新计算次数
  73. }
  74. //④关闭IO
  75. ftpStream.Close();
  76. outputStream.Close();
  77. response.Close();
  78. setPicBoxSet(_path + "\\" + files[files.Length - 1]);
  79. }
  80. catch (Exception ex)
  81. {
  82. }
  83. }
  84. /// <summary>
  85. /// 加载
  86. /// </summary>
  87. /// <param name="sender"></param>
  88. /// <param name="e"></param>
  89. private void frmLocalTruckImageShow_Load(object sender, EventArgs e)
  90. {
  91. if (!Directory.Exists(_path))
  92. {
  93. Directory.CreateDirectory(_path);
  94. }
  95. try
  96. {
  97. DirectoryInfo dir = new DirectoryInfo(_path);
  98. FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
  99. foreach (FileSystemInfo i in fileinfo)
  100. {
  101. if (i is DirectoryInfo) //判断是否文件夹
  102. {
  103. DirectoryInfo subdir = new DirectoryInfo(i.FullName);
  104. subdir.Delete(true); //删除子目录和文件
  105. }
  106. else
  107. {
  108. File.Delete(i.FullName); //删除指定文件
  109. }
  110. }
  111. cmbRecordNo.Items.Add(parame.RecordNo1);
  112. cmbRecordNo.Items.Add(parame.RecordNo2);
  113. GetImgeNme();
  114. cmbRecordNo.SelectedIndex = 0;
  115. textBox1.Text = _path;
  116. }
  117. catch(Exception ee)
  118. {
  119. }
  120. }
  121. /// <summary>
  122. /// 获取图片名称
  123. /// </summary>
  124. void GetImgeNme()
  125. {
  126. foreach (var pp in cmbRecordNo.Items)
  127. {
  128. string ss = pp.ToString();
  129. if (ss != null)
  130. {
  131. string no = pp.ToString();
  132. var loadinfo = EntityHelper.GetData<CmmWorkImageEntity>(
  133. "com.steering.Mcms.CmmWorkImageServer.doQuery",
  134. new object[] { no },
  135. ob);
  136. foreach (var p in loadinfo)
  137. {
  138. List<string> aa = new List<string>();
  139. string num = null;
  140. foreach (PropertyInfo t in p.GetType().GetProperties())
  141. {
  142. string name = t.Name.ToString();//字段名称
  143. var value = t.GetValue(p, null);//字段值
  144. var type = t.PropertyType.Name;//字段类型
  145. if (name.Contains("ImageFile") && value != null)
  146. {
  147. aa.Add(value.ToString());
  148. }
  149. if (name == "ActualFirstNo")
  150. {
  151. num = value.ToString();
  152. }
  153. }
  154. dic.Add(num, aa);
  155. }
  156. }
  157. }
  158. }
  159. private void cmbRecordNo_SelectedIndexChanged(object sender, EventArgs e)
  160. {
  161. dicPic = new Dictionary<string, string>();
  162. try
  163. {
  164. setPanelRemoveMsg(pnlpic2);
  165. ipic = 0;
  166. foreach (var p in dic[cmbRecordNo.Text])
  167. {
  168. ftpDown(p);
  169. }
  170. }
  171. catch
  172. { }
  173. }
  174. private void setPicBoxSet(string path)
  175. {
  176. PictureBox pb = new PictureBox();
  177. pb.Name = "pb" + ipic;
  178. pb.Dock = DockStyle.Top;
  179. pb.Height = 134;
  180. pb.DoubleClick += new EventHandler(PictureBoxDoubleClick);
  181. pb.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  182. pnlpic2.Controls.Add(pb);
  183. pb.ImageLocation = path;
  184. pb.SizeMode = PictureBoxSizeMode.Zoom;
  185. dicPic.Add(pb.Name, path);
  186. ipic++;
  187. }
  188. private void PictureBoxDoubleClick(object sender, EventArgs e)
  189. {
  190. PictureBox picture = (PictureBox)sender;
  191. picMax.ImageLocation = dicPic[picture.Name];
  192. pnlpicMax.Visible = true;
  193. }
  194. private int iPic = 0;
  195. /// <summary>
  196. /// 不显示大图
  197. /// </summary>
  198. private void pictureShow_DoubleClick(object sender, EventArgs e)
  199. {
  200. pnlpicMax.Visible = false;
  201. }
  202. private void setPanelRemoveMsg(Panel p)
  203. {
  204. if (p.InvokeRequired)
  205. {
  206. Action<Panel> action = new Action<Panel>(setPanelRemovenfo);
  207. Invoke(action, new object[] { p });
  208. }
  209. else
  210. {
  211. for (int i = p.Controls.Count - 1; i >= 0; i--)
  212. {
  213. p.Controls.RemoveAt(i);
  214. }
  215. }
  216. }
  217. public void setPanelRemovenfo(Panel control)
  218. {
  219. setPanelRemoveMsg(control);
  220. }
  221. }
  222. }