ParticlesGridDevidePage.cs 69 KB

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