ParticlesGridDevidePage.cs 70 KB

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