FieldImage.cs 16 KB

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