ParticleData.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SQLite;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Linq;
  9. namespace OTSIncAReportApp.DataOperation.DataAccess
  10. {
  11. public class ParticleData
  12. {
  13. private SqlHelper dbHelper;
  14. public ParticleData(string path)
  15. {
  16. dbHelper = new SqlHelper("data source='" + path + "\\FIELD_FILES\\Inclusion.db'");
  17. }
  18. /// <summary>
  19. /// 获取SegmentList
  20. /// </summary>
  21. /// <param name="model">Feature</param>
  22. /// <returns></returns>
  23. public List<Model.Particle> GetParticleList(Model.Particle model)
  24. {
  25. //存放查询数据的数据表
  26. SQLiteParameter[] Parameter = new SQLiteParameter[1]
  27. {
  28. new SQLiteParameter("@FieldId", model.FieldId)
  29. };
  30. string sql = "select * from IncAData where Fieldid=@FieldId";
  31. DataTable DT = dbHelper.ExecuteDataTable(sql, Parameter);
  32. var result = new List<Model.Particle>();
  33. foreach (DataRow dr in DT.Rows)
  34. {
  35. Model.Particle item = new Model.Particle() { };
  36. result.Add(item);
  37. }
  38. return result;
  39. }
  40. /// <summary>
  41. /// 获取ParticleListBy
  42. /// </summary>
  43. /// <param name="model">Feature</param>
  44. /// <returns></returns>
  45. public List<Model.Particle> GetParticleListByCon(string con, string max, string min, int display)
  46. {
  47. //string sqlp = "select a.*,b.XrayData from IncAData a left join XRayData b on a.FieldId =b.FieldId and a.XrayId= b.XrayIndex";
  48. //string where = " where 1=1 ";
  49. string sqlp = "select a.* from IncAData a ";
  50. string where = " where 1=1 ";
  51. if (display == 1)
  52. {
  53. where = where + " and a.XrayId >-1 ";
  54. }
  55. if (con != "")
  56. {
  57. where = where + " and a." + con + ">" + min + " and a." + con + "<" + max;
  58. }
  59. sqlp = sqlp + where;
  60. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  61. List<Model.Particle> listp = dbHelper.TableToList<Model.Particle>(DT);
  62. return listp;
  63. }
  64. /// <summary>
  65. /// 获取ParticleList
  66. /// </summary>
  67. /// <param name="model">Feature</param>
  68. /// <returns></returns>
  69. public DataTable GetParticleListAndEm()
  70. {
  71. string sqlp = @"select *,
  72. (select group_concat(name||'-'||Percentage,';')
  73. from ElementChemistry where XRayId =INcAData.XRayId and fieldid=INcAData.fieldid ) as Element
  74. from INcAData where xrayid>-1";
  75. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  76. return DT;
  77. }
  78. /// <summary>
  79. /// 获取ParticleList
  80. /// </summary>
  81. /// <param name="model">Feature</param>
  82. /// <returns></returns>
  83. public DataTable GetMergedParticleInfo()
  84. {
  85. string sqlp = @"select *,
  86. (select group_concat(name||'-'||Percentage,';')
  87. from ElementChemistry where XRayId =MergedParticleInfo.XRayId and fieldid=MergedParticleInfo.fieldid ) as Element
  88. from MergedParticleInfo ";
  89. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  90. return DT;
  91. }
  92. /// <summary>
  93. /// 获取ParticleList
  94. /// </summary>
  95. /// <param name="model">Feature</param>
  96. /// <returns></returns>
  97. public DataTable GetParticleListByIncA(string con)
  98. {
  99. string sqlp = @"select TypeId,TypeName,TypeColor,count(1) as con,sum(Area) as Area,avg(" + con
  100. + ") as av,max(" + con
  101. + ") as max ";
  102. sqlp = sqlp + " from IncAData group by TypeId";
  103. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  104. return DT;
  105. }
  106. /// <summary>
  107. /// 获取ParticleList
  108. /// </summary>
  109. /// <param name="model">Feature</param>
  110. /// <returns></returns>
  111. public DataTable GetParticleListForParticlSize(string con, string fieldAndPartic)
  112. {
  113. string sqlp = @"select TypeId,TypeName,TypeColor,count(1) as con,avg(" + con
  114. + ") as av,max(" + con
  115. + ") as max ,GroupName";
  116. sqlp = sqlp + " from IncAData ";
  117. if (fieldAndPartic != "")
  118. {
  119. sqlp = sqlp + " where '" + fieldAndPartic + "' like ('%,'||fieldid||'-'||particleid||',%')";
  120. }
  121. sqlp = sqlp + " group by TypeId ";
  122. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  123. if (con == "area")
  124. {
  125. for (int i = 0; i < DT.Rows.Count; i++)
  126. {
  127. DT.Rows[i]["max"] = Math.Sqrt((double)DT.Rows[i]["max"] / Math.PI) * 2;
  128. }
  129. }
  130. return DT;
  131. }
  132. /// <summary>
  133. /// 获取element含量
  134. /// </summary>
  135. /// <returns></returns>
  136. public DataTable GetElementForArea(string fieldAndPartic)
  137. {
  138. string sqlp = @"select e.name,sum(e.percentage*p.area) as earea from ElementChemistry e
  139. inner join INcAData p on e.xrayid=p.xrayid and e.fieldid = p.fieldid";
  140. if (fieldAndPartic != "")
  141. {
  142. sqlp = sqlp + " where '" + fieldAndPartic + "' like ('%,'||p.fieldid||'-'||p.particleid||',%')";
  143. }
  144. sqlp = sqlp + " group by e.name order by sum(e.percentage*p.area) desc";
  145. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  146. return DT;
  147. }
  148. /// <summary>
  149. /// 获取element含量
  150. /// </summary>
  151. /// <returns></returns>
  152. public DataTable GetSmallElementForArea()
  153. {
  154. string sqlp = @"select e.name, sum(e.percentage*i.area) as earea from elementchemistry e inner join incadata i on e.xrayid = i.xrayid and e.fieldid = i.fieldid
  155. group by e.name order by sum(e.percentage*i.area) desc";
  156. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  157. return DT;
  158. }
  159. /// <summary>
  160. /// 获取所有Particle
  161. /// </summary>
  162. /// <returns></returns>
  163. public DataTable GetParticleAll(string fieldAndPartic)
  164. {
  165. string sqlp = @"select * from INcAData";
  166. if (fieldAndPartic != "")
  167. {
  168. sqlp = sqlp + " where '" + fieldAndPartic + "' like ('%,'||fieldid||'-'||particleid||',%')";
  169. }
  170. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  171. for (int i=0;i< DT.Rows.Count;i++)
  172. {
  173. DT.Rows[i]["Area"] = Math.Sqrt((double)DT.Rows[i]["Area"]/Math.PI) * 2;
  174. }
  175. return DT;
  176. }
  177. /// <summary>
  178. /// 获取所有Particle
  179. /// </summary>
  180. /// <returns></returns>
  181. public DataTable GetParticleHaveXray(string fieldAndPartic)
  182. {
  183. string sqlp = @"select *,
  184. (select group_concat(name||'-'||Percentage,';')
  185. from ElementChemistry where XRayId =INcAData.XRayId and fieldid=INcAData.fieldid ) as Element from INcAData where xrayid>-1 ";
  186. if (fieldAndPartic != "")
  187. {
  188. sqlp = sqlp + " and '" + fieldAndPartic + "' like ('%,'||fieldid||'-'||particleid||',%')";
  189. }
  190. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  191. for (int i = 0; i < DT.Rows.Count; i++)
  192. {
  193. DT.Rows[i]["Area"] = Math.Sqrt((double)DT.Rows[i]["Area"] / Math.PI) * 2;
  194. }
  195. return DT;
  196. }
  197. /// <summary>
  198. /// 获取所有Particle
  199. /// </summary>
  200. /// <returns></returns>
  201. public List<Model.Particle> GetParticleAllList()
  202. {
  203. string sqlp = @"select * from INcAData";
  204. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  205. List<Model.Particle> listp = dbHelper.TableToList<Model.Particle>(DT);
  206. return listp;
  207. }
  208. /// <summary>
  209. /// 获取所有分类的面积
  210. /// </summary>
  211. /// <param name="fieldAndPartic">选择颗粒</param>
  212. /// <returns></returns>
  213. public DataTable GetAreaByAllIncA(string fieldAndPartic)
  214. {
  215. string sqlp = @"select TypeId,TypeName,sum(area) as ar,count(1) as con ,GroupName from INcAData";
  216. if (fieldAndPartic != "")
  217. {
  218. sqlp = sqlp + " where '" + fieldAndPartic + "' like ('%,'||fieldid||'-'||particleid||',%')";
  219. }
  220. sqlp = sqlp + " group by TypeId";
  221. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  222. return DT;
  223. }
  224. /// <summary>
  225. /// 获取所有大颗粒没有的小颗粒分类
  226. /// </summary>
  227. /// <param name="fieldAndPartic">选择颗粒</param>
  228. /// <returns></returns>
  229. public DataTable GetSmallParticleInfo()
  230. {
  231. string sqlp = @"select TypeId,TypeName,TypeColor,sum(area) as area,sum(ParticleQuant) as ParticleQuant from SmallParticleInfo where TypeId not in(select TypeId from INcAData) group by TypeId";
  232. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  233. return DT;
  234. }
  235. /// <summary>
  236. /// 获取不同分类的面积
  237. /// </summary>
  238. /// <param name="fieldAndPartic">选择颗粒</param>
  239. /// <returns></returns>
  240. public DataTable GetAreaByIncA(string TypeId, string fieldAndPartic)
  241. {
  242. string sqlp = @"select e.name,sum(e.percentage*p.area) as pc,p.TypeId from ElementChemistry e
  243. inner join INcAData p on e.xrayid=p.xrayid and e.fieldid = p.fieldid where p.TypeId=" + TypeId + " ";
  244. if (fieldAndPartic != "")
  245. {
  246. sqlp = sqlp + " and '" + fieldAndPartic + "' like ('%,'||p.fieldid||'-'||p.particleid||',%')";
  247. }
  248. sqlp = sqlp + " group by e.name";
  249. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  250. return DT;
  251. }
  252. public DataTable GetAreaByIncA_All()
  253. {
  254. string sqlp = @"select e.name,sum(e.percentage*p.area) as pc,p.TypeId from ElementChemistry e
  255. inner join INcAData p on e.xrayid=p.xrayid and e.fieldid = p.fieldid group by e.name ";
  256. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  257. return DT;
  258. }
  259. /// <summary>
  260. /// 获取全部的物质大类
  261. /// </summary>
  262. /// <returns></returns>
  263. public DataTable GetAllClass()
  264. {
  265. string sqlp = @"select GroupName from IncAData group by GroupName order by count(1) desc";
  266. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  267. return DT;
  268. }
  269. /// <summary>
  270. /// 获取所有元素
  271. /// </summary>
  272. /// <param name="model">Feature</param>
  273. /// <returns></returns>
  274. public DataTable GetAllElement()
  275. {
  276. string sqlp = @"select name from ElementChemistry group by name order by count(1) desc";
  277. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  278. return DT;
  279. }
  280. /// <summary>
  281. /// 获取常用夹杂物分类信息
  282. /// </summary>
  283. /// <returns></returns>
  284. public DataTable GetCommonlyUsedClassifyData()
  285. {
  286. string sqlp = @"select
  287. (select count(typeid) from incadata where typeid BETWEEN 10100 and 10199 and typeid BETWEEN 12200 and 12299 and typeid BETWEEN 11300 and 11299 ) as SPINEL ,
  288. (select count(typeid) from incadata where typeid BETWEEN 10000 and 10999 ) as OXIDE ,
  289. (select count(typeid) from incadata where typeid BETWEEN 11200 and 11299 and typeid BETWEEN 11400 and 11499 and typeid BETWEEN 11200 and 11599 ) as SULFIDE_OXIDE ,
  290. (select count(typeid) from incadata where typeid BETWEEN 12000 and 12999 ) as NITRIDE ,
  291. (select count(typeid) from incadata where typeid BETWEEN 11000 and 11999 ) as SULFIDE ";
  292. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  293. return DT;
  294. }
  295. public bool DeleteFromData(string fieldid, string particleid)
  296. {
  297. string sqlp = @"delete from IncAData where FieldId="+ fieldid + " and ParticleId="
  298. + particleid;
  299. if (dbHelper.ExecuteQuery_bool(sqlp))
  300. {
  301. return true;
  302. }else
  303. {
  304. return false;
  305. }
  306. }
  307. /// <summary>
  308. /// 获取颗粒信息
  309. /// </summary>
  310. /// <param name="model">Feature</param>
  311. /// <returns></returns>
  312. public Model.Particle GetParticleByFidAndPid(string fieldid, string particleid)
  313. {
  314. string sqlp = @"select *,(select xraydata from xraydata where xrayindex=INcAData.xrayid and fieldid="
  315. + fieldid + ") as XRayData,(select group_concat(name || '-' || Percentage, ';')from ElementChemistry where XRayId = INcAData.XRayId and fieldid ="
  316. + fieldid + ") as Element from INcAData where fieldid="
  317. + fieldid + " and particleid= " + particleid;
  318. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  319. List<Model.Particle> listp = dbHelper.TableToList<Model.Particle>(DT);
  320. Model.Particle particle = new Model.Particle();
  321. if (listp.Count > 0)
  322. {
  323. particle = listp[0];
  324. List<Model.Element> ElementList = new List<Model.Element>();
  325. string element = DT.Rows[0]["Element"].ToString();
  326. for (int i = 0; i < element.Split(';').Count(); i++)
  327. {
  328. if (element.Split(';')[i] != "")
  329. {
  330. Model.Element ele = new Model.Element() { Name = element.Split(';')[i].Split('-')[0], Percentage = Convert.ToDouble(element.Split(';')[i].Split('-')[1]) };
  331. ElementList.Add(ele);
  332. }
  333. }
  334. particle.ElementList = ElementList;
  335. }
  336. return particle;
  337. }
  338. public Model.Particle GetParticleXrayDataByFidAndPid(string fieldid, string particleid)
  339. {
  340. string sqlp = @"select xraydata from xraydata where xrayindex="+particleid+" and fieldid="
  341. + fieldid ;
  342. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  343. List<Model.Particle> listp = dbHelper.TableToList<Model.Particle>(DT);
  344. if (listp.Count > 0)
  345. {
  346. return listp[0];
  347. }
  348. else
  349. {
  350. return null;
  351. }
  352. }
  353. public enum PARTCLE_TYPE
  354. {
  355. SMALL = 0,//过小颗粒
  356. OVERSIZE = 1,//过大颗粒
  357. AVE_GRAY_NOT_INRANRE = 2,//亮度不在分析范围内的颗粒
  358. SEARCH_X_RAY = 3,
  359. LOW_COUNT = 4,//低计数率颗粒
  360. NO_INTEREST_ELEMENTS = 5,
  361. NO_ANALYSIS_X_RAY = 6,//不含分析元素
  362. NOT_IDENTIFIED_SIC = 7,//非夹杂物颗粒SiC
  363. NOT_IDENTIFIED_FEO = 8,//非夹杂物颗粒FeO
  364. NOT_IDENTIFIED = 9,//未识别颗粒
  365. IDENTIFIED = 10,//分析颗粒,当为可识别类型时,可以被进一步识别为用户类型(1000以上),系统预定义类型(10000以上),所以最终颗粒类型不会为8,但可能为7
  366. USER_DEFINED_MIN = 1000,
  367. SYS_DEFINED_MIN = 10000
  368. }
  369. #region 分页添加读取数据库函数
  370. /// <summary>
  371. /// 获取分页查询所需信息
  372. /// </summary>
  373. /// <param name=""></param>
  374. /// <param name=""></param>
  375. /// <returns></returns>
  376. public DataTable GetInfoForPartucleDevidePage(int currentPage, int pagesize, string OrderFunction, string condition)
  377. {
  378. int p = (currentPage - 1) * pagesize;
  379. string sqliteString = "select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,SegmentNum,FieldPosX as 'SEMPosX',FieldPosY as 'SEMPosY',ParticleId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,SubParticles, (select group_concat(name || '-' || Percentage, ';') from ElementChemistry where XRayId = MergedParticleInfo.XRayId and fieldid = MergedParticleInfo.fieldid) as Element from MergedParticleInfo where 1=1 " + condition + " union select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,SegmentNum,FieldPosX,FieldPosY,ParticleId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,'' as SubParticles,(select group_concat(name || '-' || Percentage, ';') from ElementChemistry where XRayId = INcAData.XRayId and fieldid = INcAData.fieldid ) as Element from INcAData where xrayid > -1 and instr(','||(select ifnull(group_concat(SubParticles, ','),'') from MergedParticleInfo)|| ',',',' || fieldid || ':' || particleid || ',')= 0 " + condition + " order by " + OrderFunction + " limit " + pagesize.ToString() + " offset " + p.ToString();
  380. DataTable DT=new DataTable();
  381. DT = dbHelper.ExecuteQuery(sqliteString);
  382. return DT;
  383. }
  384. public DataTable GetIncaSurfaceData()
  385. {
  386. DataTable particlesAll = new DataTable();
  387. particlesAll = GetInfoForPartucleDevidePage2("");
  388. DataTable elementchemistry = GetElementChemistry();
  389. for (int i = 0; i < particlesAll.Rows.Count; i++)
  390. {
  391. string str = "XRayId = " + particlesAll.Rows[i]["particleId"].ToString() + " and fieldid = " + particlesAll.Rows[i]["fieldid"].ToString();
  392. DataRow[] drs = elementchemistry.Select(str);
  393. string ConcatenatedString = "";
  394. for (int j = 0; j < drs.Length; j++)
  395. {
  396. ConcatenatedString += drs[j]["name"] + "-" + drs[j]["Percentage"] + ';';
  397. }
  398. particlesAll.Rows[i]["Element"] = ConcatenatedString;
  399. }
  400. //string sqlliteString = "select * from INcAData";
  401. //DataTable dt = dbHelper.ExecuteQuery(sqlliteString);
  402. return particlesAll;
  403. }
  404. public DataTable GetInfoForPartucleDevidePage2(string condition)
  405. {
  406. string sqliteString = "select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,SegmentNum,FieldPosX as 'SEMPosX',FieldPosY as 'SEMPosY',XrayId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,SubParticles, (select group_concat(name || '-' || Percentage, ';') from ElementChemistry where XRayId = MergedParticleInfo.XRayId and fieldid = MergedParticleInfo.fieldid) as Element from MergedParticleInfo where 1=1 " + condition + " union select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,SegmentNum,SEMPosX,SEMPosY,XrayId,DMAX,DMIN,DPERP,PERIMETER,ORIENTATION,DINSCR,DMEAN,DELONG,DFERET,TypeName,TypeColor,'' as SubParticles,'' as Element from INcAData where xrayid > -1 and instr(','||(select ifnull(group_concat(SubParticles, ','),'') from MergedParticleInfo)|| ',',',' || fieldid || ':' || particleid || ',')= 0 " + condition;
  407. DataTable DT = new DataTable();
  408. DT = dbHelper.ExecuteQuery(sqliteString);
  409. return DT;
  410. }
  411. public DataTable GetElementChemistry()
  412. {
  413. string sqliteString = "select * from ElementChemistry";
  414. DataTable DT = new DataTable();
  415. DT = dbHelper.ExecuteQuery(sqliteString);
  416. return DT;
  417. }
  418. public Bitmap GetBitmapForBig(string sub, double xs, string path,int picHeight,int picWidth)
  419. {
  420. string vs = "," + sub.Replace(':', '-') + ",";
  421. DataTable dataTable = GetParticleAll(vs);
  422. if (dataTable.Rows.Count == 0)
  423. {
  424. return null;
  425. }
  426. //内接矩形
  427. double max_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs - Convert.ToInt64(dataTable.Rows[0]["RectTop"]);
  428. double max_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs + Convert.ToInt64(dataTable.Rows[0]["RectLeft"]);
  429. double min_Y = max_Y;
  430. double min_X = max_X;
  431. //拼接field矩形
  432. double MAX_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs;
  433. double MAX_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs;
  434. double MIN_Y = MAX_Y;
  435. double MIN_X = MAX_X;
  436. foreach (DataRow item in dataTable.Rows)
  437. {
  438. //颗粒外接矩形
  439. double lefttopX = Convert.ToInt64(item["FieldPosX"]) * xs + Convert.ToInt64(item["RectLeft"]);
  440. if (lefttopX < min_X)
  441. {
  442. min_X = lefttopX;
  443. }
  444. if (lefttopX + Convert.ToInt64(item["RectWidth"]) > max_X)
  445. {
  446. max_X = lefttopX + Convert.ToInt64(item["RectWidth"]);
  447. }
  448. double lrfttopY = Convert.ToInt64(item["FieldPosY"]) * xs - Convert.ToInt64(item["RectTop"]);
  449. if (max_Y < lrfttopY)
  450. {
  451. max_Y = lrfttopY;
  452. }
  453. if (min_Y > lrfttopY - Convert.ToInt64(item["RectHeight"]))
  454. {
  455. min_Y = lrfttopY - Convert.ToInt64(item["RectHeight"]);
  456. }
  457. //画布
  458. double lefttopXH = Convert.ToInt64(item["FieldPosX"]) * xs;
  459. if (lefttopXH > MAX_X)
  460. {
  461. MAX_X = lefttopXH;
  462. }
  463. if (lefttopXH < MIN_X)
  464. {
  465. MIN_X = lefttopXH;
  466. }
  467. double lrfttopYH = Convert.ToInt64(item["FieldPosY"]) * xs;
  468. if (MAX_Y < lrfttopYH)
  469. {
  470. MAX_Y = lrfttopYH;
  471. }
  472. if (MIN_Y > lrfttopYH)
  473. {
  474. MIN_Y = lrfttopYH;
  475. }
  476. }
  477. int WIDTH = Convert.ToInt32(MAX_X - MIN_X) + picWidth;
  478. int HEIGHT = Convert.ToInt32(MAX_Y - MIN_Y) + picHeight;
  479. //构造最终的图片白板
  480. Bitmap tableChartImage = new Bitmap(WIDTH, HEIGHT);
  481. Graphics graph = Graphics.FromImage(tableChartImage);
  482. //初始化这个大图
  483. graph.DrawImage(tableChartImage, 0, 0);
  484. int width = Convert.ToInt32(max_X - min_X);
  485. int height = Convert.ToInt32(max_Y - min_Y);
  486. int X = Convert.ToInt32(min_X - MIN_X);
  487. int Y = Convert.ToInt32(MAX_Y - max_Y);
  488. Rectangle rectangle = new Rectangle() { X = X, Y = Y, Width = width, Height = height };
  489. foreach (DataRow item in dataTable.Rows)
  490. {
  491. string filePath = path + "\\FIELD_FILES\\";
  492. string imagePath = filePath + "Field" + item["fieldid"].ToString() + ".bmp";
  493. //然后将取出的数据,转换成Bitmap对象
  494. Bitmap ls_bt = ReadImageFile(imagePath);
  495. int x = Convert.ToInt32(Convert.ToDouble(item["FieldPosX"]) * xs - MIN_X);
  496. int y = Convert.ToInt32(Convert.ToDouble(item["FieldPosY"]) * xs - MIN_Y);
  497. graph.DrawImage(ls_bt, x, y);
  498. }
  499. Bitmap bmap = tableChartImage.Clone(rectangle, PixelFormat.Format8bppIndexed);
  500. return bmap;
  501. }
  502. /// <summary>
  503. /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
  504. /// </summary>
  505. /// <param name="path"></param>
  506. /// <returns></returns>
  507. public Bitmap ReadImageFile(string path)
  508. {
  509. if (!File.Exists(path))
  510. {
  511. return null;//文件不存在
  512. }
  513. FileStream fs = File.OpenRead(path); //OpenRead
  514. int filelength = 0;
  515. filelength = (int)fs.Length; //获得文件长度
  516. Byte[] image = new Byte[filelength]; //建立一个字节数组
  517. fs.Read(image, 0, filelength); //按字节流读取
  518. System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
  519. fs.Close();
  520. Bitmap bit = new Bitmap(result);
  521. return bit;
  522. }
  523. /// <summary>
  524. /// 传入单颗颗粒的particle类对象,返回从field中抠取出的bitmap对象,抠取单颗颗粒
  525. /// </summary>
  526. /// <param name="in_cotsparticleclr"></param>
  527. /// <returns></returns>
  528. public Bitmap GetBitmapByParticle(Bitmap ls_bt, Rectangle offset_rect)
  529. {
  530. //为了能把整个颗粒显示完整
  531. offset_rect.X = offset_rect.X - 5;
  532. offset_rect.Y = offset_rect.Y - 5;
  533. offset_rect.Width = offset_rect.Width + 10;
  534. offset_rect.Height = offset_rect.Height + 10;
  535. //防止计算偏差后,有坐标溢出现象
  536. if (offset_rect.X < 0)
  537. offset_rect.X = 0;
  538. if (offset_rect.Y < 0)
  539. offset_rect.Y = 0;
  540. if (offset_rect.X + offset_rect.Width > ls_bt.Width)
  541. {
  542. offset_rect.Width = ls_bt.Width - offset_rect.X;
  543. }
  544. if (offset_rect.Y + offset_rect.Height > ls_bt.Height)
  545. {
  546. offset_rect.Height = ls_bt.Height - offset_rect.Y;
  547. }
  548. Bitmap new_ret_bp;
  549. //防止为0后面计算出错
  550. if (offset_rect.Width > 0 && offset_rect.Height > 0)
  551. {
  552. //最后通过list_showsegment组建成新的图片,进行返回
  553. new_ret_bp = ls_bt.Clone(offset_rect, PixelFormat.Format8bppIndexed);
  554. }
  555. else
  556. {
  557. new_ret_bp = new Bitmap(offset_rect.Width, offset_rect.Height);
  558. }
  559. return new_ret_bp;
  560. }
  561. #endregion
  562. }
  563. public class UserLibraryData
  564. {
  565. private SqlHelper dbHelper;
  566. public UserLibraryData(string libraryName)
  567. {
  568. NLog.Logger log = NLog.LogManager.GetCurrentClassLogger();
  569. string fullPath = System.IO.Directory.GetCurrentDirectory() + "\\Config\\SysData\\" + libraryName + ".db";
  570. if (System.IO.File.Exists(fullPath))
  571. {
  572. dbHelper = new SqlHelper("data source='" + fullPath + "'");
  573. }
  574. else
  575. {
  576. dbHelper = null;
  577. log.Error("Failed to load user-defined library!");
  578. }
  579. }
  580. public DataTable GetSubAttributeFromDatabase()
  581. {
  582. string sqliteString = "select STDId,Hardness,Density,Electrical_conductivity from ClassifySTD";
  583. DataTable DT = new DataTable();
  584. DT = dbHelper.ExecuteQuery(sqliteString);
  585. return DT;
  586. }
  587. public SqlHelper GetSqlHelper() { return dbHelper; }
  588. }
  589. }