ParticlesGridDevidePage.cs 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  1. using NPOI.HSSF.UserModel;
  2. using NPOI.SS.UserModel;
  3. using OTSIncAReportApp;
  4. using OTSIncAReportApp.DataOperation.DataAccess;
  5. using OTSIncAReportApp.DataOperation.Model;
  6. using OTSIncAReportApp.SysMgrTools;
  7. using OTSIncAReportGraph.Class;
  8. using OTSIncAReportGrids.OTSIncAReportGridsFuncation;
  9. using OTSIncAReportMailInterface;
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.Data;
  14. using System.Drawing;
  15. using System.Drawing.Imaging;
  16. using System.IO;
  17. using System.Linq;
  18. using System.Reflection;
  19. using System.Threading;
  20. using System.Windows.Forms;
  21. namespace OTSIncAReportGrids
  22. {
  23. public partial class ParticlesGridDevidePage : UserControl
  24. {
  25. #region 枚举定义
  26. /// <summary>
  27. /// 样品台X轴方向
  28. /// </summary>
  29. enum OTS_X_AXIS_DIRECTION
  30. {
  31. LEFT_TOWARD = 0,
  32. RIGHT_TOWARD = 1
  33. }
  34. /// <summary>
  35. /// 样品台Y轴方向
  36. /// </summary>
  37. enum OTS_Y_AXIS_DIRECTION
  38. {
  39. UP_TOWARD = 0,
  40. DOWN_TOWARD = 1
  41. }
  42. #endregion
  43. //设置模拟数据表
  44. public DataTable m_dt = new DataTable();
  45. //底层操作类
  46. OTSReportGridsFun m_OTSIncAReportGridsFun = null;
  47. //进度条窗体
  48. public Frm_UserProgress m_frm_userprogress;
  49. //移动SEM到指定位置发生线程
  50. private Thread m_mythread;
  51. //记录线程是否已经运行完成的状态
  52. private bool m_mythread_state = false;
  53. //导出图像文件的路径
  54. private string m_imagefilepath = "";
  55. //显示元素列表
  56. public List<string> m_list_elementscolname = null;
  57. //单个数据源所拥有的列数,这里保存下来,供导出模块使用
  58. public int m_oneresult_columncount = 0;
  59. //多个数据源,每个数据源拥有的列数,这里保存下来,供分组使用
  60. public List<int> m_list_oneresult_columncount;
  61. //国际化
  62. Language lan;
  63. Hashtable table;
  64. //测量结果
  65. ResultFile result = null;
  66. FieldData fieldData;
  67. DataTable particlesAll;
  68. ParticleData Particledata;
  69. NLog.Logger log= NLog.LogManager.GetCurrentClassLogger();
  70. #region 分页器相关
  71. #region 分页字段和属性
  72. string condition = "";
  73. public string FileName = null;
  74. private int pageIndex = 1;
  75. /// <summary>
  76. /// 当前页面
  77. /// </summary>
  78. public virtual int PageIndex
  79. {
  80. get { return pageIndex; }
  81. set { pageIndex = value; }
  82. }
  83. private int pageSize = 20;
  84. /// <summary>
  85. /// 每页记录数
  86. /// </summary>
  87. public virtual int PageSize
  88. {
  89. get { return pageSize; }
  90. set { pageSize = value; }
  91. }
  92. //string OrderFunction = "fieldid,particleid";
  93. private int recordCount = 0;
  94. /// <summary>
  95. /// 总记录数
  96. /// </summary>
  97. public virtual int RecordCount
  98. {
  99. get { return recordCount; }
  100. set { recordCount = value; }
  101. }
  102. private int pageCount = 0;
  103. /// <summary>
  104. /// 总页数
  105. /// </summary>
  106. public int PageCount
  107. {
  108. get
  109. {
  110. pageCount = GetPageCount();
  111. return pageCount;
  112. }
  113. }
  114. /// <summary>
  115. /// 计算总页数
  116. /// </summary>
  117. /// <returns></returns>
  118. private int GetPageCount()
  119. {
  120. if (PageSize == 0)
  121. {
  122. return 0;
  123. }
  124. int pageCount = RecordCount / PageSize;
  125. if (RecordCount % PageSize == 0)
  126. {
  127. pageCount = RecordCount / PageSize;
  128. }
  129. else
  130. {
  131. pageCount = RecordCount / PageSize + 1;
  132. }
  133. return pageCount;
  134. }
  135. #endregion
  136. frmReportApp m_ReportApp;
  137. #endregion
  138. public ParticlesGridDevidePage(frmReportApp ReportApp)
  139. {
  140. m_ReportApp = ReportApp;
  141. result = m_ReportApp.m_rstDataMgr.ResultFilesList[m_ReportApp.m_rstDataMgr.GetWorkingResult()];
  142. //设置窗体的双缓冲,以保证大数据时拖动不卡
  143. this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint, true);
  144. this.UpdateStyles();
  145. InitializeComponent();
  146. //利用反射设置DataGridView的双缓冲
  147. Type dgvType = this.dgV_ParticlesDevidePage.GetType();
  148. PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
  149. pi.SetValue(this.dgV_ParticlesDevidePage, true, null);
  150. }
  151. bool Init()
  152. {
  153. try
  154. {
  155. lan = new Language(this);
  156. table = lan.GetNameTable(this.Name);
  157. m_mythread_state = false;
  158. m_mythread = new Thread(new ParameterizedThreadStart(Thread_GO));
  159. //m_frm_userprogress = new Frm_UserProgress();
  160. fieldData = new FieldData(result.FilePath);
  161. Particledata = new ParticleData(result.FilePath);
  162. //初始化底层操作类
  163. m_OTSIncAReportGridsFun = new OTSReportGridsFun(m_ReportApp, this);
  164. return true;
  165. }
  166. catch(Exception ex)
  167. {
  168. log.Error(ex.ToString());
  169. return false;
  170. }
  171. }
  172. private void ParticlesGridDevidePage_Load(object sender, EventArgs e)
  173. {
  174. if (!Init())
  175. {
  176. return;
  177. }
  178. ProgressStart();//进度条关闭通过cbB_PageSize.SelectedIndex事件
  179. if (!UpdateTable())
  180. {
  181. return;
  182. }
  183. cbB_PageSize.Items.Add("20");
  184. cbB_PageSize.Items.Add("100");
  185. cbB_PageSize.Items.Add("500");
  186. cbB_PageSize.Items.Add("1000");
  187. cbB_PageSize.Items.Add("5000");
  188. cbB_PageSize.Items.Add("All");
  189. cbB_PageSize.SelectedIndex = 0;
  190. }
  191. #region 自定义方法
  192. bool UpdateTable()
  193. {
  194. int sel = m_ReportApp.m_conditionChoose.m_SourceGridData.GetComboDownListIndexByItemName(OTSIncAReportApp.OTSSampleReportInfo.OTS_REPORT_PROP_GRID_ITEMS.DATA_SOURCE_TYPE);
  195. if (sel == 1)
  196. {
  197. List<OTSIncAReportApp.DataOperation.Model.Particle> selectParticles = m_ReportApp.GetSelectedParticles();
  198. DataTable dtUelect = new DataTable();
  199. dtUelect.Columns.Add("fieldid");
  200. dtUelect.Columns.Add("particleid");
  201. dtUelect.Columns.Add("AveGray");
  202. dtUelect.Columns.Add("RectLeft");
  203. dtUelect.Columns.Add("RectTop");
  204. dtUelect.Columns.Add("RectWidth");
  205. dtUelect.Columns.Add("RectHeight");
  206. dtUelect.Columns.Add("Area");
  207. dtUelect.Columns.Add("PosX");
  208. dtUelect.Columns.Add("PosY");
  209. dtUelect.Columns.Add("TypeId");
  210. //dtUelect.Columns.Add("ElementNum");
  211. dtUelect.Columns.Add("SegmentNum");
  212. dtUelect.Columns.Add("SEMPosX");
  213. dtUelect.Columns.Add("SEMPosY");
  214. dtUelect.Columns.Add("XrayId");
  215. dtUelect.Columns.Add("DMAX");
  216. dtUelect.Columns.Add("DMIN");
  217. dtUelect.Columns.Add("DPERP");
  218. dtUelect.Columns.Add("PERIMETER");
  219. dtUelect.Columns.Add("ORIENTATION");
  220. dtUelect.Columns.Add("DINSCR");
  221. dtUelect.Columns.Add("DMEAN");
  222. dtUelect.Columns.Add("DELONG");
  223. dtUelect.Columns.Add("DFERET");
  224. dtUelect.Columns.Add("TypeName");
  225. dtUelect.Columns.Add("TypeColor");
  226. dtUelect.Columns.Add("SubParticles");
  227. dtUelect.Columns.Add("Element");
  228. for(int i=0;i< selectParticles.Count;i++)
  229. {
  230. dtUelect.Rows.Add(selectParticles[i].FieldId, selectParticles[i].ParticleId, selectParticles[i].AveGray, selectParticles[i].RectLeft, selectParticles[i].RectTop, selectParticles[i].RectWidth, selectParticles[i].RectHeight, selectParticles[i].Area, selectParticles[i].PosX, selectParticles[i].PosX, selectParticles[i].TypeId, /*selectParticles[i].ElementNum,*/ selectParticles[i].SegmentNum, selectParticles[i].SEMPosX, selectParticles[i].SEMPosY, selectParticles[i].ParticleId, selectParticles[i].DMAX, selectParticles[i].DMIN, selectParticles[i].DPERP, selectParticles[i].PERIMETER, selectParticles[i].ORIENTATION, selectParticles[i].DINSCR, selectParticles[i].DMEAN, selectParticles[i].DELONG, selectParticles[i].DFERET, selectParticles[i].TypeName, selectParticles[i].TypeColor, "", "");
  231. }
  232. particlesAll = new DataTable();
  233. particlesAll = dtUelect.Copy();
  234. btn_Sel.Enabled = false;
  235. }
  236. else
  237. {
  238. //多次测试发现 读取数据库时间耗时长,但读取条数目对读取数据库时长影响不大,故此处直接读取所有数据库信息,以取代多次连接数据库方式,优化分页速度
  239. particlesAll = new DataTable();
  240. particlesAll = Particledata.GetInfoForPartucleDevidePage2(condition);
  241. btn_Sel.Enabled = true;
  242. }
  243. if(particlesAll==null)
  244. {
  245. log.Error("There is an exception in the data of the database!");
  246. #region 加载进度条进度部份结束
  247. //加载完成设置鼠标为默认
  248. this.Cursor = Cursors.Default;
  249. //加载完成,关闭进度条
  250. m_frm_userprogress.Close();
  251. #endregion
  252. return false;
  253. }
  254. DataTable elementchemistry = Particledata.GetElementChemistry();
  255. for (int i = 0; i < particlesAll.Rows.Count; i++)
  256. {
  257. string str = "XRayId = " + particlesAll.Rows[i]["particleId"].ToString() + " and fieldid = " + particlesAll.Rows[i]["fieldid"].ToString();
  258. DataRow[] drs = elementchemistry.Select(str);
  259. string ConcatenatedString = "";
  260. for (int j = 0; j < drs.Length; j++)
  261. {
  262. ConcatenatedString += drs[j]["name"] + "-" + drs[j]["Percentage"] + ';';
  263. }
  264. particlesAll.Rows[i]["Element"] = ConcatenatedString;
  265. }
  266. return true;
  267. }
  268. /// <summary>
  269. /// 传入单颗颗粒的particle类对象,返回从field中抠取出的bitmap对象,抠取单颗颗粒
  270. /// </summary>
  271. /// <param name="in_cotsparticleclr"></param>
  272. /// <returns></returns>
  273. public Bitmap GetBitmapByParticle(string fieldImage, Rectangle offset_rect)
  274. {
  275. //然后将取出的数据,转换成Bitmap对象
  276. Bitmap ls_bt = DrawFunction.ReadImageFile(fieldImage);
  277. //为了能把整个颗粒显示完整
  278. offset_rect.X = offset_rect.X - 5;
  279. offset_rect.Y = offset_rect.Y - 5;
  280. offset_rect.Width = offset_rect.Width + 10;
  281. offset_rect.Height = offset_rect.Height + 10;
  282. //防止计算偏差后,有坐标溢出现象
  283. if (offset_rect.X < 0)
  284. offset_rect.X = 0;
  285. if (offset_rect.Y < 0)
  286. offset_rect.Y = 0;
  287. if (offset_rect.X + offset_rect.Width > ls_bt.Width)
  288. {
  289. offset_rect.Width = ls_bt.Width - offset_rect.X;
  290. }
  291. if (offset_rect.Y + offset_rect.Height > ls_bt.Height)
  292. {
  293. offset_rect.Height = ls_bt.Height - offset_rect.Y;
  294. }
  295. //
  296. Bitmap new_ret_bp;
  297. //防止为0后面计算出错
  298. if (offset_rect.Width > 0 && offset_rect.Height > 0)
  299. {
  300. //最后通过list_showsegment组建成新的图片,进行返回
  301. new_ret_bp = ls_bt.Clone(offset_rect, PixelFormat.Format8bppIndexed);
  302. }
  303. else
  304. {
  305. new_ret_bp = new Bitmap(offset_rect.Width, offset_rect.Height);
  306. }
  307. return new_ret_bp;
  308. }
  309. public Bitmap GetBitmapForBig(string sub, ParticleData fielddata, double xs)
  310. {
  311. string vs = "," + sub.Replace(':', '-') + ",";
  312. DataTable dataTable = fielddata.GetParticleAll(vs);
  313. if (dataTable.Rows.Count == 0)
  314. {
  315. return null;
  316. }
  317. string path = result.FilePath;
  318. //内接矩形
  319. double max_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs - Convert.ToInt64(dataTable.Rows[0]["RectTop"]);
  320. double max_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs + Convert.ToInt64(dataTable.Rows[0]["RectLeft"]);
  321. double min_Y = max_Y;
  322. double min_X = max_X;
  323. //拼接field矩形
  324. double MAX_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs;
  325. double MAX_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs;
  326. double MIN_Y = MAX_Y;
  327. double MIN_X = MAX_X;
  328. foreach (DataRow item in dataTable.Rows)
  329. {
  330. //颗粒外接矩形
  331. double lefttopX = Convert.ToInt64(item["FieldPosX"]) * xs + Convert.ToInt64(item["RectLeft"]);
  332. if (lefttopX < min_X)
  333. {
  334. min_X = lefttopX;
  335. }
  336. if (lefttopX + Convert.ToInt64(item["RectWidth"]) > max_X)
  337. {
  338. max_X = lefttopX + Convert.ToInt64(item["RectWidth"]);
  339. }
  340. double lrfttopY = Convert.ToInt64(item["FieldPosY"]) * xs - Convert.ToInt64(item["RectTop"]);
  341. if (max_Y < lrfttopY)
  342. {
  343. max_Y = lrfttopY;
  344. }
  345. if (min_Y > lrfttopY - Convert.ToInt64(item["RectHeight"]))
  346. {
  347. min_Y = lrfttopY - Convert.ToInt64(item["RectHeight"]);
  348. }
  349. //画布
  350. double lefttopXH = Convert.ToInt64(item["FieldPosX"]) * xs;
  351. if (lefttopXH > MAX_X)
  352. {
  353. MAX_X = lefttopXH;
  354. }
  355. if (lefttopXH < MIN_X)
  356. {
  357. MIN_X = lefttopXH;
  358. }
  359. double lrfttopYH = Convert.ToInt64(item["FieldPosY"]) * xs;
  360. if (MAX_Y < lrfttopYH)
  361. {
  362. MAX_Y = lrfttopYH;
  363. }
  364. if (MIN_Y > lrfttopYH)
  365. {
  366. MIN_Y = lrfttopYH;
  367. }
  368. }
  369. int WIDTH = Convert.ToInt32(MAX_X - MIN_X) + 1024;
  370. int HEIGHT = Convert.ToInt32(MAX_Y - MIN_Y) + 768;
  371. //构造最终的图片白板
  372. Bitmap tableChartImage = new Bitmap(WIDTH, HEIGHT);
  373. Graphics graph = Graphics.FromImage(tableChartImage);
  374. //初始化这个大图
  375. graph.DrawImage(tableChartImage, 0, 0);
  376. int width = Convert.ToInt32(max_X - min_X);
  377. int height = Convert.ToInt32(max_Y - min_Y);
  378. int X = Convert.ToInt32(min_X - MIN_X);
  379. int Y = Convert.ToInt32(MAX_Y - max_Y);
  380. Rectangle rectangle = new Rectangle() { X = X, Y = Y, Width = width, Height = height };
  381. foreach (DataRow item in dataTable.Rows)
  382. {
  383. string filePath = path + "\\FIELD_FILES\\";
  384. string imagePath = filePath + "Field" + item["fieldid"].ToString() + ".bmp";
  385. //然后将取出的数据,转换成Bitmap对象
  386. Bitmap ls_bt = DrawFunction.ReadImageFile(imagePath);
  387. int x = Convert.ToInt32(Convert.ToDouble(item["FieldPosX"]) * xs - MIN_X);
  388. int y = System.Math.Abs(Convert.ToInt32(Convert.ToDouble(item["FieldPosY"]) * xs - MAX_Y));
  389. try
  390. {
  391. graph.DrawImage(ls_bt, x, y);
  392. }
  393. catch /*(Exception e)*/
  394. {
  395. throw;
  396. }
  397. }
  398. Bitmap bmap = tableChartImage.Clone(rectangle, PixelFormat.Format8bppIndexed);
  399. return bmap;
  400. }
  401. private void BindDataGridView()
  402. {
  403. dgV_ParticlesDevidePage.Visible = false;
  404. if (m_frm_userprogress.IsDisposed)
  405. {
  406. ProgressStart();
  407. }
  408. dgV_ParticlesDevidePage.Rows.Clear();
  409. dgV_ParticlesDevidePage.Columns.Clear();
  410. //从报告xml文件中加载,显示计算列,显示元素信息
  411. string str_DefaultComputedColName = "", str_ElementsColName = "";
  412. DataSet ds = OTSIncAReportApp.DataOperation.DataAccess.XMLoperate.GetXmlData(Application.StartupPath + m_ReportApp.m_OTSReportMgrParamFile, "XMLData");
  413. DataTable dt = ds.Tables["Member"];
  414. foreach (DataRow element in dt.Rows)
  415. {
  416. string RegName = element["RegName"].ToString();
  417. if (RegName == "DefaultComputedColName")
  418. {
  419. str_DefaultComputedColName = element["strValue"].ToString();
  420. }
  421. if (RegName == "ElementsColName")
  422. {
  423. str_ElementsColName = element["strValue"].ToString();
  424. }
  425. }
  426. //获取需要显示的计算列
  427. string[] strs = str_DefaultComputedColName.Split(',');
  428. //列名
  429. Dictionary<string, string> keyValues = new Dictionary<string, string>() { };
  430. keyValues.Add("rowid", table["str4"].ToString());
  431. keyValues.Add("TypeName", table["str6"].ToString());
  432. keyValues.Add("ParticleImage", table["str5"].ToString());
  433. keyValues.Add("FieldID", "FieldID");
  434. keyValues.Add("SEMPosX", "SEMPosX");
  435. keyValues.Add("SEMPosY", "SEMPosY");
  436. for (int i = 0; i < strs.Count(); i++)
  437. {
  438. if (strs[i] == "Area")
  439. {
  440. keyValues.Add("Area", table["str21"].ToString());
  441. }
  442. if (strs[i] == "EquivalentCircleDiameter")
  443. {
  444. keyValues.Add("Equivalent", table["str22"].ToString());
  445. }
  446. if (strs[i] == "MaxDiameter")
  447. {
  448. keyValues.Add("DMAX", table["str23"].ToString());
  449. }
  450. if (strs[i] == "MinDiameter")
  451. {
  452. keyValues.Add("DMIN", table["str24"].ToString());
  453. }
  454. if (strs[i] == "DiameterRatio")
  455. {
  456. keyValues.Add("DiameterRatio", table["str25"].ToString());
  457. }
  458. if (strs[i] == "FerretDiameter")
  459. {
  460. keyValues.Add("DFERET", table["str26"].ToString());
  461. }
  462. if (strs[i] == "PERP")
  463. {
  464. keyValues.Add("DPERP", table["str27"].ToString());
  465. }
  466. if (strs[i] == "PERI")
  467. {
  468. keyValues.Add("PERIMETER", table["str28"].ToString());
  469. }
  470. if (strs[i] == "INSCR")
  471. {
  472. keyValues.Add("DINSCR", table["str29"].ToString());
  473. }
  474. if (strs[i] == "MEAN")
  475. {
  476. keyValues.Add("DMEAN", table["str30"].ToString());
  477. }
  478. if (strs[i] == "ELONG")
  479. {
  480. keyValues.Add("DELONG", table["str31"].ToString());
  481. }
  482. if (strs[i] == "ASPECT_ELONG")
  483. {
  484. keyValues.Add("ASPECT_ELONG", table["str32"].ToString());
  485. }
  486. if (strs[i] == "Orientation")
  487. {
  488. keyValues.Add("ORIENTATION", table["str33"].ToString());
  489. }
  490. }
  491. keyValues.Add("Element", "Element");
  492. Dictionary<string, string>.Enumerator en = keyValues.GetEnumerator();
  493. for (int irow = 0; irow < keyValues.Count; irow++)
  494. {
  495. if (en.MoveNext())
  496. {
  497. if (en.Current.Key == "ParticleImage")
  498. {
  499. DataGridViewImageColumn iconColumn = new DataGridViewImageColumn();
  500. iconColumn.Name = en.Current.Key;
  501. iconColumn.HeaderText = en.Current.Value;
  502. dgV_ParticlesDevidePage.Columns.Add(iconColumn);
  503. }
  504. else if (en.Current.Key == "Element")
  505. {
  506. }
  507. else
  508. {
  509. int columndid = dgV_ParticlesDevidePage.Columns.Add(en.Current.Key, en.Current.Value);
  510. }
  511. }
  512. }
  513. if(PageSize==-1)
  514. {
  515. PageSize = particlesAll.Rows.Count;
  516. }
  517. if (particlesAll == null)
  518. {
  519. return;
  520. }
  521. RecordCount = particlesAll.Rows.Count;
  522. DataTable particles = particlesAll.Clone();
  523. for (int fi = (PageIndex - 1) * pageSize; fi < PageIndex * pageSize; fi++)
  524. {
  525. if (fi > RecordCount - 1)
  526. {
  527. break;
  528. }
  529. particles.ImportRow(particlesAll.Rows[fi]);
  530. }
  531. string particleM = "";
  532. //获取需要显示的元素名
  533. List<string> ElementTypeSort = new List<string>(str_ElementsColName.Split(',').ToList());//去重
  534. for (int i = 0; i < ElementTypeSort.Count; i++)
  535. {
  536. dgV_ParticlesDevidePage.Columns.Add(ElementTypeSort[i], ElementTypeSort[i]);
  537. }
  538. double jd = 95f / (double)particles.Rows.Count;//计算进度刻度
  539. string filePath = result.FilePath + "\\FIELD_FILES\\";
  540. KeyValuePair<string, Bitmap> FieldImage = new KeyValuePair<string, Bitmap>();
  541. for (int i = 0; i < particles.Rows.Count; i++)
  542. {
  543. //更新进度,每100条记录加载完,更新一次进度
  544. if (i % 10 == 0)
  545. m_frm_userprogress.SetProgressValueAndText((int)(jd * i), "loading..");
  546. Dictionary<string, string>.Enumerator enl = keyValues.GetEnumerator();
  547. int add_rowindex = dgV_ParticlesDevidePage.Rows.Add();
  548. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[0].Value = (pageSize * (PageIndex - 1) + add_rowindex + 1).ToString();
  549. for (int k = 0; k < keyValues.Count; k++)
  550. {
  551. if (enl.MoveNext())
  552. {
  553. if (enl.Current.Key == "rowid")
  554. {
  555. //dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = i + 1;
  556. }
  557. if (enl.Current.Key == "ParticleImage")
  558. {
  559. string subt = particles.Rows[i]["SubParticles"].ToString();
  560. if (subt != null && subt != "")
  561. {
  562. double ScanFieldSize = Convert.ToDouble(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)result.ResultInfo["Sample"])["Members"])["SEMDataMsr"])["ScanFieldSize"]);
  563. string filePatht = result.FilePath + "\\FIELD_FILES\\";
  564. string imagePatht = filePatht + "Field" + subt.Split(',')[0].Split(':')[0].ToString() + ".bmp";
  565. //然后将取出的数据,转换成Bitmap对象
  566. Bitmap tempbit = Particledata.ReadImageFile(imagePatht);
  567. int pixw = tempbit.Width;
  568. double xs = pixw / ScanFieldSize;
  569. particleM = particleM + "," + subt;
  570. Bitmap bmap = Particledata.GetBitmapForBig(subt, xs, result.FilePath);
  571. if (bmap != null)
  572. {
  573. string[] str = subt.Split(',');
  574. bmap.Tag = new List<string>() { str[0].Split(':')[0], str[0].Split(':')[1], particles.Rows[i]["TypeId"].ToString() };
  575. dgV_ParticlesDevidePage.Rows[add_rowindex].Height = 150;
  576. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = bmap;
  577. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Style.BackColor = Color.Azure;
  578. dgV_ParticlesDevidePage.Rows[add_rowindex].DefaultCellStyle.ForeColor = Color.Chocolate;
  579. }
  580. else
  581. {
  582. dgV_ParticlesDevidePage.Rows[add_rowindex].Height = 150;
  583. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Style.BackColor = Color.Azure;
  584. }
  585. }
  586. else
  587. {
  588. if (FieldImage.Key != particles.Rows[i]["fieldid"].ToString() || FieldImage.Value == null)
  589. {
  590. string imagePath = filePath + "Field" + particles.Rows[i]["fieldid"].ToString() + ".bmp";
  591. FieldImage = new KeyValuePair<string, Bitmap>(particles.Rows[i]["fieldid"].ToString(), Particledata.ReadImageFile(imagePath));
  592. }
  593. Rectangle rectangle = new Rectangle() { X = Convert.ToInt32(particles.Rows[i]["RectLeft"]), Y = Convert.ToInt32(particles.Rows[i]["RectTop"]), Width = Convert.ToInt32(particles.Rows[i]["RectWidth"]), Height = Convert.ToInt32(particles.Rows[i]["RectHeight"]) };
  594. Bitmap bmap = Particledata.GetBitmapByParticle(FieldImage.Value, rectangle);
  595. bmap.Tag = new List<string>() { particles.Rows[i]["FieldId"].ToString(), particles.Rows[i]["ParticleId"].ToString(), particles.Rows[i]["TypeId"].ToString() };
  596. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = bmap;
  597. dgV_ParticlesDevidePage.Rows[add_rowindex].Height = bmap.Height + 20;
  598. }
  599. }
  600. if (enl.Current.Key == "DiameterRatio")
  601. {
  602. double d = Convert.ToDouble(particles.Rows[i]["DMAX"]) / Convert.ToDouble(particles.Rows[i]["DMIN"]);
  603. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = Math.Round(d, 2);
  604. }
  605. if (enl.Current.Key == "ASPECT_ELONG")
  606. {
  607. double d = Convert.ToDouble(particles.Rows[i]["DELONG"]) / Convert.ToDouble(particles.Rows[i]["DMEAN"]);
  608. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = Math.Round(d, 2);
  609. }
  610. if (particles.Columns.Contains(enl.Current.Key))
  611. {
  612. double num = 0;
  613. if (double.TryParse(particles.Rows[i][enl.Current.Key].ToString(), out num))
  614. {
  615. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = Math.Round(num, 2);
  616. }
  617. else if (enl.Current.Key == "Element")
  618. {
  619. List<string> elementtemp = new List<string>(ElementTypeSort);
  620. string[] strcbo = particles.Rows[i][enl.Current.Key].ToString().Split(';');
  621. for (int j = 0; j < strcbo.Length; j++)
  622. {
  623. string[] str = strcbo[j].Split('-');
  624. if (ElementTypeSort.Contains(str[0]))
  625. { dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[str[0].ToString()].Value = Math.Round(double.Parse(str[1]), 2).ToString(); }
  626. elementtemp.Remove(str[0].ToString());
  627. }
  628. foreach (var ele in elementtemp)
  629. {
  630. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[ele].Value = "0";
  631. }
  632. }
  633. else
  634. {
  635. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = particles.Rows[i][enl.Current.Key];
  636. }
  637. }
  638. if (enl.Current.Key == "TypeName")
  639. {
  640. if (particles.Rows[i]["TypeId"].ToString() == "9")
  641. {
  642. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = "Not Identified";
  643. }
  644. }
  645. if (enl.Current.Key == "Equivalent")
  646. {
  647. double dSize = Convert.ToDouble(particles.Rows[i]["Area"]);
  648. double Diameter = Math.Sqrt(dSize / Math.PI) * 2;
  649. dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = Math.Round(Diameter, 2);
  650. }
  651. }
  652. }
  653. }
  654. #region 加载进度条进度部份结束
  655. //加载完成设置鼠标为默认
  656. this.Cursor = Cursors.Default;
  657. string str8 = table["str8"].ToString();
  658. m_frm_userprogress.SetProgressValueAndText(100, str8);
  659. //加载完成,关闭进度条
  660. m_frm_userprogress.Close();
  661. #endregion
  662. dgV_ParticlesDevidePage.Visible = true;
  663. }
  664. /// <summary>
  665. /// 设置DataGridView样式
  666. /// </summary>
  667. private void SetDataGridViewStyle()
  668. {
  669. //用户不能调整标题的高度
  670. dgV_ParticlesDevidePage.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  671. //用户不能调整 行高
  672. dgV_ParticlesDevidePage.AllowUserToResizeRows = false;
  673. //点击选择整行
  674. dgV_ParticlesDevidePage.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  675. //居中显示
  676. System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
  677. dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
  678. dgV_ParticlesDevidePage.DefaultCellStyle = dataGridViewCellStyle1;
  679. dgV_ParticlesDevidePage.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
  680. //再次重覆禁用拖动表头高度,居然有效果了
  681. dgV_ParticlesDevidePage.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  682. //设置grid可以复制
  683. dgV_ParticlesDevidePage.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
  684. //设置每列的宽度
  685. dgV_ParticlesDevidePage.Columns[0].Width = 40;//第一列序号的宽度设置一下吧,要不太丑
  686. dgV_ParticlesDevidePage.Columns[1].Width = 150;
  687. //dgV_ParticlesDevidePage.Columns[dgV_ParticlesDevidePage.Columns.Count - 1].Width = 450;
  688. //dgV_ParticlesDevidePage.Columns[dgV_ParticlesDevidePage.Columns.Count - 1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
  689. //设置序号列不排序
  690. dgV_ParticlesDevidePage.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
  691. //设置序号列不可以设置宽度
  692. dgV_ParticlesDevidePage.Columns[0].Resizable = DataGridViewTriState.False;
  693. dgV_ParticlesDevidePage.RowsDefaultCellStyle.BackColor = Color.Azure;
  694. }
  695. #endregion
  696. #region 相关事件
  697. private void ToolStripMenuItem1_Click(object sender, EventArgs e)
  698. {
  699. //复制整个表
  700. CopyAll();
  701. }
  702. private void ToolStripMenuItem2_Click(object sender, EventArgs e)
  703. {
  704. //复制选择区域
  705. CopySelected();
  706. }
  707. /// <summary>
  708. /// 复制选择区域
  709. /// </summary>
  710. public void CopySelected()
  711. {
  712. //复制选择区域
  713. object oo = dgV_ParticlesDevidePage.GetClipboardContent();
  714. if (oo != null)
  715. Clipboard.SetDataObject(dgV_ParticlesDevidePage.GetClipboardContent());
  716. }
  717. /// <summary>
  718. /// 复制所有
  719. /// </summary>
  720. public void CopyAll()
  721. {
  722. dgV_ParticlesDevidePage.SelectAll();
  723. Clipboard.SetDataObject(dgV_ParticlesDevidePage.GetClipboardContent());
  724. }
  725. /// <summary>
  726. /// 以图像的方式将GridView进行截图
  727. /// </summary>
  728. public void CopyImage()
  729. {
  730. int height, width;
  731. width = dgV_ParticlesDevidePage.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + 2;
  732. height = this.Height;
  733. Bitmap image = new Bitmap(width, height);
  734. dgV_ParticlesDevidePage.DrawToBitmap(image, new Rectangle(0, 0, width, height));
  735. Clipboard.SetImage(image);
  736. }
  737. private void toolStripMenuItem4_Click(object sender, EventArgs e)
  738. {
  739. //对gridview进行截图
  740. CopyImage();
  741. }
  742. private void dgV_ParticlesDevidePage_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
  743. {
  744. //排序中
  745. if (this.dgV_ParticlesDevidePage.Rows[e.RowIndex1].Tag != null && this.dgV_ParticlesDevidePage.Rows[e.RowIndex1].Tag.ToString() == "统计行")
  746. {
  747. //ROW1>ROW2
  748. e.SortResult = 1;
  749. if (this.dgV_ParticlesDevidePage.SortOrder == SortOrder.Descending)
  750. e.SortResult = -1;
  751. e.Handled = true;
  752. return;
  753. }
  754. if (this.dgV_ParticlesDevidePage.Rows[e.RowIndex2].Tag != null && this.dgV_ParticlesDevidePage.Rows[e.RowIndex2].Tag.ToString() == "统计行")
  755. {
  756. //ROW1<ROW2
  757. e.SortResult = -1;
  758. if (this.dgV_ParticlesDevidePage.SortOrder == SortOrder.Descending)
  759. e.SortResult = 1;
  760. e.Handled = true;
  761. return;
  762. }
  763. }
  764. /// <summary>
  765. /// 将OTS坐标转换为Sem 坐标
  766. /// </summary>
  767. /// <param name="POTSCoord"></param>
  768. /// <returns></returns>
  769. public Point ChangeOTSToSemCoord(Point POTSCoord)
  770. {
  771. //first if m_semstagedata is null to get stage inforation
  772. Convert.ToDouble(((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["scanFieldSize"]);
  773. //after obtaining stage info,calc stage point data
  774. Point ret_SEM_point = new Point();
  775. // get center point, um
  776. long xStart = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["Members"])["XAxis"])["start"]);
  777. long xEnd = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["Members"])["XAxis"])["end"]);
  778. long xCenter = (xStart + xEnd) / 2;
  779. long yStart = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["Members"])["YAxis"])["start"]);
  780. long yEnd = Convert.ToInt64(((Dictionary<string, object>)((Dictionary<string, object>)((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["Members"])["YAxis"])["end"]);
  781. long yCenter = (yStart + yEnd) / 2;
  782. // delte = SEM - OTSa
  783. long deltex = xCenter - 0;
  784. long deltey = yCenter - 0;
  785. int xdir = Convert.ToInt32(((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["xAxisDir"]);
  786. int ydir = Convert.ToInt32(((Dictionary<string, object>)result.ResultInfo["SEMStageData"])["yAxisDir"]);
  787. if (xdir == (int)OTS_X_AXIS_DIRECTION.LEFT_TOWARD)
  788. {
  789. ret_SEM_point.X = -1 * (POTSCoord.X - Convert.ToInt32(deltex));
  790. }
  791. else if (xdir == (int)OTS_X_AXIS_DIRECTION.RIGHT_TOWARD)
  792. {
  793. ret_SEM_point.X = POTSCoord.X + Convert.ToInt32(deltex);
  794. }
  795. if (ydir == (int)OTS_Y_AXIS_DIRECTION.UP_TOWARD)
  796. {
  797. ret_SEM_point.Y = POTSCoord.Y + Convert.ToInt32(deltey);
  798. }
  799. else if (ydir == (int)OTS_Y_AXIS_DIRECTION.DOWN_TOWARD)
  800. {
  801. ret_SEM_point.Y = -1 * (POTSCoord.Y - Convert.ToInt32(deltey));
  802. }
  803. return ret_SEM_point;
  804. }
  805. #endregion
  806. #region 获取向导出模块提供的DataTable和GridView对象
  807. /// <summary>
  808. /// 获取到该模块输出后形成的DataTable,和GridView
  809. /// </summary>
  810. /// <param name="out_dt"></param>
  811. /// <param name="out_dg"></param>
  812. public void GetDataTableAndGridView(out DataTable out_dt, out DataGridView out_dg)
  813. {
  814. out_dt = m_dt;
  815. out_dg = this.dgV_ParticlesDevidePage;//同样通过 dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[2].Value 来获取bitmap对象
  816. }
  817. #endregion
  818. #region 连接电镜相关
  819. /// <summary>
  820. /// 移动SEM到指定位置线程函数
  821. /// </summary>
  822. private void Thread_GO(object in_obj)
  823. {
  824. if (m_mythread_state == false)
  825. {
  826. m_mythread_state = true;
  827. Point sem_point = (Point)in_obj;
  828. //第一步,连接电镜
  829. m_OTSIncAReportGridsFun.Connection_ForParticlesGrid();
  830. Thread.Sleep(500);
  831. //第二步,移动到指定位置,先读取再设置
  832. if (m_OTSIncAReportGridsFun.m_SEMConnectionState == true)
  833. {
  834. m_OTSIncAReportGridsFun.MoveSemToPointXY_ForParticlesGrid(sem_point.X, sem_point.Y);
  835. }
  836. Thread.Sleep(1500);
  837. //第三步,断开电镜连接
  838. m_OTSIncAReportGridsFun.DisConnectSEM_ForParticlesGrid();
  839. //Thread.Sleep(500);
  840. m_mythread_state = false;
  841. }
  842. }
  843. #endregion
  844. private void ToolStripMenuItem_exportzoomimagefile_Click(object sender, EventArgs e)
  845. {
  846. //导出放大图像
  847. FolderBrowserDialog fbd = new FolderBrowserDialog();
  848. if (fbd.ShowDialog() == DialogResult.OK)
  849. {
  850. m_imagefilepath = fbd.SelectedPath;
  851. for (int i = 0; i < m_dt.Rows.Count; i++)
  852. {
  853. Bitmap ls_bp = new Bitmap(1, 1);
  854. ls_bp = (Bitmap)m_dt.Rows[i][1];
  855. string strPath = m_imagefilepath + "\\" + (i + 1).ToString() + ".jpg";
  856. string strPath2 = m_imagefilepath + "\\" + (i + 1).ToString() + "s.jpg";
  857. ls_bp.Save(strPath);
  858. //m_COTSReportProjFileMgrClr.ReZoom(strPath, strPath2);
  859. System.IO.File.Delete(strPath);//再将文件删除
  860. }
  861. string str1 = table["str9"].ToString();
  862. MessageBox.Show(str1 + m_dt.Rows.Count.ToString());
  863. }
  864. }
  865. /// <summary>
  866. /// 导出二次放大图像
  867. /// </summary>
  868. /// <param name="str_srcPathName"></param>
  869. /// <param name="str_outPathName"></param>
  870. public void ParticleReZoom(string str_srcPathName, string str_outPathName)
  871. {
  872. //m_COTSReportProjFileMgrClr.ReZoom(str_srcPathName, str_outPathName);
  873. }
  874. private void ToolStripMenuItem3_Click(object sender, EventArgs e)
  875. {
  876. //恢复至初始状态
  877. dgV_ParticlesDevidePage.Rows.Clear();
  878. dgV_ParticlesDevidePage.Columns.Clear();
  879. BindDataGridView();
  880. SetDataGridViewStyle();
  881. }
  882. private void ToolStripMenuItem5_Click(object sender, EventArgs e)
  883. {
  884. /*注意:多数据源情况下,只获取第一个数据源的选择了多行的情况下,只获取最后一个选择行的,没有办法,这是为了符合李阳博士提出的要求*/
  885. //先获取鼠标所选择的行里的颗粒的FieldID和ParticleID
  886. string i_ls_fieldid = "";
  887. string i_ls_particleid = "";
  888. int cou = dgV_ParticlesDevidePage.SelectedRows.Count;
  889. if (cou > 0)
  890. {
  891. DataGridViewRow dgvr = dgV_ParticlesDevidePage.SelectedRows[cou - 1];
  892. Bitmap ls_bp = (Bitmap)dgvr.Cells[2].Value;
  893. List<string> list_str = (List<string>)ls_bp.Tag;
  894. if (list_str != null)
  895. {
  896. i_ls_fieldid = list_str[0];
  897. i_ls_particleid = list_str[1];
  898. }
  899. }
  900. ParticleData particleData = new ParticleData(result.FilePath);
  901. Particle particle = particleData.GetParticleByFidAndPid(i_ls_fieldid, i_ls_particleid);
  902. ////然后遍历所有的Field
  903. //Point ls_ots_point = new Point() { X = particle.PosX, Y = particle.PosY };
  904. ////将该坐标转成SEM坐标
  905. //Point ls_sem_point = ChangeOTSToSemCoord(ls_ots_point);
  906. //取得Field的Position,然后执行线程移动SEM到当前Field的位置
  907. if (m_mythread == null)
  908. {
  909. return;
  910. }
  911. if (m_mythread.ThreadState == ThreadState.Running || m_mythread.ThreadState == ThreadState.WaitSleepJoin)
  912. {
  913. return;
  914. }
  915. if (m_mythread.ThreadState == ThreadState.Stopped)
  916. {
  917. m_mythread = new Thread(new ParameterizedThreadStart(Thread_GO));
  918. }
  919. //改为线程调用,先判断线程状态
  920. m_mythread.Start(new Point(particle.SEMPosX, particle.SEMPosY));
  921. }
  922. private void ToolStripMenuItem_selectparticle_Click(object sender, EventArgs e)
  923. {
  924. string path = result.FilePath;
  925. ParticleData fielddata = new ParticleData(result.FilePath);
  926. List<Particle> particles = fielddata.GetParticleAllList();
  927. List<Particle> ls_list_cotsparticleclr = new List<Particle>();
  928. for (int l = 0; l < dgV_ParticlesDevidePage.SelectedRows.Count; l++)
  929. {
  930. //从事先加载的Bitmap对象的Tag中取出List<string>
  931. DataGridViewRow dgvr = dgV_ParticlesDevidePage.SelectedRows[l];
  932. Bitmap ls_bp = (Bitmap)dgvr.Cells[2].Value;
  933. List<string> list_str = (List<string>)ls_bp.Tag;
  934. if (list_str != null)
  935. {
  936. int i_ls_fieldid = Convert.ToInt32(list_str[0]);
  937. int i_ls_particleid = Convert.ToInt32(list_str[1]);
  938. foreach (Particle item in particles)
  939. {
  940. if (i_ls_fieldid == item.FieldId && i_ls_particleid == item.ParticleId)
  941. {
  942. ls_list_cotsparticleclr.Add(item);
  943. break;
  944. }
  945. }
  946. }
  947. }
  948. m_ReportApp.SetSelectedParticles(ls_list_cotsparticleclr);
  949. string str1 = table["str1"].ToString();
  950. //然后循环得出,list cotsparticleclr列表,返回给框架
  951. //m_sec.SendCall(str1, "", "", ls_list_cotsparticleclr);
  952. string str2 = table["str2"].ToString();
  953. string str3 = table["str3"].ToString();
  954. MessageBox.Show(str2 + ls_list_cotsparticleclr.Count.ToString() + str3);
  955. }
  956. private void ToolStripMenuItem_exportimagefile_Click(object sender, EventArgs e)
  957. {
  958. FolderBrowserDialog fbd = new FolderBrowserDialog();
  959. if (fbd.ShowDialog() == DialogResult.OK)
  960. {
  961. m_imagefilepath = fbd.SelectedPath;
  962. for (int i = 0; i < dgV_ParticlesDevidePage.Rows.Count; i++)
  963. {
  964. Bitmap ls_bp = new Bitmap(1, 1);
  965. ls_bp = (Bitmap)dgV_ParticlesDevidePage.Rows[i].Cells[2].Value;
  966. ls_bp.Save(m_imagefilepath + "\\" + (i + 1).ToString() + ".jpg");
  967. }
  968. string str9 = table["str9"].ToString();
  969. MessageBox.Show(str9 + dgV_ParticlesDevidePage.Rows.Count.ToString());
  970. }
  971. }
  972. private void testToolStripMenuItem_Click(object sender, EventArgs e)
  973. {
  974. ////测试导出指定颗粒的能谱图,测试相关代码
  975. //OTSIncAReportGraph.DParticle ls_dparticle = new OTSIncAReportGraph.DParticle();
  976. //for (int i = 0; i < dgV_ParticlesDevidePage.SelectedRows.Count; i++)
  977. //{
  978. // //获取选择行颗粒的fieldid,和particleid
  979. // string str_fieldid = "";
  980. // string str_particleid = "";
  981. // string str_stdtypeid = "";
  982. // DataGridViewRow dgvr = dgV_ParticlesDevidePage.SelectedRows[i];
  983. // Bitmap ls_bp = (Bitmap)dgvr.Cells[2].Value;
  984. // List<string> list_str = (List<string>)ls_bp.Tag;
  985. // if (list_str != null)
  986. // {
  987. // str_fieldid = list_str[0];
  988. // str_particleid = list_str[1];
  989. // str_stdtypeid = list_str[2];
  990. // }
  991. // ls_dparticle.CLRFieldID = Convert.ToInt32(str_fieldid);
  992. // ls_dparticle.CLRTagID = Convert.ToInt32(str_particleid);
  993. // ls_dparticle.STDTypeID = Convert.ToInt32(str_stdtypeid);
  994. //}
  995. ////显示xray能谱
  996. //ShowXRay(ls_dparticle);
  997. //Bitmap ls_bpex = new Bitmap(control_XRayTable1.Width, control_XRayTable1.Height);
  998. //control_XRayTable1.DrawToBitmap(ls_bpex, new Rectangle(0, 0, control_XRayTable1.Width, control_XRayTable1.Height));
  999. ////Bitmap ls_bpex = control_XRayTable1.ExportXRayImage();
  1000. //ls_bpex.Save("e:\\test222.jpg");
  1001. }
  1002. /// <summary>
  1003. /// 判断条件输入 前<后
  1004. /// </summary>
  1005. /// <param name="min"></param>
  1006. /// <param name="max"></param>
  1007. /// <returns></returns>
  1008. bool CompareInput(string min,string max)
  1009. {
  1010. int imax = 0;
  1011. int imin = 0;
  1012. double dmax = 0;
  1013. double dmin = 0;
  1014. if (int.TryParse(max, out imax))
  1015. {
  1016. dmax = (double)imax;
  1017. }
  1018. else
  1019. {
  1020. dmax = Convert.ToDouble(max);
  1021. }
  1022. if (int.TryParse(min, out imin))
  1023. {
  1024. dmin = (double)imin;
  1025. }
  1026. else
  1027. {
  1028. dmin = Convert.ToDouble(min);
  1029. }
  1030. if(dmin<=dmax)
  1031. {
  1032. return true;
  1033. }
  1034. else
  1035. {
  1036. return false;
  1037. }
  1038. }
  1039. private void btn_Sel_Click(object sender, EventArgs e)
  1040. {
  1041. condition = "";
  1042. if (!string.IsNullOrWhiteSpace(tBx_AreaMin.Text))
  1043. {
  1044. double dnum = 0;
  1045. int inum = 0;
  1046. if (double.TryParse(tBx_AreaMin.Text, out dnum) && dnum >= 0)
  1047. {
  1048. condition += " and Area" + " > " + tBx_AreaMin.Text;
  1049. }
  1050. else if(int.TryParse(tBx_AreaMin.Text, out inum) && inum >= 0)
  1051. {
  1052. condition += " and Area" + " > " + tBx_AreaMin.Text;
  1053. }
  1054. else
  1055. {
  1056. MessageBox.Show(table["str10"].ToString());
  1057. return;
  1058. }
  1059. }
  1060. if (!string.IsNullOrWhiteSpace(Tbx_AreaMax.Text))
  1061. {
  1062. double dnum = 0;
  1063. int inum = 0;
  1064. if (double.TryParse(Tbx_AreaMax.Text, out dnum) && dnum >= 0)
  1065. {
  1066. condition += " and Area" + "<" + Tbx_AreaMax.Text;
  1067. }
  1068. else if (int.TryParse(Tbx_AreaMax.Text, out inum) && inum >= 0)
  1069. {
  1070. condition += " and Area" + "<" + Tbx_AreaMax.Text;
  1071. }
  1072. else
  1073. {
  1074. MessageBox.Show(table["str10"].ToString());
  1075. return;
  1076. }
  1077. }
  1078. if (!string.IsNullOrWhiteSpace(tBx_AreaMin.Text) && !string.IsNullOrWhiteSpace(Tbx_AreaMax.Text))
  1079. {
  1080. if (!CompareInput(tBx_AreaMin.Text, Tbx_AreaMax.Text))
  1081. {
  1082. MessageBox.Show(table["str11"].ToString());
  1083. return;
  1084. }
  1085. }
  1086. if (!string.IsNullOrWhiteSpace(tbx_DmaxMin.Text))
  1087. {
  1088. double dnum = 0;
  1089. int inum = 0;
  1090. if (double.TryParse(tbx_DmaxMin.Text, out dnum) && dnum >= 0)
  1091. {
  1092. condition += " and DMAX" + " > " + tbx_DmaxMin.Text;
  1093. }
  1094. else if(int.TryParse(tbx_DmaxMin.Text, out inum)&& inum >= 0)
  1095. {
  1096. condition += " and DMAX" + " > " + tbx_DmaxMin.Text;
  1097. }
  1098. else
  1099. {
  1100. MessageBox.Show(table["str10"].ToString());
  1101. return;
  1102. }
  1103. }
  1104. if (!string.IsNullOrWhiteSpace(tbx_DmaxMax.Text))
  1105. {
  1106. double dnum = 0;
  1107. int inum = 0;
  1108. if (double.TryParse(tbx_DmaxMax.Text, out dnum) && dnum > 0)
  1109. {
  1110. condition += " and DMAX" + "<" + tbx_DmaxMax.Text;
  1111. }
  1112. else if(int.TryParse(tbx_DmaxMax.Text, out inum)&& inum >= 0)
  1113. {
  1114. condition += " and DMAX" + "<" + tbx_DmaxMax.Text;
  1115. }
  1116. else
  1117. {
  1118. MessageBox.Show(table["str10"].ToString());
  1119. return;
  1120. }
  1121. }
  1122. if (!string.IsNullOrWhiteSpace(tbx_DmaxMin.Text) && !string.IsNullOrWhiteSpace(tbx_DmaxMax.Text))
  1123. {
  1124. if (!CompareInput(tbx_DmaxMin.Text, tbx_DmaxMax.Text))
  1125. {
  1126. MessageBox.Show(table["str11"].ToString());
  1127. return;
  1128. }
  1129. }
  1130. if (!string.IsNullOrWhiteSpace(tbx_Type.Text))
  1131. {
  1132. condition += " and TypeName Like \"%" + tbx_Type.Text + "%\" ";
  1133. }
  1134. if (m_frm_userprogress.IsDisposed)
  1135. {
  1136. ProgressStart();
  1137. }
  1138. UpdateTable();
  1139. lnkFirst_Click(null, null);
  1140. SetDataGridViewStyle();
  1141. }
  1142. void ProgressStart()
  1143. {
  1144. #region 加载显示进度条部份
  1145. this.Cursor = Cursors.WaitCursor;
  1146. m_frm_userprogress = new Frm_UserProgress();
  1147. //显示进度条,计算进度条应该显示的位置和宽度
  1148. Form ls_main_form = this.ParentForm.ParentForm;//取出父窗体
  1149. if (ls_main_form == null)
  1150. {
  1151. m_frm_userprogress.Visible = false;
  1152. }
  1153. else
  1154. {
  1155. m_frm_userprogress.Width = (int)(MyPrimaryScreen.DESKTOP.Width / MyPrimaryScreen.ScaleX * 0.9);
  1156. m_frm_userprogress.Location = new Point(ls_main_form.Location.X + 200, ls_main_form.Location.Y + 200 + (int)(MyPrimaryScreen.DESKTOP.Height / MyPrimaryScreen.ScaleX) / 2);
  1157. m_frm_userprogress.Show();
  1158. m_frm_userprogress.SetProgressValueAndText(0, "Loading data of particles...");
  1159. }
  1160. #endregion
  1161. }
  1162. private void cbB_order_SelectedIndexChanged(object sender, EventArgs e)
  1163. {
  1164. //int ordernum = cbB_order.SelectedIndex;
  1165. //if (ordernum == 0)
  1166. //{
  1167. // OrderFunction = "fieldid,particleid";
  1168. //}
  1169. //else if (ordernum == 1)
  1170. //{
  1171. // OrderFunction = "TypeName";
  1172. //}
  1173. //else if (ordernum == 2)
  1174. //{
  1175. // OrderFunction = "DMAX";
  1176. //}
  1177. //else if (ordernum == 3)
  1178. //{
  1179. // OrderFunction = "DMAX desc";
  1180. //}
  1181. //else if (ordernum == 4)
  1182. //{
  1183. // OrderFunction = "Area";
  1184. //}
  1185. //else if (ordernum == 5)
  1186. //{
  1187. // OrderFunction = "Area desc";
  1188. //}
  1189. lnkFirst_Click(null, null);
  1190. SetDataGridViewStyle();
  1191. }
  1192. /// <summary>
  1193. /// 页面控件呈现
  1194. /// </summary>
  1195. private void DrawControl(bool callEvent)
  1196. {
  1197. BindDataGridView();
  1198. lblTotalCount.Text = RecordCount.ToString();
  1199. lblPageCount.Text = PageCount.ToString();
  1200. txtPageNum.Text = PageIndex.ToString();
  1201. SetFormCtrEnabled();
  1202. if (PageCount == 0 || PageCount == 1)//有且仅有一页
  1203. {
  1204. lnkFirst.Enabled = false;
  1205. lnkPrev.Enabled = false;
  1206. lnkNext.Enabled = false;
  1207. lnkLast.Enabled = false;
  1208. btnGo.Enabled = false;
  1209. }
  1210. else if (PageIndex == 1)//第一页
  1211. {
  1212. lnkFirst.Enabled = false;
  1213. lnkPrev.Enabled = false;
  1214. }
  1215. else if (PageIndex == PageCount)//最后一页
  1216. {
  1217. lnkNext.Enabled = false;
  1218. lnkLast.Enabled = false;
  1219. }
  1220. }
  1221. private void SetFormCtrEnabled()
  1222. {
  1223. lnkFirst.Enabled = true;
  1224. lnkPrev.Enabled = true;
  1225. lnkNext.Enabled = true;
  1226. lnkLast.Enabled = true;
  1227. btnGo.Enabled = true;
  1228. }
  1229. /// <summary>
  1230. /// 分页属性改变了。
  1231. /// </summary>
  1232. private void txtPageSize_TextChanged(object sender, EventArgs e)
  1233. {
  1234. int num = 0;
  1235. if (int.TryParse(txtPageNum.Text.Trim(), out num) && num > 0)
  1236. {
  1237. }
  1238. else { txtPageNum.Text = "1"; }
  1239. }
  1240. private void lnkFirst_Click(object sender, EventArgs e)
  1241. {
  1242. PageIndex = 1;
  1243. DrawControl(true);
  1244. }
  1245. private void lnkPrev_Click(object sender, EventArgs e)
  1246. {
  1247. PageIndex = Math.Max(1, PageIndex - 1);
  1248. DrawControl(true);
  1249. }
  1250. private void lnkNext_Click(object sender, EventArgs e)
  1251. {
  1252. PageIndex = Math.Min(PageCount, PageIndex + 1);
  1253. DrawControl(true);
  1254. }
  1255. private void lnkLast_Click(object sender, EventArgs e)
  1256. {
  1257. PageIndex = PageCount;
  1258. DrawControl(true);
  1259. }
  1260. /// <summary>
  1261. /// 跳转
  1262. /// </summary>
  1263. /// <param name="sender"></param>
  1264. /// <param name="e"></param>
  1265. private void btnGo_Click(object sender, EventArgs e)
  1266. {
  1267. int num = 0;
  1268. if (int.TryParse(txtPageNum.Text.Trim(), out num) && num > 0)
  1269. {
  1270. if (num > PageCount)
  1271. {
  1272. num = PageCount;
  1273. txtPageNum.Text = PageCount.ToString();
  1274. }
  1275. PageIndex = num;
  1276. DrawControl(true);
  1277. }
  1278. }
  1279. private void cbB_PageSize_SelectedIndexChanged(object sender, EventArgs e)
  1280. {
  1281. if (cbB_PageSize.SelectedItem.ToString() == "All")
  1282. {
  1283. pageSize = -1;
  1284. }
  1285. else
  1286. {
  1287. pageSize = int.Parse(cbB_PageSize.SelectedItem.ToString());
  1288. }
  1289. lnkFirst_Click(null, null);
  1290. SetDataGridViewStyle();
  1291. }
  1292. /// <summary>
  1293. /// 在帧图上标记颗粒的位置矩形,并保存到FIELD_FILES_MARK文件夹中
  1294. /// </summary>
  1295. public void SaveMarkParticleRectangleOnFieldFile(DataTable dataTable ,out List<string> vs , out DataTable dt_FIeld)
  1296. {
  1297. List<string> maxlength = new List<string>();
  1298. for (int i=0;i< dataTable.Rows.Count;i++)
  1299. {
  1300. if (dataTable.Rows[i]["TypeName"].ToString()!= "Not Identified")
  1301. {
  1302. if (maxlength.Count<10)
  1303. {
  1304. maxlength.Add(dataTable.Rows[i]["Fieldid"].ToString());
  1305. }
  1306. else
  1307. {
  1308. break;
  1309. }
  1310. }
  1311. }
  1312. List<string> max_list = maxlength.Distinct().ToList();
  1313. string str_path_FIELD_FILES = result.FilePath + "\\FIELD_FILES\\";
  1314. string str_path_FIELD_FILES_MARK = result.FilePath + "\\FIELD_FILES_MARK\\";
  1315. int intQuantity = 0;
  1316. //List<string> vs = new List<string>();
  1317. dt_FIeld = new DataTable();
  1318. vs = new List<string>();
  1319. ////判断是否已经有导出过的标记帧图文件夹,如果已经存在,则直接返回不输出,先不加
  1320. DirectoryInfo theFolder_FieldMark = new DirectoryInfo(str_path_FIELD_FILES_MARK);
  1321. if (!theFolder_FieldMark.Exists)
  1322. {
  1323. //创建
  1324. theFolder_FieldMark.Create();
  1325. }
  1326. else
  1327. {
  1328. theFolder_FieldMark.Delete(true);
  1329. //创建
  1330. theFolder_FieldMark.Create();
  1331. //已经存在的话,直接返回
  1332. //return;
  1333. }
  1334. //查找该field下对应所有的颗粒
  1335. DataTable dt_AllParticle = fieldData.GetAllParticle_DataTable();
  1336. DirectoryInfo theFolder = new DirectoryInfo(str_path_FIELD_FILES);
  1337. if (theFolder.Exists)
  1338. {
  1339. DataTable dt = new DataTable();
  1340. dt.Columns.Add("fieldid");
  1341. dt.Columns.Add("id");
  1342. dt.Columns.Add("fieldName");
  1343. dt.Columns.Add("cunt",typeof(int));
  1344. dt.Columns.Add("fieldFullName");
  1345. for (int i = 0; i < max_list.Count; i++)
  1346. {
  1347. foreach (FileInfo nextifile in theFolder.GetFiles())
  1348. {
  1349. if (nextifile.Name.Contains(".bmp") == true || nextifile.Name.Contains(".BMP") == true)
  1350. {
  1351. //确认对应的帧图名
  1352. string str_fieldid = Path.GetFileNameWithoutExtension(nextifile.Name);
  1353. str_fieldid = str_fieldid.Substring(5, str_fieldid.Length - 5);//减去field字符长度
  1354. bool bl = false;
  1355. if (str_fieldid == max_list[i].ToString())
  1356. {
  1357. bl = true;
  1358. }
  1359. if (bl)
  1360. {
  1361. DataRow dr = dt.NewRow();
  1362. dr["fieldid"] = " fieldid = " + str_fieldid;
  1363. dr["cunt"] = dataTable.Select(" fieldid = " + str_fieldid).Count();
  1364. dr["fieldName"] = nextifile.Name;
  1365. dr["fieldFullName"] = nextifile.FullName;
  1366. dr["id"] = str_fieldid;
  1367. dt.Rows.Add(dr);
  1368. }
  1369. }
  1370. }
  1371. }
  1372. //DataView dv = dt.DefaultView;
  1373. //dv.Sort = "cunt DESC";
  1374. dt_FIeld = dt.Copy();
  1375. for (int i=0;i< dt_FIeld.Rows.Count; i++)
  1376. {
  1377. if (dataTable.Select(dt_FIeld.Rows[i]["fieldid"].ToString()).Count() > 0)
  1378. {
  1379. vs.Add(dt_FIeld.Rows[i]["id"].ToString());
  1380. Image img = Image.FromFile(dt_FIeld.Rows[i]["fieldFullName"].ToString());
  1381. //img.Save(@"D:\1.bmp");
  1382. Bitmap ls_fieldbp = new Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  1383. using (Graphics g = Graphics.FromImage(ls_fieldbp))
  1384. {
  1385. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  1386. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  1387. g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  1388. g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height));
  1389. }
  1390. //ls_fieldbp.Save(@"D:\1.bmp");
  1391. img.Dispose();
  1392. img = null;
  1393. //循环将颗粒,进行标记到bitmap图上
  1394. DataRow[] drlist = dt_AllParticle.Select(dt_FIeld.Rows[i]["fieldid"].ToString());
  1395. if (drlist.Count() > 0)
  1396. intQuantity = intQuantity + 1;
  1397. for (int i_row = 0; i_row < drlist.Count(); i_row++)
  1398. {
  1399. //string str_fieldid = drlist[i_row]["fieldid"].ToString();
  1400. string str_particleid = drlist[i_row]["particleid"].ToString();
  1401. string str_stdtypeid = drlist[i_row]["typeid"].ToString();
  1402. string str_offset_rect_X = drlist[i_row]["RectLeft"].ToString();
  1403. string str_offset_rect_Y = drlist[i_row]["RectTop"].ToString();
  1404. string str_offset_rect_Width = drlist[i_row]["RectWidth"].ToString();
  1405. string str_offset_rect_Height = drlist[i_row]["RectHeight"].ToString();
  1406. //确定需要标记颗粒的定位
  1407. Rectangle offset_rect = new Rectangle(Convert.ToInt32(str_offset_rect_X), Convert.ToInt32(str_offset_rect_Y),
  1408. Convert.ToInt32(str_offset_rect_Width), Convert.ToInt32(str_offset_rect_Height));
  1409. //向帧图进行标记颗粒位置
  1410. Graphics g = Graphics.FromImage(ls_fieldbp);
  1411. g.DrawRectangle(new Pen(Color.Aquamarine), offset_rect);
  1412. //判断是否超出了图像外面,是的话,让文字在左侧显示,默认在右侧显示
  1413. int ls_offsetx = offset_rect.X + offset_rect.Width;
  1414. if (ls_offsetx > ls_fieldbp.Width - 10)
  1415. {
  1416. ls_offsetx = offset_rect.X - 10;
  1417. }
  1418. g.DrawString(dt_FIeld.Rows[i]["id"].ToString() + "" + str_particleid.ToString(),
  1419. new Font("黑体", 8), new SolidBrush(Color.Aqua), new PointF(ls_offsetx, offset_rect.Y));
  1420. //保存带有标记的帧图
  1421. ls_fieldbp.Save(str_path_FIELD_FILES_MARK + dt_FIeld.Rows[i]["id"].ToString() + ".bmp");
  1422. //ls_fieldbp.Dispose();
  1423. }
  1424. }
  1425. }
  1426. }
  1427. //
  1428. GC.Collect();
  1429. GC.WaitForPendingFinalizers();
  1430. }
  1431. private void testToolStripMenuItem1_Click(object sender, EventArgs e)
  1432. {
  1433. //SaveMarkParticleRectangleOnFieldFile();
  1434. MessageBox.Show("Export completed!");
  1435. }
  1436. private void EXCELToolStripMenuItem_Click(object sender, EventArgs e)
  1437. {
  1438. //将所有的数据导出到EXCEL中
  1439. SaveFileDialog sfd = new SaveFileDialog();
  1440. sfd.Filter = "Excel File(*.xls)|*.xls";
  1441. //设置默认文件类型显示顺序
  1442. sfd.FilterIndex = 1;
  1443. //保存对话框是否记忆上次打开的目录
  1444. sfd.RestoreDirectory = true;
  1445. if (sfd.ShowDialog() == DialogResult.OK)
  1446. {
  1447. IWorkbook workbook = new HSSFWorkbook(); //用于创建.xls office2003开始以前的
  1448. //IWorkbook workbook = new XSSFWorkbook(); //用于创建.xlsx office2007开始以后的
  1449. ISheet sheet;
  1450. //创建Excel文件
  1451. FileStream fs = File.Create(sfd.FileName);
  1452. fs.Close();
  1453. sheet = workbook.CreateSheet("Particles");//创建工作表
  1454. //创建表格边框样式风格
  1455. ICellStyle cellStyle = workbook.CreateCellStyle();
  1456. cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
  1457. cellStyle.VerticalAlignment = VerticalAlignment.Center;
  1458. //设置上4边
  1459. cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  1460. cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  1461. cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  1462. cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  1463. //设置字符大小和颜色
  1464. IFont font = workbook.CreateFont();
  1465. font.FontName = "黑体";
  1466. font.FontHeightInPoints = 10;
  1467. cellStyle.SetFont(font);
  1468. //设置颗粒名列宽
  1469. sheet.SetColumnWidth(1, 30 * 256);//夹杂物名列宽
  1470. sheet.SetColumnWidth(2, 3 * 256);//图像列宽
  1471. IRow row;
  1472. ICell cell;
  1473. //插入表头
  1474. row = sheet.CreateRow(1);//从第15行添加一行
  1475. for (int i_cell = 0; i_cell < dgV_ParticlesDevidePage.Columns.Count; i_cell++)
  1476. {
  1477. cell = row.CreateCell(i_cell);
  1478. cell.CellStyle = cellStyle;
  1479. cell.SetCellValue(dgV_ParticlesDevidePage.Columns[i_cell].Name);
  1480. }
  1481. //插入表内容
  1482. for (int i_row = 0; i_row < dgV_ParticlesDevidePage.Rows.Count; i_row++)
  1483. {
  1484. row = sheet.CreateRow(2 + i_row);
  1485. for (int i_cell = 0; i_cell < dgV_ParticlesDevidePage.Columns.Count; i_cell++)
  1486. {
  1487. cell = row.CreateCell(i_cell);
  1488. cell.CellStyle = cellStyle;
  1489. if (dgV_ParticlesDevidePage[i_cell, i_row].Value == null)
  1490. continue;
  1491. if (i_cell == 2)
  1492. {
  1493. //颗粒图像列
  1494. Bitmap bp = (Bitmap)dgV_ParticlesDevidePage.Rows[i_row].Cells[2].Value;
  1495. byte[] bytes = ImageConvertToBytes(bp);
  1496. //第二步,将图片添加到workbook中,指定图片的格式,返回图片所在workbook->paicture数组中的索引的地址,从1开始
  1497. int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG);
  1498. //第三步,在sheet中创建画布
  1499. IDrawing patriarch = sheet.CreateDrawingPatriarch();
  1500. //第四步,设置锚点,(在起始单元格的X坐标0-1023,Y的坐标0-255,在终止单元格的X坐标0-1023,Y的坐标0-255,起始单元格行数,列数,终止单元格行数,列数)
  1501. IClientAnchor anchor = patriarch.CreateAnchor(1, 1, 2, 2, i_cell, i_row + 2, i_cell + 1, i_row + 3);//终止比开始位置大1,会自动缩放到一个单元格内的
  1502. //第五步,创建图片
  1503. IPicture pict = patriarch.CreatePicture(anchor, pictureIdx);
  1504. //图像效果不好,自己另外导出吧,这里对该列宽进行了设置0
  1505. }
  1506. else
  1507. {
  1508. //非图像列
  1509. cell.SetCellValue(dgV_ParticlesDevidePage[i_cell, i_row].Value.ToString());
  1510. }
  1511. }
  1512. }
  1513. //完成后,对Excel进行保存
  1514. FileStream file = new FileStream(sfd.FileName, FileMode.Create);
  1515. workbook.Write(file);
  1516. file.Close();
  1517. MessageBox.Show("Export complete!");
  1518. //导出完成后,打开Excel文件
  1519. if (File.Exists(sfd.FileName))
  1520. {
  1521. //打开刚才导出的文件
  1522. System.Diagnostics.Process.Start(sfd.FileName);
  1523. }
  1524. }
  1525. }
  1526. /// <summary>
  1527. /// 将image转成bytes
  1528. /// </summary>
  1529. /// <param name="in_img"></param>
  1530. /// <returns></returns>
  1531. public byte[] ImageConvertToBytes(System.Drawing.Image in_img)
  1532. {
  1533. MemoryStream ms = new MemoryStream();
  1534. in_img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  1535. return ms.ToArray();
  1536. }
  1537. public bool CheckParamExist<T>(T Param)
  1538. {
  1539. if (Param == null)
  1540. {
  1541. return false;
  1542. }
  1543. else
  1544. {
  1545. return true;
  1546. }
  1547. }
  1548. }
  1549. }