frmLocalTruckImageShow.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. try
  92. {
  93. DirectoryInfo dir = new DirectoryInfo(_path);
  94. FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
  95. foreach (FileSystemInfo i in fileinfo)
  96. {
  97. if (i is DirectoryInfo) //判断是否文件夹
  98. {
  99. DirectoryInfo subdir = new DirectoryInfo(i.FullName);
  100. subdir.Delete(true); //删除子目录和文件
  101. }
  102. else
  103. {
  104. File.Delete(i.FullName); //删除指定文件
  105. }
  106. }
  107. cmbRecordNo.Items.Add(parame.RecordNo1);
  108. cmbRecordNo.Items.Add(parame.RecordNo2);
  109. GetImgeNme();
  110. cmbRecordNo.SelectedIndex = 0;
  111. textBox1.Text = _path;
  112. }
  113. catch
  114. {
  115. }
  116. }
  117. /// <summary>
  118. /// 获取图片名称
  119. /// </summary>
  120. void GetImgeNme()
  121. {
  122. foreach (var pp in cmbRecordNo.Items)
  123. {
  124. string ss = pp.ToString();
  125. if (ss != null)
  126. {
  127. string no = pp.ToString();
  128. var loadinfo = EntityHelper.GetData<CmmWorkImageEntity>(
  129. "com.steering.Mcms.CmmWorkImageServer.doQuery",
  130. new object[] { no },
  131. ob);
  132. foreach (var p in loadinfo)
  133. {
  134. List<string> aa = new List<string>();
  135. string num = null;
  136. foreach (PropertyInfo t in p.GetType().GetProperties())
  137. {
  138. string name = t.Name.ToString();//字段名称
  139. var value = t.GetValue(p, null);//字段值
  140. var type = t.PropertyType.Name;//字段类型
  141. if (name.Contains("ImageFile") && value != null)
  142. {
  143. aa.Add(value.ToString());
  144. }
  145. if (name == "ActualFirstNo")
  146. {
  147. num = value.ToString();
  148. }
  149. }
  150. dic.Add(num, aa);
  151. }
  152. }
  153. }
  154. }
  155. private void cmbRecordNo_SelectedIndexChanged(object sender, EventArgs e)
  156. {
  157. dicPic = new Dictionary<string, string>();
  158. try
  159. {
  160. setPanelRemoveMsg(pnlpic2);
  161. ipic = 0;
  162. foreach (var p in dic[cmbRecordNo.Text])
  163. {
  164. ftpDown(p);
  165. }
  166. }
  167. catch
  168. { }
  169. }
  170. private void setPicBoxSet(string path)
  171. {
  172. PictureBox pb = new PictureBox();
  173. pb.Name = "pb" + ipic;
  174. pb.Dock = DockStyle.Top;
  175. pb.Height = 134;
  176. pb.DoubleClick += new EventHandler(PictureBoxDoubleClick);
  177. pb.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  178. pnlpic2.Controls.Add(pb);
  179. pb.ImageLocation = path;
  180. pb.SizeMode = PictureBoxSizeMode.Zoom;
  181. dicPic.Add(pb.Name, path);
  182. ipic++;
  183. }
  184. private void PictureBoxDoubleClick(object sender, EventArgs e)
  185. {
  186. PictureBox picture = (PictureBox)sender;
  187. picMax.ImageLocation = dicPic[picture.Name];
  188. pnlpicMax.Visible = true;
  189. }
  190. private int iPic = 0;
  191. /// <summary>
  192. /// 不显示大图
  193. /// </summary>
  194. private void pictureShow_DoubleClick(object sender, EventArgs e)
  195. {
  196. pnlpicMax.Visible = false;
  197. }
  198. private void setPanelRemoveMsg(Panel p)
  199. {
  200. if (p.InvokeRequired)
  201. {
  202. Action<Panel> action = new Action<Panel>(setPanelRemovenfo);
  203. Invoke(action, new object[] { p });
  204. }
  205. else
  206. {
  207. for (int i = p.Controls.Count - 1; i >= 0; i--)
  208. {
  209. p.Controls.RemoveAt(i);
  210. }
  211. }
  212. }
  213. public void setPanelRemovenfo(Panel control)
  214. {
  215. setPanelRemoveMsg(control);
  216. }
  217. }
  218. }