FieldImage.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using OTSCommon.DBOperate.Model;
  2. using OTSIncAReportGraph.Class;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.IO;
  9. namespace OTSIncAReportApp._3_ServiceCenter.DataOperation.DataAccess
  10. {
  11. public class FieldImage
  12. {
  13. string imagePath;
  14. public FieldImage(string rstpath)
  15. {
  16. imagePath= rstpath + @"\FIELD_FILES\";
  17. }
  18. /// <summary>
  19. /// 拼接颗粒黑白图
  20. /// </summary>
  21. /// <param name="sub">拼接字符串</param>
  22. /// <param name="xs">xs = pixw / ScanFieldSize</param>
  23. /// <param name="path">报告文件路径</param>
  24. /// <param name="picHeight">图像高</param>
  25. /// <param name="picWidth">帧图宽</param>
  26. /// <param name="segsData">KEY为FidANDpID</param>
  27. /// <returns></returns>
  28. public Bitmap GetBlackAndWhiteBitmapForMergedParticle(string sub, double xs, int picHeight, int picWidth,DataTable PartdataTable,Dictionary<string,List<Segment>> segsData)
  29. {
  30. string vs = "," + sub.Replace(':', '-') + ",";
  31. if (PartdataTable.Rows.Count == 0)
  32. {
  33. return null;
  34. }
  35. //内接矩形
  36. double max_Y = -1;
  37. double max_X = -1;
  38. double min_Y = 99999999;
  39. double min_X = 99999999;
  40. //拼接field矩形
  41. double B_Y = Convert.ToInt64(PartdataTable.Rows[0]["FieldPosY"]) * xs;
  42. double R_X = Convert.ToInt64(PartdataTable.Rows[0]["FieldPosX"]) * xs;
  43. double T_Y = B_Y;
  44. double L_X = R_X;
  45. foreach (DataRow item in PartdataTable.Rows)
  46. {
  47. double lefttopXH = Convert.ToInt64(item["FieldPosX"]) * xs;
  48. if (lefttopXH > R_X)
  49. {
  50. R_X = lefttopXH;
  51. }
  52. if (lefttopXH < L_X)
  53. {
  54. L_X = lefttopXH;
  55. }
  56. double lrfttopYH = Convert.ToInt64(item["FieldPosY"]) * xs;
  57. if (lrfttopYH < B_Y)
  58. {
  59. B_Y = lrfttopYH;
  60. }
  61. if (lrfttopYH > T_Y)
  62. {
  63. T_Y = lrfttopYH;
  64. }
  65. }
  66. int WIDTH = Math.Abs(Convert.ToInt32(R_X - L_X)) + picWidth;
  67. int HEIGHT = Math.Abs(Convert.ToInt32(B_Y - T_Y)) + picHeight;
  68. //构造最终的图片白板
  69. Bitmap tableChartImage = new Bitmap(WIDTH, HEIGHT);
  70. Graphics graph = Graphics.FromImage(tableChartImage);
  71. graph.Clear(Color.White);
  72. graph.DrawImage(tableChartImage, 0, 0);
  73. foreach (DataRow item in PartdataTable.Rows)
  74. {
  75. int x = Math.Abs(Convert.ToInt32(Convert.ToDouble(item["FieldPosX"]) * xs - L_X));
  76. int y = Math.Abs(Convert.ToInt32(Convert.ToDouble(item["FieldPosY"]) * xs - T_Y));
  77. //颗粒外接矩形
  78. double lefttopX = x + Convert.ToInt64(item["RectLeft"]);
  79. if (lefttopX < min_X)
  80. {
  81. min_X = lefttopX;
  82. }
  83. if (lefttopX + Convert.ToInt64(item["RectWidth"]) > max_X)
  84. {
  85. max_X = lefttopX + Convert.ToInt64(item["RectWidth"]);
  86. }
  87. double lrfttopY = y + Convert.ToInt64(item["RectTop"]);
  88. if (lrfttopY + Convert.ToInt64(item["RectHeight"]) > max_Y)
  89. {
  90. max_Y = lrfttopY + Convert.ToInt64(item["RectHeight"]);
  91. }
  92. if (lrfttopY < min_Y)
  93. {
  94. min_Y = lrfttopY;
  95. }
  96. string key = item["fieldid"].ToString() + "_" + item["particleid"].ToString();
  97. foreach (Segment seg in segsData[key])
  98. {
  99. int f_length = seg.Length;
  100. for (int m = 0; m < f_length; m++)
  101. {
  102. int lsjs_x = seg.Start + m + x;
  103. int lsjs_y = seg.Height + y;
  104. tableChartImage.SetPixel(lsjs_x, lsjs_y, Color.Black);
  105. }
  106. }
  107. }
  108. int width = Convert.ToInt32(max_X - min_X);
  109. int height = Convert.ToInt32(max_Y - min_Y);
  110. int X = Convert.ToInt32(min_X);
  111. int Y = Convert.ToInt32(min_Y);
  112. Rectangle rectangle = new Rectangle() { X = X, Y = Y, Width = width, Height = height };
  113. Bitmap bmap = CapturePic(tableChartImage, rectangle);
  114. return bmap;
  115. }
  116. public Bitmap ReadImageFile(string filename)
  117. {
  118. if (!File.Exists(imagePath+filename))
  119. {
  120. return null;//文件不存在
  121. }
  122. FileStream fs = File.OpenRead(imagePath + filename); //OpenRead
  123. int filelength = 0;
  124. filelength = (int)fs.Length; //获得文件长度
  125. Byte[] image = new Byte[filelength]; //建立一个字节数组
  126. fs.Read(image, 0, filelength); //按字节流读取
  127. System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
  128. fs.Close();
  129. Bitmap bit = new Bitmap(result);
  130. return bit;
  131. }
  132. /// <summary>
  133. /// 拼接颗粒图
  134. /// </summary>
  135. /// <param name="sub">拼接字符串</param>
  136. /// <param name="xs">xs = pixw / ScanFieldSize</param>
  137. /// <param name="path">报告文件路径</param>
  138. /// <param name="picHeight">图像高</param>
  139. /// <param name="picWidth">帧图宽</param>
  140. /// <returns></returns>
  141. public Bitmap GetBitmapForMergedParticle(string sub, double xs, int picHeight, int picWidth,DataTable dataTable)
  142. {
  143. string vs = "," + sub.Replace(':', '-') + ",";
  144. if (dataTable.Rows.Count == 0)
  145. {
  146. return null;
  147. }
  148. //内接矩形
  149. double max_Y = -1;
  150. double max_X = -1;
  151. double min_Y = 99999999;
  152. double min_X = 99999999;
  153. //拼接field矩形
  154. double B_Y = Convert.ToInt64(dataTable.Rows[0]["FieldPosY"]) * xs;
  155. double R_X = Convert.ToInt64(dataTable.Rows[0]["FieldPosX"]) * xs;
  156. double T_Y = B_Y;
  157. double L_X = R_X;
  158. foreach (DataRow item in dataTable.Rows)
  159. {
  160. double lefttopXH = Convert.ToInt64(item["FieldPosX"]) * xs;
  161. if (lefttopXH > R_X)
  162. {
  163. R_X = lefttopXH;
  164. }
  165. if (lefttopXH < L_X)
  166. {
  167. L_X = lefttopXH;
  168. }
  169. double lrfttopYH = Convert.ToInt64(item["FieldPosY"]) * xs;
  170. if (lrfttopYH < B_Y)
  171. {
  172. B_Y = lrfttopYH;
  173. }
  174. if (lrfttopYH > T_Y)
  175. {
  176. T_Y = lrfttopYH;
  177. }
  178. }
  179. int WIDTH = Math.Abs(Convert.ToInt32(R_X - L_X)) + picWidth;
  180. int HEIGHT = Math.Abs(Convert.ToInt32(B_Y - T_Y)) + picHeight;
  181. //构造最终的图片白板
  182. Bitmap tableChartImage = new Bitmap(WIDTH, HEIGHT);
  183. Graphics graph = Graphics.FromImage(tableChartImage);
  184. graph.DrawImage(tableChartImage, 0, 0);
  185. foreach (DataRow item in dataTable.Rows)
  186. {
  187. string path1= "Field" + item["fieldid"].ToString() + ".bmp";
  188. //然后将取出的数据,转换成Bitmap对象
  189. Bitmap ls_bt = ReadImageFile(path1);
  190. int x = Math.Abs(Convert.ToInt32(Convert.ToDouble(item["FieldPosX"]) * xs - L_X));
  191. int y = Math.Abs(Convert.ToInt32(Convert.ToDouble(item["FieldPosY"]) * xs - T_Y));
  192. graph.DrawImage(ls_bt, x, y);
  193. //颗粒外接矩形
  194. double lefttopX = x + Convert.ToInt64(item["RectLeft"]);
  195. if (lefttopX < min_X)
  196. {
  197. min_X = lefttopX;
  198. }
  199. if (lefttopX + Convert.ToInt64(item["RectWidth"]) > max_X)
  200. {
  201. max_X = lefttopX + Convert.ToInt64(item["RectWidth"]);
  202. }
  203. double lrfttopY = y + Convert.ToInt64(item["RectTop"]);
  204. if (lrfttopY + Convert.ToInt64(item["RectHeight"]) > max_Y)
  205. {
  206. max_Y = lrfttopY + Convert.ToInt64(item["RectHeight"]);
  207. }
  208. if (lrfttopY < min_Y)
  209. {
  210. min_Y = lrfttopY;
  211. }
  212. }
  213. int width = Convert.ToInt32(max_X - min_X);
  214. int height = Convert.ToInt32(max_Y - min_Y);
  215. int X = Convert.ToInt32(min_X);
  216. int Y = Convert.ToInt32(min_Y);
  217. Rectangle rectangle = new Rectangle() { X = X, Y = Y, Width = width, Height = height };
  218. Bitmap bmap = tableChartImage.Clone(rectangle, PixelFormat.Format8bppIndexed);
  219. graph.DrawImage(tableChartImage, 0, 0);
  220. return bmap;
  221. }
  222. /// <summary>
  223. /// 传入单颗颗粒的particle类对象,返回从field中抠取出的bitmap对象,抠取单颗颗粒
  224. /// </summary>
  225. /// <param name="in_cotsparticleclr"></param>
  226. /// <returns></returns>
  227. public Bitmap GetBitmapByParticle(DataTable dataTable)
  228. {
  229. string path1 = "Field" + dataTable.Rows[0]["fieldid"].ToString() + ".bmp";
  230. //然后将取出的数据,转换成Bitmap对象
  231. Bitmap ls_bt = ReadImageFile(path1);
  232. Rectangle offset_rect = new Rectangle() { X = Convert.ToInt32(dataTable.Rows[0]["RectLeft"]), Y = Convert.ToInt32(dataTable.Rows[0]["RectTop"]), Width = Convert.ToInt32(dataTable.Rows[0]["RectWidth"]), Height = Convert.ToInt32(dataTable.Rows[0]["RectHeight"]) };
  233. //为了能把整个颗粒显示完整
  234. offset_rect.X = offset_rect.X - 20;
  235. offset_rect.Y = offset_rect.Y - 20;
  236. offset_rect.Width = offset_rect.Width + 40;
  237. offset_rect.Height = offset_rect.Height + 40;
  238. //防止计算偏差后,有坐标溢出现象
  239. if (offset_rect.X < 0)
  240. offset_rect.X = 0;
  241. if (offset_rect.Y < 0)
  242. offset_rect.Y = 0;
  243. if (offset_rect.X + offset_rect.Width > ls_bt.Width)
  244. {
  245. offset_rect.Width = ls_bt.Width - offset_rect.X;
  246. }
  247. if (offset_rect.Y + offset_rect.Height > ls_bt.Height)
  248. {
  249. offset_rect.Height = ls_bt.Height - offset_rect.Y;
  250. }
  251. Bitmap new_ret_bp;
  252. //防止为0后面计算出错
  253. if (offset_rect.Width > 0 && offset_rect.Height > 0)
  254. {
  255. //最后通过list_showsegment组建成新的图片,进行返回
  256. new_ret_bp = ls_bt.Clone(offset_rect, ls_bt.PixelFormat);
  257. }
  258. else
  259. {
  260. new_ret_bp = new Bitmap(offset_rect.Width, offset_rect.Height);
  261. }
  262. return new_ret_bp;
  263. }
  264. /// <summary>
  265. /// 获取全部颗粒图
  266. /// </summary>
  267. /// <param name="sub">拼接颗粒连接字符串</param>
  268. /// <param name="dataTable">颗粒表</param>
  269. /// <param name="xs">pixel</param>
  270. /// <param name="picHeight">帧图高</param>
  271. /// <param name="picWidth">帧图宽</param>
  272. /// <returns></returns>
  273. public Bitmap GetBitmapForParticleAll(string sub, DataTable dataTable, double xs=0, int picHeight=0, int picWidth=0)
  274. {
  275. Bitmap bmap;
  276. if(sub != "")
  277. {
  278. bmap = GetBitmapForMergedParticle(sub, xs, picHeight, picWidth,dataTable);
  279. }
  280. else
  281. {
  282. bmap=GetBitmapByParticle(dataTable);
  283. }
  284. return bmap;
  285. }
  286. /// <summary>
  287. /// 获取全部颗粒图
  288. /// </summary>
  289. /// <param name="sub">拼接颗粒连接字符串</param>
  290. /// <param name="dataTable">颗粒表</param>
  291. /// <param name="xs">pixel</param>
  292. /// <param name="picHeight">帧图高</param>
  293. /// <param name="picWidth">帧图宽</param>
  294. /// <returns></returns>
  295. public Bitmap GetBitmapForParticleAllWithBlackAndWhite(string sub, DataTable dataTable, Dictionary<string, List<Segment>> segsData, double xs = 0, int picHeight = 0, int picWidth = 0)
  296. {
  297. Bitmap bmap;
  298. if (sub != "")
  299. {
  300. bmap = GetBlackAndWhiteBitmapForMergedParticle(sub, xs, picHeight, picWidth, dataTable, segsData);
  301. }
  302. else
  303. {
  304. List<Segment> liseg=segsData[dataTable.Rows[0]["fieldid"].ToString() + "_" + dataTable.Rows[0]["particleid"].ToString()];
  305. bmap = GetBlackAndWhiteImage(dataTable, picHeight, picWidth,liseg);
  306. }
  307. return bmap;
  308. }
  309. public Bitmap CapturePic(Bitmap ls_bt, Rectangle offset_rect)
  310. {
  311. //为了能把整个颗粒显示完整
  312. offset_rect.X = offset_rect.X - 20;
  313. offset_rect.Y = offset_rect.Y - 20;
  314. offset_rect.Width = offset_rect.Width + 40;
  315. offset_rect.Height = offset_rect.Height + 40;
  316. //防止计算偏差后,有坐标溢出现象
  317. if (offset_rect.X < 0)
  318. offset_rect.X = 0;
  319. if (offset_rect.Y < 0)
  320. offset_rect.Y = 0;
  321. if (offset_rect.X + offset_rect.Width > ls_bt.Width)
  322. {
  323. offset_rect.Width = ls_bt.Width - offset_rect.X;
  324. }
  325. if (offset_rect.Y + offset_rect.Height > ls_bt.Height)
  326. {
  327. offset_rect.Height = ls_bt.Height - offset_rect.Y;
  328. }
  329. Bitmap new_ret_bp;
  330. //防止为0后面计算出错
  331. if (offset_rect.Width > 0 && offset_rect.Height > 0)
  332. {
  333. //最后通过list_showsegment组建成新的图片,进行返回
  334. new_ret_bp = ls_bt.Clone(offset_rect, ls_bt.PixelFormat);
  335. }
  336. else
  337. {
  338. new_ret_bp = new Bitmap(offset_rect.Width, offset_rect.Height);
  339. }
  340. return new_ret_bp;
  341. }
  342. public Bitmap GetBlackAndWhiteImage(DataTable particle,int picHeight, int picWidth, List<Segment> segsData)
  343. {
  344. Bitmap dpImage=new Bitmap(picWidth, picHeight);
  345. using (Graphics graphics = Graphics.FromImage(dpImage))
  346. {
  347. graphics.Clear(Color.White); // 清除位图背景为白色
  348. }
  349. foreach (Segment seg in segsData)
  350. {
  351. int f_length = seg.Length;
  352. for (int m = 0; m < f_length; m++)
  353. {
  354. int lsjs_x = seg.Start + m;
  355. int lsjs_y = seg.Height;
  356. dpImage.SetPixel(lsjs_x, lsjs_y, Color.Black);
  357. }
  358. }
  359. DataRow row = particle.Rows[0];
  360. Rectangle rectangle = new Rectangle() { X = Convert.ToInt32(row["RectLeft"]), Y = Convert.ToInt32(row["RectTop"]), Width = Convert.ToInt32(row["RectWidth"]), Height = Convert.ToInt32(row["RectHeight"]) };
  361. Bitmap bmap = CapturePic(dpImage, rectangle);
  362. return bmap;
  363. }
  364. }
  365. }