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

217 строки
8.5 KiB

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