版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

277 строки
11 KiB

  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace ProductionControl
  15. {
  16. public partial class FrmPhoto : Form
  17. {
  18. private Control.ControlCollection picBoxList;
  19. private int picIndex;
  20. private int tagIndex;
  21. private double Xmm,Ymm;
  22. private Bitmap currBmp;
  23. //
  24. private double ratio = 1; // 图片的起始显示比例
  25. private double ratioStep = 0.1;
  26. private Size pic_size;
  27. private int xPos;
  28. private int yPos;
  29. public FrmPhoto(Control.ControlCollection picBoxColl,int index)
  30. {
  31. InitializeComponent();
  32. this.picBoxList = picBoxColl;
  33. this.picIndex = index;
  34. //
  35. this.Width = Screen.PrimaryScreen.WorkingArea.Width / 2;
  36. this.Location = new Point(0, 0);
  37. //size = _size;
  38. //this.ClientSize = new Size(this.ClientSize.Width,
  39. // (int)(this.ClientSize.Width * (size.Height * 1.0f / size.Width)));
  40. //this.pictureBox1.Size = this.ClientSize;
  41. //this.pictureBox1.Location = new Point(0, 0);
  42. }
  43. protected override bool ProcessDialogKey(Keys keyData)
  44. {
  45. if (keyData == Keys.Up || keyData == Keys.Down || keyData == Keys.Left || keyData == Keys.Right)
  46. return false;
  47. else
  48. return base.ProcessDialogKey(keyData);
  49. }
  50. private void FrmPhoto_Load(object sender, EventArgs e)
  51. {
  52. PictureBox picBox = picBoxList[this.picIndex * 2] as PictureBox;
  53. Size size = picBox.Image.Size;
  54. this.panel1.ClientSize = new Size(this.panel1.ClientSize.Width,
  55. (int)(this.panel1.ClientSize.Width * (size.Height * 1.0f / size.Width)));
  56. this.pictureBox1.Size = this.panel1.ClientSize;
  57. this.pictureBox1.Location = new Point(0, 0);
  58. //this.pictureBox1.Image = bmp;
  59. pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  60. pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
  61. pictureBox1.MouseMove += pictureBox1_MouseMove;
  62. pictureBox1.MouseDown += pictureBox1_MouseDown;
  63. this.ActiveControl = this.pictureBox1; // 设置焦点
  64. pic_size = this.panel1.ClientSize;
  65. //
  66. //MessageBox.Show("菜单栏厚度" + SystemInformation.MenuHeight.ToString()); //获取标准菜单栏的厚度
  67. //MessageBox.Show("标题栏厚度" + SystemInformation.CaptionHeight.ToString()); //获取标准标题栏的厚度
  68. this.Height = panel1.Bottom + SystemInformation.CaptionHeight + SystemInformation.MenuHeight + statusStrip1.Height;
  69. this.Left = (Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2;
  70. this.Top = (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2;
  71. }
  72. private void FrmPhoto_Shown(object sender, EventArgs e)
  73. {
  74. showPic(this.picIndex);
  75. }
  76. private void tsbtnPre_Click(object sender, EventArgs e)
  77. {
  78. showPic(--this.picIndex);
  79. }
  80. private void tsbtnNext_Click(object sender, EventArgs e)
  81. {
  82. showPic(++this.picIndex);
  83. }
  84. private void FrmPhoto_KeyDown(object sender, KeyEventArgs e)
  85. {
  86. switch (e.KeyCode)
  87. {
  88. case Keys.Up:
  89. case Keys.Left:
  90. if (!this.tsbtnPre.Enabled) return;
  91. tsbtnPre_Click(null, null);
  92. break;
  93. case Keys.Down:
  94. case Keys.Right:
  95. if (!this.tsbtnNext.Enabled) return;
  96. tsbtnNext_Click(null, null);
  97. break;
  98. }
  99. }
  100. private void showPic(int index)
  101. {
  102. try
  103. {
  104. PictureBox picBox = picBoxList[index*2] as PictureBox;
  105. string[] tags = picBox.Tag.ToString().Split(',');
  106. this.tagIndex=Convert.ToInt32(tags[0]);
  107. this.Xmm = Convert.ToDouble(tags[1]);
  108. this.Ymm = Convert.ToDouble(tags[2]);
  109. //tsStateMsg.Text = $"索引:{tagIndex}, X:{Xmm}mm, Y:{Ymm}mm";
  110. tsStateStep.Text = $"第{index+1}/{picBoxList.Count/2}张";
  111. currBmp = (Bitmap)picBox.Image;
  112. this.pictureBox1.Image = (Bitmap)currBmp.Clone() ;
  113. //
  114. CheckBox cb = picBoxList[index * 2 + 1] as CheckBox;
  115. this.chkAdd.Checked = cb.Checked;
  116. //
  117. this.tsbtnPre.Enabled = (index > 0);
  118. this.tsbtnNext.Enabled = (index < picBoxList.Count / 2 - 1);
  119. }
  120. catch (Exception ex)
  121. {
  122. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  123. }
  124. }
  125. private void tsbtnSave_Click(object sender, EventArgs e)
  126. {
  127. try
  128. {
  129. if (Config.Defect_Small_SavePath.Trim() == "")
  130. throw new Exception("请先到系统设置中设置缺陷小图保存路径!");
  131. //
  132. string savePath = "";
  133. if (Config.Defect_Small_SavePath != "")
  134. {
  135. savePath = Config.Defect_Small_SavePath + "\\" + DateTime.Now.ToString("yyyyMMdd");
  136. if (!Directory.Exists(savePath))
  137. Directory.CreateDirectory(savePath);
  138. }
  139. //save
  140. PictureBox picBox = picBoxList[this.picIndex * 2] as PictureBox;
  141. string[] tags = picBox.Tag.ToString().Split(',');
  142. int tagIndex = Convert.ToInt32(tags[0]);
  143. //Xmm = Convert.ToDouble(tags[1]);
  144. //Ymm = Convert.ToDouble(tags[2]);
  145. CheckBox cb = picBoxList[this.picIndex * 2 + 1] as CheckBox;
  146. ((Bitmap)cb.Tag).Save($"{savePath}\\X{Xmm}_Y{Ymm}_K{tagIndex}_{DateTime.Now.Ticks}.bmp", ImageFormat.Bmp);
  147. //OpenCvSharp.Cv2.ImWrite($"{savePath}\\X{Xmm}_Y{Ymm}_K{tagIndex}_{DateTime.Now.Ticks}.bmp", ((OpenCvSharp.Mat)cb.Tag));//保存图片
  148. MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  149. }
  150. catch (Exception ex)
  151. {
  152. MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  153. }
  154. }
  155. private void tsbtnClose_Click(object sender, EventArgs e)
  156. {
  157. this.Close();
  158. }
  159. private void chkAdd_CheckedChanged(object sender, EventArgs e)
  160. {
  161. CheckBox cb = picBoxList[picIndex * 2 + 1] as CheckBox;
  162. cb.Checked = this.chkAdd.Checked;
  163. }
  164. //----------
  165. private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
  166. {
  167. Point pictureBoxPoint = this.PointToClient(Cursor.Position);
  168. //this.Text = $"{e.X}:{e.Y}; {pictureBoxPoint.X}:{pictureBoxPoint.Y}; {pictureBox1.Left}:{pictureBox1.Top}; " +
  169. // $"{this.pictureBox1.Width}:{this.pictureBox1.Height}; {this.Width}:{this.Height}; " +
  170. // $"Cursor:{Cursor.Position.X}:{Cursor.Position.Y}";
  171. if (e.Delta > 0)
  172. {
  173. ratio += ratioStep;
  174. if (ratio > 100) // 放大上限
  175. ratio = 100;
  176. else
  177. {
  178. int x = (int)(pictureBox1.Left - e.X * (e.X * 1.0d / pictureBox1.Width * ratioStep) + 0.5);
  179. int y = (int)(pictureBox1.Top - e.Y * (e.Y * 1.0d / pictureBox1.Height * ratioStep) + 0.5);
  180. //this.Text = $"{pictureBox1.Width}-{pic_size.Width};{ratio},{pictureBox1.Left}-{e.X}* 比例:{e.X*1.0d/pictureBox1.Width}={e.X}/{pictureBox1.Width} * {(ratioStep)}={x} | " +
  181. // $"{pictureBox1.Top}-{e.Y}* {e.Y * 1.0f / pictureBox1.Height * (ratio - 1.0d)}={y}";
  182. this.changePictureBoxSize(new Point(x, y), ratio);
  183. }
  184. }
  185. else if (ratio - ratioStep >= 1)
  186. {
  187. ratio -= ratioStep;
  188. //if (ratio < 0.1) // 放大下限
  189. // ratio = 0.1;
  190. //else
  191. int x = (int)(pictureBox1.Left + e.X * (e.X * 1.0d / pictureBox1.Width * ratioStep) + 0.5);
  192. int y = (int)(pictureBox1.Top + e.Y * (e.Y * 1.0d / pictureBox1.Height * ratioStep) + 0.5);
  193. this.changePictureBoxSize(new Point(x, y), ratio);
  194. }
  195. }
  196. private void changePictureBoxSize(Point location, double ratio)
  197. {
  198. var picSize = pictureBox1.Size;
  199. picSize.Width = Convert.ToInt32(pic_size.Width * ratio);
  200. picSize.Height = Convert.ToInt32(pic_size.Height * ratio);
  201. pictureBox1.Size = picSize;
  202. if (location.X > 0) location.X = 0;
  203. if (location.Y > 0) location.Y = 0;
  204. if (picSize.Width + location.X < this.panel1.ClientSize.Width) location.X = -(picSize.Width - this.panel1.ClientSize.Width);
  205. if (picSize.Height + location.Y < this.panel1.ClientSize.Height) location.Y = -(picSize.Height - this.panel1.ClientSize.Height);
  206. //Point location = new Point();
  207. //location.X = (this.ClientSize.Width - this.pictureBox1.Width) / 2;
  208. //location.Y = (this.ClientSize.Height - this.pictureBox1.Height) / 2;
  209. this.pictureBox1.Location = location;
  210. }
  211. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  212. {
  213. try
  214. {
  215. // 鼠标按下拖拽图片
  216. if (e.Button == MouseButtons.Left)
  217. {
  218. //this.Text = $"{e.X}:{e.Y};{xPos}:{yPos}";
  219. // 限制拖拽出框
  220. if (pictureBox1.Width > this.panel1.ClientSize.Width
  221. || pictureBox1.Height > this.panel1.ClientSize.Height)
  222. {
  223. int moveX = e.X - xPos;
  224. int moveY = e.Y - yPos;
  225. if ((pictureBox1.Left + moveX) <= 0
  226. && (pictureBox1.Top + moveY) <= 0
  227. && (pictureBox1.Right + moveX) >= this.panel1.ClientSize.Width
  228. && (pictureBox1.Bottom + moveY) >= this.panel1.ClientSize.Height)
  229. {
  230. pictureBox1.Left += moveX;//设置x坐标.
  231. pictureBox1.Top += moveY;//设置y坐标.
  232. }
  233. }
  234. }
  235. }
  236. catch (Exception dd)
  237. {
  238. MessageBox.Show(dd.Message);
  239. }
  240. }
  241. private void toolStripSeparator2_Click(object sender, EventArgs e)
  242. {
  243. }
  244. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  245. {
  246. xPos = e.X;//当前x坐标.
  247. yPos = e.Y;//当前y坐标.
  248. }
  249. }
  250. }