ParticleData.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 ";
  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. return DT;
  124. }
  125. /// <summary>
  126. /// 获取element含量
  127. /// </summary>
  128. /// <returns></returns>
  129. public DataTable GetElementForArea(string fieldAndPartic)
  130. {
  131. string sqlp = @"select e.name,sum(e.percentage*p.area) as earea from ElementChemistry e
  132. inner join INcAData p on e.xrayid=p.xrayid and e.fieldid = p.fieldid";
  133. if (fieldAndPartic != "")
  134. {
  135. sqlp = sqlp + " where '" + fieldAndPartic + "' like ('%,'||p.fieldid||'-'||p.particleid||',%')";
  136. }
  137. sqlp = sqlp + " group by e.name order by sum(e.percentage*p.area) desc";
  138. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  139. return DT;
  140. }
  141. /// <summary>
  142. /// 获取element含量
  143. /// </summary>
  144. /// <returns></returns>
  145. public DataTable GetSmallElementForArea()
  146. {
  147. 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
  148. group by e.name order by sum(e.percentage*i.area) desc";
  149. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  150. return DT;
  151. }
  152. /// <summary>
  153. /// 获取所有Particle
  154. /// </summary>
  155. /// <returns></returns>
  156. public DataTable GetParticleAll(string fieldAndPartic)
  157. {
  158. string sqlp = @"select * from INcAData";
  159. if (fieldAndPartic != "")
  160. {
  161. sqlp = sqlp + " where '" + fieldAndPartic + "' like ('%,'||fieldid||'-'||particleid||',%')";
  162. }
  163. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  164. return DT;
  165. }
  166. /// <summary>
  167. /// 获取所有Particle
  168. /// </summary>
  169. /// <returns></returns>
  170. public DataTable GetParticleHaveXray(string fieldAndPartic)
  171. {
  172. string sqlp = @"select *,
  173. (select group_concat(name||'-'||Percentage,';')
  174. from ElementChemistry where XRayId =INcAData.XRayId and fieldid=INcAData.fieldid ) as Element from INcAData where xrayid>-1 ";
  175. if (fieldAndPartic != "")
  176. {
  177. sqlp = sqlp + " and '" + fieldAndPartic + "' like ('%,'||fieldid||'-'||particleid||',%')";
  178. }
  179. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  180. return DT;
  181. }
  182. /// <summary>
  183. /// 获取所有Particle
  184. /// </summary>
  185. /// <returns></returns>
  186. public List<Model.Particle> GetParticleAllList()
  187. {
  188. string sqlp = @"select * from INcAData";
  189. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  190. List<Model.Particle> listp = dbHelper.TableToList<Model.Particle>(DT);
  191. return listp;
  192. }
  193. /// <summary>
  194. /// 获取所有分类的面积
  195. /// </summary>
  196. /// <param name="fieldAndPartic">选择颗粒</param>
  197. /// <returns></returns>
  198. public DataTable GetAreaByAllIncA(string fieldAndPartic)
  199. {
  200. string sqlp = @"select TypeId,TypeName,sum(area) as ar,count(1) as con from INcAData";
  201. if (fieldAndPartic != "")
  202. {
  203. sqlp = sqlp + " where '" + fieldAndPartic + "' like ('%,'||fieldid||'-'||particleid||',%')";
  204. }
  205. sqlp = sqlp + " group by TypeId";
  206. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  207. return DT;
  208. }
  209. /// <summary>
  210. /// 获取所有大颗粒没有的小颗粒分类
  211. /// </summary>
  212. /// <param name="fieldAndPartic">选择颗粒</param>
  213. /// <returns></returns>
  214. public DataTable GetSmallParticleInfo()
  215. {
  216. 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";
  217. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  218. return DT;
  219. }
  220. /// <summary>
  221. /// 获取不同分类的面积
  222. /// </summary>
  223. /// <param name="fieldAndPartic">选择颗粒</param>
  224. /// <returns></returns>
  225. public DataTable GetAreaByIncA(string TypeId, string fieldAndPartic)
  226. {
  227. string sqlp = @"select e.name,sum(e.percentage*p.area) as pc,p.TypeId from ElementChemistry e
  228. inner join INcAData p on e.xrayid=p.xrayid and e.fieldid = p.fieldid where p.TypeId=" + TypeId + " ";
  229. if (fieldAndPartic != "")
  230. {
  231. sqlp = sqlp + " and '" + fieldAndPartic + "' like ('%,'||p.fieldid||'-'||p.particleid||',%')";
  232. }
  233. sqlp = sqlp + " group by e.name";
  234. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  235. return DT;
  236. }
  237. public DataTable GetAreaByIncA_All()
  238. {
  239. string sqlp = @"select e.name,sum(e.percentage*p.area) as pc,p.TypeId from ElementChemistry e
  240. inner join INcAData p on e.xrayid=p.xrayid and e.fieldid = p.fieldid group by e.name ";
  241. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  242. return DT;
  243. }
  244. /// <summary>
  245. /// 获取所有元素
  246. /// </summary>
  247. /// <param name="model">Feature</param>
  248. /// <returns></returns>
  249. public DataTable GetAllElement()
  250. {
  251. string sqlp = @"select name from ElementChemistry group by name order by count(1) desc";
  252. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  253. return DT;
  254. }
  255. /// <summary>
  256. /// 获取常用夹杂物分类信息
  257. /// </summary>
  258. /// <returns></returns>
  259. public DataTable GetCommonlyUsedClassifyData()
  260. {
  261. string sqlp = @"select
  262. (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 ,
  263. (select count(typeid) from incadata where typeid BETWEEN 10000 and 10999 ) as OXIDE ,
  264. (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 ,
  265. (select count(typeid) from incadata where typeid BETWEEN 12000 and 12999 ) as NITRIDE ,
  266. (select count(typeid) from incadata where typeid BETWEEN 11000 and 11999 ) as SULFIDE ";
  267. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  268. return DT;
  269. }
  270. /// <summary>
  271. /// 获取颗粒信息
  272. /// </summary>
  273. /// <param name="model">Feature</param>
  274. /// <returns></returns>
  275. public Model.Particle GetParticleByFidAndPid(string fieldid, string particleid)
  276. {
  277. string sqlp = @"select *,(select xraydata from xraydata where xrayindex=INcAData.xrayid and fieldid="
  278. + fieldid + ") as XRayData,(select group_concat(name || '-' || Percentage, ';')from ElementChemistry where XRayId = INcAData.XRayId and fieldid ="
  279. + fieldid + ") as Element from INcAData where fieldid="
  280. + fieldid + " and particleid= " + particleid;
  281. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  282. List<Model.Particle> listp = dbHelper.TableToList<Model.Particle>(DT);
  283. Model.Particle particle = new Model.Particle();
  284. if (listp.Count > 0)
  285. {
  286. particle = listp[0];
  287. List<Model.Element> ElementList = new List<Model.Element>();
  288. string element = DT.Rows[0]["Element"].ToString();
  289. for (int i = 0; i < element.Split(';').Count(); i++)
  290. {
  291. if (element.Split(';')[i] != "")
  292. {
  293. Model.Element ele = new Model.Element() { Name = element.Split(';')[i].Split('-')[0], Percentage = Convert.ToDouble(element.Split(';')[i].Split('-')[1]) };
  294. ElementList.Add(ele);
  295. }
  296. }
  297. particle.ElementList = ElementList;
  298. }
  299. return particle;
  300. }
  301. public Model.Particle GetParticleXrayDataByFidAndPid(string fieldid, string particleid)
  302. {
  303. string sqlp = @"select xraydata from xraydata where xrayindex="+particleid+" and fieldid="
  304. + fieldid ;
  305. DataTable DT = dbHelper.ExecuteDataTable(sqlp, null);
  306. List<Model.Particle> listp = dbHelper.TableToList<Model.Particle>(DT);
  307. return listp[0];
  308. }
  309. public enum PARTCLE_TYPE
  310. {
  311. SMALL = 0,//过小颗粒
  312. OVERSIZE = 1,//过大颗粒
  313. AVE_GRAY_NOT_INRANRE = 2,//亮度不在分析范围内的颗粒
  314. SEARCH_X_RAY = 3,
  315. LOW_COUNT = 4,//低计数率颗粒
  316. NO_INTEREST_ELEMENTS = 5,
  317. NO_ANALYSIS_X_RAY = 6,//不含分析元素
  318. NOT_IDENTIFIED_SIC = 7,//非夹杂物颗粒SiC
  319. NOT_IDENTIFIED_FEO = 8,//非夹杂物颗粒FeO
  320. NOT_IDENTIFIED = 9,//未识别颗粒
  321. IDENTIFIED = 10,//分析颗粒,当为可识别类型时,可以被进一步识别为用户类型(1000以上),系统预定义类型(10000以上),所以最终颗粒类型不会为8,但可能为7
  322. USER_DEFINED_MIN = 1000,
  323. SYS_DEFINED_MIN = 10000
  324. }
  325. #region 分页添加读取数据库函数
  326. /// <summary>
  327. /// 获取分页查询所需信息
  328. /// </summary>
  329. /// <param name=""></param>
  330. /// <param name=""></param>
  331. /// <returns></returns>
  332. public DataTable GetInfoForPartucleDevidePage(int currentPage, int pagesize, string OrderFunction, string condition)
  333. {
  334. int p = (currentPage - 1) * pagesize;
  335. string sqliteString = "select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,ElementNum,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,ElementNum,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();
  336. DataTable DT=new DataTable();
  337. DT = dbHelper.ExecuteQuery(sqliteString);
  338. return DT;
  339. }
  340. public DataTable GetInfoForPartucleDevidePage2(string condition)
  341. {
  342. string sqliteString = "select fieldid,particleid,AveGray,RectLeft,RectTop,RectWidth,RectHeight,Area,PosX,PosY,TypeId,ElementNum,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,ElementNum,SegmentNum,FieldPosX,FieldPosY,ParticleId,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;
  343. DataTable DT = new DataTable();
  344. DT = dbHelper.ExecuteQuery(sqliteString);
  345. return DT;
  346. }
  347. public DataTable GetElementChemistry()
  348. {
  349. string sqliteString = "select * from ElementChemistry";
  350. DataTable DT = new DataTable();
  351. DT = dbHelper.ExecuteQuery(sqliteString);
  352. return DT;
  353. }
  354. public Bitmap GetBitmapForBig(string sub, double xs, string path)
  355. {
  356. string vs = "," + sub.Replace(':', '-') + ",";
  357. DataTable dataTable = GetParticleAll(vs);
  358. if (dataTable.Rows.Count == 0)
  359. {
  360. return null;
  361. }
  362. //内接矩形
  363. double max_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs - Convert.ToInt64(dataTable.Rows[0]["RectTop"]);
  364. double max_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs + Convert.ToInt64(dataTable.Rows[0]["RectLeft"]);
  365. double min_Y = max_Y;
  366. double min_X = max_X;
  367. //拼接field矩形
  368. double MAX_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs;
  369. double MAX_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs;
  370. double MIN_Y = MAX_Y;
  371. double MIN_X = MAX_X;
  372. foreach (DataRow item in dataTable.Rows)
  373. {
  374. //颗粒外接矩形
  375. double lefttopX = Convert.ToInt64(item["FieldPosX"]) * xs + Convert.ToInt64(item["RectLeft"]);
  376. if (lefttopX < min_X)
  377. {
  378. min_X = lefttopX;
  379. }
  380. if (lefttopX + Convert.ToInt64(item["RectWidth"]) > max_X)
  381. {
  382. max_X = lefttopX + Convert.ToInt64(item["RectWidth"]);
  383. }
  384. double lrfttopY = Convert.ToInt64(item["FieldPosY"]) * xs - Convert.ToInt64(item["RectTop"]);
  385. if (max_Y < lrfttopY)
  386. {
  387. max_Y = lrfttopY;
  388. }
  389. if (min_Y > lrfttopY - Convert.ToInt64(item["RectHeight"]))
  390. {
  391. min_Y = lrfttopY - Convert.ToInt64(item["RectHeight"]);
  392. }
  393. //画布
  394. double lefttopXH = Convert.ToInt64(item["FieldPosX"]) * xs;
  395. if (lefttopXH > MAX_X)
  396. {
  397. MAX_X = lefttopXH;
  398. }
  399. if (lefttopXH < MIN_X)
  400. {
  401. MIN_X = lefttopXH;
  402. }
  403. double lrfttopYH = Convert.ToInt64(item["FieldPosY"]) * xs;
  404. if (MAX_Y < lrfttopYH)
  405. {
  406. MAX_Y = lrfttopYH;
  407. }
  408. if (MIN_Y > lrfttopYH)
  409. {
  410. MIN_Y = lrfttopYH;
  411. }
  412. }
  413. int WIDTH = Convert.ToInt32(MAX_X - MIN_X) + 1024;
  414. int HEIGHT = Convert.ToInt32(MAX_Y - MIN_Y) + 768;
  415. //构造最终的图片白板
  416. Bitmap tableChartImage = new Bitmap(WIDTH, HEIGHT);
  417. Graphics graph = Graphics.FromImage(tableChartImage);
  418. //初始化这个大图
  419. graph.DrawImage(tableChartImage, 0, 0);
  420. int width = Convert.ToInt32(max_X - min_X);
  421. int height = Convert.ToInt32(max_Y - min_Y);
  422. int X = Convert.ToInt32(min_X - MIN_X);
  423. int Y = Convert.ToInt32(MAX_Y - max_Y);
  424. Rectangle rectangle = new Rectangle() { X = X, Y = Y, Width = width, Height = height };
  425. foreach (DataRow item in dataTable.Rows)
  426. {
  427. string filePath = path + "\\FIELD_FILES\\";
  428. string imagePath = filePath + "Field" + item["fieldid"].ToString() + ".bmp";
  429. //然后将取出的数据,转换成Bitmap对象
  430. Bitmap ls_bt = ReadImageFile(imagePath);
  431. int x = Convert.ToInt32(Convert.ToDouble(item["FieldPosX"]) * xs - MIN_X);
  432. int y = Convert.ToInt32(Convert.ToDouble(item["FieldPosY"]) * xs - MIN_Y);
  433. try
  434. {
  435. graph.DrawImage(ls_bt, x, y);
  436. }
  437. catch (Exception e)
  438. {
  439. throw;
  440. }
  441. }
  442. Bitmap bmap = tableChartImage.Clone(rectangle, PixelFormat.Format8bppIndexed);
  443. return bmap;
  444. }
  445. /// <summary>
  446. /// 通过FileStream 来打开文件,这样就可以实现不锁定Image文件,到时可以让多用户同时访问Image文件
  447. /// </summary>
  448. /// <param name="path"></param>
  449. /// <returns></returns>
  450. public Bitmap ReadImageFile(string path)
  451. {
  452. if (!File.Exists(path))
  453. {
  454. return null;//文件不存在
  455. }
  456. FileStream fs = File.OpenRead(path); //OpenRead
  457. int filelength = 0;
  458. filelength = (int)fs.Length; //获得文件长度
  459. Byte[] image = new Byte[filelength]; //建立一个字节数组
  460. fs.Read(image, 0, filelength); //按字节流读取
  461. System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
  462. fs.Close();
  463. Bitmap bit = new Bitmap(result);
  464. return bit;
  465. }
  466. /// <summary>
  467. /// 传入单颗颗粒的particle类对象,返回从field中抠取出的bitmap对象,抠取单颗颗粒
  468. /// </summary>
  469. /// <param name="in_cotsparticleclr"></param>
  470. /// <returns></returns>
  471. public Bitmap GetBitmapByParticle(Bitmap ls_bt, Rectangle offset_rect)
  472. {
  473. //为了能把整个颗粒显示完整
  474. offset_rect.X = offset_rect.X - 5;
  475. offset_rect.Y = offset_rect.Y - 5;
  476. offset_rect.Width = offset_rect.Width + 10;
  477. offset_rect.Height = offset_rect.Height + 10;
  478. //防止计算偏差后,有坐标溢出现象
  479. if (offset_rect.X < 0)
  480. offset_rect.X = 0;
  481. if (offset_rect.Y < 0)
  482. offset_rect.Y = 0;
  483. if (offset_rect.X + offset_rect.Width > ls_bt.Width)
  484. {
  485. offset_rect.Width = ls_bt.Width - offset_rect.X;
  486. }
  487. if (offset_rect.Y + offset_rect.Height > ls_bt.Height)
  488. {
  489. offset_rect.Height = ls_bt.Height - offset_rect.Y;
  490. }
  491. Bitmap new_ret_bp;
  492. //防止为0后面计算出错
  493. if (offset_rect.Width > 0 && offset_rect.Height > 0)
  494. {
  495. //最后通过list_showsegment组建成新的图片,进行返回
  496. new_ret_bp = ls_bt.Clone(offset_rect, PixelFormat.Format8bppIndexed);
  497. }
  498. else
  499. {
  500. new_ret_bp = new Bitmap(offset_rect.Width, offset_rect.Height);
  501. }
  502. return new_ret_bp;
  503. }
  504. #endregion
  505. }
  506. }