版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 2 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using Newtonsoft.Json.Linq;
  2. using ProductionControl.Device;
  3. using ProductionControl.Utils;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Drawing.Imaging;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. namespace ProductionControl.UI
  17. {
  18. public partial class FrmScannerShow2 : Form
  19. {
  20. private Size size;
  21. #region pic缩放变量
  22. private double ratio = 1; // 图片的起始显示比例
  23. private double ratioStep = 0.1;
  24. private Size pic_size;
  25. private int xPos;
  26. private int yPos;
  27. #endregion
  28. ScannerDev scannerDev;
  29. public IntPtr picHwnd
  30. {
  31. get { return this.pictureBox1.Handle; }
  32. }
  33. public FrmScannerShow2(Size _size, ScannerDev dev = null)
  34. {
  35. InitializeComponent();
  36. scannerDev = dev;
  37. size = _size;
  38. this.SizeChanged += FrmScannerShow2_SizeChanged;
  39. FrmScannerShow2_SizeChanged(null,null);
  40. //this.ClientSize = new Size(this.ClientSize.Width,
  41. // (int)(this.ClientSize.Width * (_size.Height * 1.0f / _size.Width)));
  42. //
  43. //this.Width = Screen.PrimaryScreen.WorkingArea.Width / 2;
  44. //this.Location = new Point(0, 0);
  45. }
  46. private void FrmScannerShow2_SizeChanged(object sender, EventArgs e)
  47. {
  48. var newSize = Util.getNewSize(this.ClientSize, new Size(size.Width * 100, size.Height * 100));
  49. this.pnlPic.Size = newSize;
  50. if (this.pnlPic.Width < this.ClientSize.Width)
  51. this.pnlPic.Left = (this.ClientSize.Width - this.pnlPic.Width) / 2;
  52. if (this.pnlPic.Height < this.ClientSize.Height)
  53. this.pnlPic.Top = (this.ClientSize.Height - this.pnlPic.Height) / 2;
  54. }
  55. private void FrmPhotoShow_Load(object sender, EventArgs e)
  56. {
  57. }
  58. private void FrmScannerShow2_FormClosed(object sender, FormClosedEventArgs e)
  59. {
  60. if (scannerDev != null)
  61. {
  62. scannerDev.stop();
  63. scannerDev.close();
  64. scannerDev = null;
  65. }
  66. }
  67. //----------
  68. #region 图片缩放控制
  69. private void reloadPic(Bitmap bmp)
  70. {
  71. var newSize = Util.getNewSize(this.ClientSize, new Size(bmp.Size.Width * 100, bmp.Size.Height * 100));
  72. this.pnlPic.Size = newSize;
  73. if (this.pnlPic.Width < this.ClientSize.Width)
  74. this.pnlPic.Left = (this.ClientSize.Width - this.pnlPic.Width) / 2;
  75. if (this.pnlPic.Height < this.ClientSize.Height)
  76. this.pnlPic.Top = (this.ClientSize.Height - this.pnlPic.Height) / 2;
  77. ratio = 1.0;// 图片的起始显示比例
  78. Size size = bmp.Size;
  79. //this.pnlPic.ClientSize = new Size(this.pnlPic.ClientSize.Width,
  80. // (int)(this.pnlPic.ClientSize.Width * (size.Height * 1.0f / size.Width)));
  81. this.pictureBox1.Size = this.pnlPic.ClientSize;
  82. this.pictureBox1.Location = new Point(0, 0);
  83. this.pictureBox1.Image = bmp;//del
  84. pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  85. pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
  86. pictureBox1.MouseMove += pictureBox1_MouseMove;
  87. pictureBox1.MouseDown += pictureBox1_MouseDown;
  88. this.ActiveControl = this.pictureBox1; // 设置焦点
  89. pic_size = this.pnlPic.ClientSize;
  90. }
  91. private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
  92. {
  93. Point pictureBoxPoint = this.PointToClient(Cursor.Position);
  94. //this.Text = $"{e.X}:{e.Y}; {pictureBoxPoint.X}:{pictureBoxPoint.Y}; {pictureBox1.Left}:{pictureBox1.Top}; " +
  95. // $"{this.pictureBox1.Width}:{this.pictureBox1.Height}; {this.Width}:{this.Height}; " +
  96. // $"Cursor:{Cursor.Position.X}:{Cursor.Position.Y}";
  97. if (e.Delta > 0)
  98. {
  99. ratio += ratioStep;
  100. if (ratio > 100) // 放大上限
  101. ratio = 100;
  102. else
  103. {
  104. int x = (int)(pictureBox1.Left - e.X * (e.X * 1.0d / pictureBox1.Width * ratioStep) + 0.5);
  105. int y = (int)(pictureBox1.Top - e.Y * (e.Y * 1.0d / pictureBox1.Height * ratioStep) + 0.5);
  106. //this.Text = $"{pictureBox1.Width}-{pic_size.Width};{ratio},{pictureBox1.Left}-{e.X}* 比例:{e.X*1.0d/pictureBox1.Width}={e.X}/{pictureBox1.Width} * {(ratioStep)}={x} | " +
  107. // $"{pictureBox1.Top}-{e.Y}* {e.Y * 1.0f / pictureBox1.Height * (ratio - 1.0d)}={y}";
  108. this.changePictureBoxSize(new Point(x, y), ratio);
  109. }
  110. }
  111. else if (ratio - ratioStep >= 1)
  112. {
  113. ratio -= ratioStep;
  114. //if (ratio < 0.1) // 放大下限
  115. // ratio = 0.1;
  116. //else
  117. int x = (int)(pictureBox1.Left + e.X * (e.X * 1.0d / pictureBox1.Width * ratioStep) + 0.5);
  118. int y = (int)(pictureBox1.Top + e.Y * (e.Y * 1.0d / pictureBox1.Height * ratioStep) + 0.5);
  119. this.changePictureBoxSize(new Point(x, y), ratio);
  120. }
  121. }
  122. private void changePictureBoxSize(Point location, double ratio)
  123. {
  124. var picSize = pictureBox1.Size;
  125. picSize.Width = Convert.ToInt32(pic_size.Width * ratio);
  126. picSize.Height = Convert.ToInt32(pic_size.Height * ratio);
  127. pictureBox1.Size = picSize;
  128. if (location.X > 0) location.X = 0;
  129. if (location.Y > 0) location.Y = 0;
  130. if (picSize.Width + location.X < this.pnlPic.ClientSize.Width) location.X = -(picSize.Width - this.pnlPic.ClientSize.Width);
  131. if (picSize.Height + location.Y < this.pnlPic.ClientSize.Height) location.Y = -(picSize.Height - this.pnlPic.ClientSize.Height);
  132. //Point location = new Point();
  133. //location.X = (this.ClientSize.Width - this.pictureBox1.Width) / 2;
  134. //location.Y = (this.ClientSize.Height - this.pictureBox1.Height) / 2;
  135. this.pictureBox1.Location = location;
  136. }
  137. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  138. {
  139. try
  140. {
  141. // 鼠标按下拖拽图片
  142. if (e.Button == MouseButtons.Left)
  143. {
  144. //this.Text = $"{e.X}:{e.Y};{xPos}:{yPos}";
  145. // 限制拖拽出框
  146. if (pictureBox1.Width > this.pnlPic.ClientSize.Width
  147. || pictureBox1.Height > this.pnlPic.ClientSize.Height)
  148. {
  149. int moveX = e.X - xPos;
  150. int moveY = e.Y - yPos;
  151. if ((pictureBox1.Left + moveX) <= 0
  152. && (pictureBox1.Top + moveY) <= 0
  153. && (pictureBox1.Right + moveX) >= this.pnlPic.ClientSize.Width
  154. && (pictureBox1.Bottom + moveY) >= this.pnlPic.ClientSize.Height)
  155. {
  156. pictureBox1.Left += moveX;//设置x坐标.
  157. pictureBox1.Top += moveY;//设置y坐标.
  158. }
  159. }
  160. }
  161. }
  162. catch (Exception dd)
  163. {
  164. MessageBox.Show(dd.Message);
  165. }
  166. }
  167. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  168. {
  169. xPos = e.X;//当前x坐标.
  170. yPos = e.Y;//当前y坐标.
  171. }
  172. #endregion
  173. }
  174. }