NewFunMethod.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Drawing;
  8. namespace HOZProject.Helpers
  9. {
  10. public class NewFunMethod
  11. {
  12. //Xray谱图生成图片
  13. /// <summary>
  14. /// Xray谱图生成图片
  15. /// </summary>
  16. /// <param name="analysis_xray">Xray数据</param>
  17. /// <param name="list_showelementinfo">元素列表</param>
  18. /// <param name="saveImgPathName">保存路径名称</param>
  19. /// <returns>Image</returns>
  20. public void GetXrayToImage(uint[] analysis_xray, List<ShowElementInfo> list_showelementinfo, string saveImgPathName)
  21. {
  22. try
  23. {
  24. Control_XRayTable control_XRayTable1 = new Control_XRayTable();
  25. //获取数据后,需要对xraytable设置
  26. control_XRayTable1.SetXRayShowLineValue(analysis_xray, list_showelementinfo);
  27. //设置图像
  28. Bitmap bitmap = new Bitmap(control_XRayTable1.Width, control_XRayTable1.Height);
  29. Rectangle rectImg = new Rectangle();
  30. rectImg.Location = control_XRayTable1.Location;
  31. rectImg.Width = control_XRayTable1.Width;
  32. rectImg.Height = control_XRayTable1.Height;
  33. control_XRayTable1.DrawToBitmap(bitmap, rectImg);
  34. //如要保存请添加保存路径名称参数
  35. bitmap.Save(saveImgPathName, System.Drawing.Imaging.ImageFormat.Png);
  36. bitmap.Dispose();
  37. }
  38. catch (Exception ex)
  39. {
  40. //记录日志
  41. }
  42. }
  43. /// <summary>
  44. /// BSE标注颗粒位置
  45. /// </summary>
  46. /// <param name="bseImgPath">BSE图像所在位置路径参数</param>
  47. /// <param name="points">分析点位置列表</param>
  48. /// <param name="drawType">绘制类型 0:点 1:线 2:面</param>
  49. /// <param name="saveImgPathName">添加分析点位置标注后,图像保存路径</param>
  50. /// <returns></returns>
  51. public void GetBseAnalyticalSite(string bseImgPath, List<DrawAnalysePoint> darwAPList, string saveImgPathName)
  52. {
  53. try
  54. {
  55. //1.BSE图像所在位置路径参数
  56. System.Drawing.Image img = System.Drawing.Image.FromFile(bseImgPath);
  57. Bitmap bitmap = new Bitmap(img.Width,img.Height);
  58. string filePath = bseImgPath;
  59. //全路径 C:\aaa\bbb\ccc.xml
  60. string fileFullPath = System.IO.Path.GetFullPath(filePath);
  61. //所在文件夹路径 C:\aaa\bbb
  62. string fileDirectoryName = System.IO.Path.GetDirectoryName(filePath);
  63. //文件名称.后缀名 ccc.xml
  64. string fileName = System.IO.Path.GetFileName(filePath);
  65. //后缀名 .xml
  66. string ExtensionName = System.IO.Path.GetExtension(filePath);
  67. //2.生成图像绘制对象
  68. Graphics g = Graphics.FromImage(bitmap);
  69. //绘制BSE原图
  70. g.DrawImage(img, 0, 0);
  71. //编辑绘制图形的线条颜色
  72. Pen drawPan = new Pen(Color.Red);
  73. //点标注 + 号 宽高长度
  74. int WH = 20;
  75. //字体大小
  76. float fontSize = 15;
  77. Font font = new Font("黑体", fontSize, FontStyle.Bold);
  78. SolidBrush solidBrush = new SolidBrush(Color.Red);
  79. //绘制标签 Y轴向上偏移位置
  80. int drawTitleOffUPLocation = 20;
  81. //3.绘制类型 0:点 1:线 2:面
  82. for (int i = 0; i < darwAPList.Count; i++)
  83. {
  84. DrawType drawType = darwAPList[i].DrawType;
  85. string title = darwAPList[i].Title;
  86. switch (drawType)
  87. {
  88. case DrawType.Point:
  89. int startPointX = darwAPList[i].Point.X - WH;
  90. int endPointX = darwAPList[i].Point.X + WH;
  91. int startPointY = darwAPList[i].Point.Y - WH;
  92. int endPointY = darwAPList[i].Point.Y + WH;
  93. //绘制点十字
  94. g.DrawLine(drawPan, startPointX, darwAPList[i].Point.Y, endPointX, darwAPList[i].Point.Y);
  95. g.DrawLine(drawPan, darwAPList[i].Point.X, startPointY, darwAPList[i].Point.X, endPointY);
  96. //绘制标签
  97. g.DrawString(title, font, solidBrush, darwAPList[i].Point.X, darwAPList[i].Point.Y- drawTitleOffUPLocation);
  98. break;
  99. case DrawType.Line:
  100. List<Point> points = darwAPList[i].LinePoints;
  101. System.Drawing.Drawing2D.GraphicsPath graphicsPath = new System.Drawing.Drawing2D.GraphicsPath();
  102. //将多点生成绘制路径
  103. graphicsPath.AddLines(points.ToArray());
  104. g.DrawPath(drawPan, graphicsPath);
  105. //绘制标签
  106. g.DrawString(title, font, solidBrush, darwAPList[i].LinePoints[0].X, darwAPList[i].LinePoints[0].Y - drawTitleOffUPLocation);
  107. break;
  108. case DrawType.Rectangle:
  109. Rectangle rect = new Rectangle();
  110. rect.Location = new Point(darwAPList[i].Point.X, darwAPList[i].Point.Y);
  111. rect.Size = darwAPList[i].RectSize;
  112. g.DrawRectangle(drawPan, rect);
  113. //绘制标签
  114. g.DrawString(title, font, solidBrush, darwAPList[i].Point.X, darwAPList[i].Point.Y - drawTitleOffUPLocation);
  115. break;
  116. default:
  117. break;
  118. }
  119. }
  120. //4.添加分析点位置标注后,图像保存路径
  121. bitmap.Save(saveImgPathName, System.Drawing.Imaging.ImageFormat.Png);
  122. g.Dispose();
  123. bitmap.Dispose();
  124. }
  125. catch (Exception ex)
  126. {
  127. //记录日志
  128. }
  129. }
  130. }
  131. public enum DrawType
  132. {
  133. Point = 0,
  134. Line = 1,
  135. Rectangle=2
  136. }
  137. //元素包含信息结构
  138. public class Periodic
  139. {
  140. string _xh; //序号
  141. string _yzzl; //原子重量,因为有标记的重量带有()括号,所以这里先用字符串进行存储
  142. string _fh; //符号
  143. string _zwysm; //中文元素名
  144. string _ywm; //英文名
  145. string _sx1; //属性1
  146. string _sx2; //属性2
  147. string _sx3; //属性3
  148. string _fl; //分类
  149. /// <summary>
  150. /// 序号
  151. /// </summary>
  152. public string XH
  153. {
  154. get { return _xh; }
  155. set { _xh = value; }
  156. }
  157. /// <summary>
  158. /// 原子重量
  159. /// </summary>
  160. public string YZZL
  161. {
  162. get { return _yzzl; }
  163. set { _yzzl = value; }
  164. }
  165. /// <summary>
  166. /// 符号
  167. /// </summary>
  168. public string FH
  169. {
  170. get { return _fh; }
  171. set { _fh = value; }
  172. }
  173. /// <summary>
  174. /// 中文元素名
  175. /// </summary>
  176. public string ZWYSM
  177. {
  178. get { return _zwysm; }
  179. set { _zwysm = value; }
  180. }
  181. /// <summary>
  182. /// 英文名
  183. /// </summary>
  184. public string YWM
  185. {
  186. get { return _ywm; }
  187. set { _ywm = value; }
  188. }
  189. /// <summary>
  190. /// 属性1
  191. /// </summary>
  192. public string SX1
  193. {
  194. get { return _sx1; }
  195. set { _sx1 = value; }
  196. }
  197. /// <summary>
  198. /// 属性2
  199. /// </summary>
  200. public string SX2
  201. {
  202. get { return _sx2; }
  203. set { _sx2 = value; }
  204. }
  205. /// <summary>
  206. /// 属性3
  207. /// </summary>
  208. public string SX3
  209. {
  210. get { return _sx3; }
  211. set { _sx3 = value; }
  212. }
  213. /// <summary>
  214. /// 分类
  215. /// </summary>
  216. public string FL
  217. {
  218. get { return _fl; }
  219. set { _fl = value; }
  220. }
  221. }
  222. public class DrawAnalysePoint
  223. {
  224. /// <summary>
  225. /// 标签名称
  226. /// </summary>
  227. private string title;
  228. /// <summary>
  229. /// 分析点位置
  230. /// </summary>
  231. private Point point;
  232. /// <summary>
  233. /// 绘制类型 面尺寸
  234. /// </summary>
  235. private Size rectSize;
  236. /// <summary>
  237. /// 绘制类型:线 点位置
  238. /// </summary>
  239. private List<Point> linePoints;
  240. /// <summary>
  241. /// 绘制类型 Point:点 Line:线 Rectangle:面
  242. /// </summary>
  243. private DrawType drawType;
  244. public string Title { get => title; set => title = value; }
  245. public Point Point { get => point; set => point = value; }
  246. public Size RectSize { get => rectSize; set => rectSize = value; }
  247. public List<Point> LinePoints { get => linePoints; set => linePoints = value; }
  248. public DrawType DrawType { get => drawType; set => drawType = value; }
  249. }
  250. }