Form1.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Drawing.Imaging;
  5. using System.Reflection;
  6. using System.Runtime.InteropServices;
  7. using System.Windows.Forms;
  8. using EInk.Dtos;
  9. using EInk.Models;
  10. using Newtonsoft.Json;
  11. using SqlSugar;
  12. using SqlSugar.Extensions;
  13. using static System.Windows.Forms.VisualStyles.VisualStyleElement.Rebar;
  14. namespace Eink.AOIDefectAnalysis
  15. {
  16. public partial class Form1 : Form
  17. {
  18. private List<HeatPoint> HeatPoints = new List<HeatPoint>();
  19. Bitmap bMap;
  20. Bitmap bMap_New;
  21. ISqlSugarClient db;
  22. List<Lot2Lot3Model> Lot2DataList;
  23. private int Intense = 25;
  24. private int drift_x;
  25. private int drift_y;
  26. public Form1()
  27. {
  28. InitializeComponent();
  29. Reset();
  30. db = new SqlSugarClient(new ConnectionConfig()
  31. {
  32. DbType = DbType.PostgreSQL,
  33. ConnectionString = "Host=127.0.0.1;Port=5432;Database=Eink;Username=postgres;Password=eink;",
  34. IsAutoCloseConnection = true
  35. });
  36. drift_x = panel1.Width / 2;
  37. drift_y = panel1.Height / 2;
  38. }
  39. private void generateBtn_Click(object sender, EventArgs e)
  40. {
  41. Reset();
  42. for (int i = (int)startNUD.Value - 1; i < (int)startNUD.Value + (int)numNUD.Value - 1; i++)
  43. {
  44. Lot2Lot3Dto? lot2lot3 = JsonConvert.DeserializeObject<Lot2Lot3Dto>(Lot2DataList[i].content);
  45. if (lot2lot3 != null && lot2lot3.result == "NG" && !generate_Picture(lot2lot3))
  46. {
  47. dataGridView1.Rows.Add(new string[] { (i + 1).ToString(), lot2lot3.createTime, lot2lot3.slidesNumber, lot2lot3.result });
  48. }
  49. }
  50. }
  51. private void clearBtn_Click(object sender, EventArgs e)
  52. {
  53. HeatPoints.Clear();
  54. bMap = new Bitmap((int)panel1.Width, (int)panel1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  55. using (Graphics g = Graphics.FromImage(bMap))
  56. {
  57. g.Clear(System.Drawing.Color.White);
  58. }
  59. bMap_New = new Bitmap((int)panel1.Width, (int)panel1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  60. UpdateView();
  61. }
  62. private void UpdateView()
  63. {
  64. // 生成灰度热力图
  65. bMap_New = CreateIntensityMask(bMap_New, HeatPoints);
  66. var bMap_byte_new = BitmapToByteArray(bMap_New);
  67. var bMap_byte = BitmapToByteArray(bMap);
  68. for (int i = 0; i < bMap_byte_new.Length; i++)
  69. {
  70. if (bMap_byte_new[i] != 255)
  71. {
  72. bMap_byte_new[i] = (byte)(255 - Intense);
  73. bMap_byte[i] = (byte)(bMap_byte[i] - Intense);
  74. }
  75. }
  76. label5.Text = ((255 - bMap_byte.Min()) / Intense).ToString();
  77. bMap = ByteArrayToBitmap(bMap_byte, bMap.Width, bMap.Height);
  78. bMap_New = bMap;
  79. panel1.BackgroundImage = bMap_New;
  80. // 上色
  81. bMap_New = Colorize(bMap_New, 255);
  82. panel2.BackgroundImage = bMap_New;
  83. }
  84. private Bitmap CreateIntensityMask(Bitmap bSurface, List<HeatPoint> aHeatPoints)
  85. {
  86. using (Graphics DrawSurface = Graphics.FromImage(bSurface))
  87. {
  88. DrawSurface.Clear(System.Drawing.Color.White);
  89. foreach (HeatPoint DataPoint in aHeatPoints)
  90. {
  91. DrawHeatPoint(DrawSurface, DataPoint); // 绘制单色矩形
  92. }
  93. }
  94. return bSurface;
  95. }
  96. private void DrawHeatPoint(Graphics Canvas, HeatPoint HeatPoint)
  97. {
  98. // 定义矩形区域
  99. Rectangle rect = new Rectangle(
  100. HeatPoint.X - HeatPoint.Width / 2,
  101. HeatPoint.Y - HeatPoint.Height / 2,
  102. HeatPoint.Width,
  103. HeatPoint.Height
  104. );
  105. // 创建图形路径
  106. using (GraphicsPath path = new GraphicsPath())
  107. {
  108. path.AddRectangle(rect);
  109. // 创建路径渐变笔刷
  110. using (PathGradientBrush brush = new PathGradientBrush(path))
  111. {
  112. var state = Canvas.Save();
  113. // 设置中心颜色(保留原始透明度)
  114. System.Drawing.Color centerColor = System.Drawing.Color.FromArgb(HeatPoint.Intensity, System.Drawing.Color.Black);
  115. brush.CenterColor = centerColor;
  116. //设置边缘颜色为完全透明
  117. System.Drawing.Color[] surroundColors = { System.Drawing.Color.Transparent };
  118. brush.SurroundColors = surroundColors;
  119. //设置渐变焦点(控制渐变范围)
  120. brush.FocusScales = new PointF(0.6f, 1f); // 控制虚化范围
  121. // 保存绘图状态
  122. // 坐标变换
  123. Canvas.TranslateTransform(HeatPoint.X, HeatPoint.Y);
  124. Canvas.RotateTransform(-HeatPoint.Rotation);
  125. Canvas.TranslateTransform(-HeatPoint.X, -HeatPoint.Y);
  126. // 填充路径
  127. Canvas.FillPath(brush, path);
  128. // 恢复绘图状态
  129. Canvas.Restore(state);
  130. }
  131. }
  132. }
  133. public static Bitmap Colorize(Bitmap Mask, byte Alpha)
  134. {
  135. Bitmap Output = new Bitmap(Mask.Width, Mask.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  136. Graphics Surface = Graphics.FromImage(Output);
  137. Surface.Clear(System.Drawing.Color.Transparent);
  138. // 构建一组颜色映射以将我们的灰度蒙版重新映射为全色
  139. // 接受一个 alpha 字节来指定输出图像的透明度
  140. ColorMap[] Colors = CreatePaletteIndex(Alpha);
  141. // 创建新的图像属性类来处理颜色重新映射
  142. // 注入我们的颜色映射数组来指示图像属性类如何进行着色
  143. ImageAttributes Remapper = new ImageAttributes();
  144. Remapper.SetRemapTable(Colors);
  145. // 使用新的颜色映射方案将我们的蒙版绘制到我们的内存位图工作表面上
  146. Surface.DrawImage(Mask, new System.Drawing.Rectangle(0, 0, Mask.Width, Mask.Height), 0, 0, Mask.Width, Mask.Height, GraphicsUnit.Pixel, Remapper);
  147. return Output;
  148. }
  149. private static ColorMap[] CreatePaletteIndex(byte Alpha)
  150. {
  151. ColorMap[] OutputMap = new ColorMap[256];
  152. Assembly myAssembly = Assembly.GetExecutingAssembly();
  153. Stream myStream = myAssembly.GetManifestResourceStream("Eink.AOIDefectAnalysis.Image.gradient-palette.jpg");
  154. Bitmap Palette = new Bitmap(myStream);
  155. for (int X = 0; X <= 255; X++)
  156. {
  157. OutputMap[X] = new ColorMap();
  158. OutputMap[X].OldColor = System.Drawing.Color.FromArgb(X, X, X);
  159. OutputMap[X].NewColor = System.Drawing.Color.FromArgb(Alpha, Palette.GetPixel(X, 0));
  160. }
  161. return OutputMap;
  162. }
  163. private void Form1_Load(object sender, EventArgs e)
  164. {
  165. dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
  166. Lot2DataList = db.Queryable<Lot2Lot3Model>().Where(x => x.create_time >= DateTime.Parse($"2025-02-26 08:00:00")).ToList();
  167. label2.Text = Lot2DataList.Count.ToString();
  168. }
  169. public byte[] BitmapToByteArray(Bitmap bmp)
  170. {
  171. BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
  172. ImageLockMode.ReadOnly, bmp.PixelFormat);
  173. // 计算Byte[]数组的大小
  174. int bytesCount = bitmapData.Stride * bitmapData.Height;
  175. byte[] bmp_byte = new byte[bytesCount];
  176. // 将像素数据复制到Byte[]数组
  177. System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, bmp_byte, 0, bytesCount);
  178. // 释放资源
  179. bmp.UnlockBits(bitmapData);
  180. return bmp_byte;
  181. }
  182. private Bitmap ByteArrayToBitmap(byte[] rgbaData, int width, int height)
  183. {
  184. var pixelFormat = PixelFormat.Format32bppArgb;
  185. Bitmap bitmap = new Bitmap(width, height, pixelFormat);
  186. BitmapData bitmapData = bitmap.LockBits(
  187. new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
  188. ImageLockMode.WriteOnly,
  189. pixelFormat);
  190. IntPtr intPtr = bitmapData.Scan0;
  191. System.Runtime.InteropServices.Marshal.Copy(rgbaData, 0, intPtr, rgbaData.Length);
  192. bitmap.UnlockBits(bitmapData);
  193. return bitmap;
  194. }
  195. private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  196. {
  197. HeatPoints.Clear();
  198. bMap = new Bitmap((int)panel1.Width, (int)panel1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  199. using (Graphics g = Graphics.FromImage(bMap))
  200. {
  201. g.Clear(System.Drawing.Color.White);
  202. }
  203. bMap_New = new Bitmap((int)panel1.Width, (int)panel1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  204. UpdateView();
  205. var dataGridViewRow = dataGridView1.CurrentRow;
  206. int index = dataGridViewRow.Cells["Index"].Value.ObjToInt();
  207. Lot2Lot3Dto? lot2lot3 = JsonConvert.DeserializeObject<Lot2Lot3Dto>(Lot2DataList[index - 1].content);
  208. if (lot2lot3 != null && lot2lot3.result == "NG")
  209. {
  210. generate_Picture(lot2lot3);
  211. }
  212. }
  213. private bool generate_Picture(Lot2Lot3Dto lot2lot3)
  214. {
  215. bool skipflag = false;
  216. foreach (var itemCheckData in lot2lot3.itemCheckDatas)
  217. {
  218. if (skipflag || itemCheckData.details.Count >= 150)
  219. {
  220. skipflag = true;
  221. continue;
  222. }
  223. foreach (var detail in itemCheckData.details)
  224. {
  225. try
  226. {
  227. int iX = (int)(Convert.ToDouble(detail.x) * 3 / 2 + drift_x);
  228. int iY = (int)(Convert.ToDouble(detail.y) * 3 / 2 + drift_y);
  229. int Width = (int)(Convert.ToDouble(detail.width) * 3 / 2);
  230. int iWidth = Width <= 5 ? 5 : Width;
  231. int Height = (int)(Convert.ToDouble(detail.height) * 3 / 2);
  232. int iHeight = Height < 5 ? 5 : Height;
  233. byte iIntense = (byte)Intense;
  234. float iRotation = float.Parse(detail.rotation);
  235. HeatPoints.Add(new HeatPoint(iX, iY, iWidth, iHeight, iIntense, iRotation));
  236. }
  237. catch (Exception)
  238. {
  239. continue;
  240. }
  241. }
  242. }
  243. if (!skipflag)
  244. {
  245. UpdateView();
  246. HeatPoints.Clear();
  247. }
  248. return skipflag;
  249. }
  250. private void Reset()
  251. {
  252. HeatPoints.Clear();
  253. dataGridView1.Rows.Clear();
  254. bMap = new Bitmap((int)panel1.Width, (int)panel1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  255. using (Graphics g = Graphics.FromImage(bMap))
  256. {
  257. g.Clear(System.Drawing.Color.White);
  258. }
  259. bMap_New = new Bitmap((int)panel1.Width, (int)panel1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
  260. }
  261. }
  262. }