革博士程序V1仓库
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

183 wiersze
6.9 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace LeatherApp.UIExtend
  11. {
  12. public partial class UCImageView : UserControl
  13. {
  14. private double ratioStep = 0.1;
  15. public UCImageView()
  16. {
  17. InitializeComponent();
  18. pictureBox1.Location = new Point(0, 0);
  19. pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  20. pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
  21. pictureBox1.MouseMove += pictureBox1_MouseMove;
  22. pictureBox1.MouseDown += pictureBox1_MouseDown;
  23. }
  24. #region private tool
  25. /// <summary>
  26. /// 获取适合大小
  27. /// </summary>
  28. /// <param name="sourceSize"></param>
  29. /// <param name="targetSize"></param>
  30. /// <returns></returns>
  31. private Size getNewSize(Size sourceSize, Size targetSize)
  32. {
  33. if (sourceSize.Width <= targetSize.Width && sourceSize.Height <= targetSize.Height)
  34. return sourceSize;
  35. int num = sourceSize.Width / targetSize.Width;
  36. int num2 = sourceSize.Height / targetSize.Height;
  37. int num3 = (num < num2) ? num2 : num;
  38. return new Size(sourceSize.Width / num3, sourceSize.Height / num3);
  39. }
  40. /// <summary>
  41. /// 等比计算宽高
  42. /// </summary>
  43. /// <param name="sourceSize"></param>
  44. /// <param name="targetSize"></param>
  45. /// <returns></returns>
  46. private SizeF getNewSize(SizeF sourceSize, SizeF targetSize)
  47. {
  48. if (sourceSize.Width <= targetSize.Width && sourceSize.Height <= targetSize.Height)
  49. return sourceSize;
  50. float num = sourceSize.Width / targetSize.Width;
  51. float num2 = sourceSize.Height / targetSize.Height;
  52. float num3 = (num < num2) ? num2 : num;
  53. return new SizeF(sourceSize.Width / num3, sourceSize.Height / num3);
  54. }
  55. #endregion
  56. public void clear()
  57. {
  58. pictureBox1.Image = null;
  59. pictureBox1.Refresh();
  60. pictureBox1.Visible = false;
  61. }
  62. public void loadImage(Image img)
  63. {
  64. if(img == null) { clear(); return; }
  65. pictureBox1.Visible = true;
  66. pictureBox1.Size = ClientSize;
  67. pictureBox1.Location = new Point(0, 0);
  68. pictureBox1.Image = img;//del
  69. pictureBox1.Refresh();
  70. //ActiveControl = pictureBox1; // 设置焦点
  71. }
  72. private void UCImageView_Load(object sender, EventArgs e)
  73. {
  74. }
  75. private void checkBorderLine()
  76. {
  77. if (pictureBox1.Width < ClientSize.Width) pictureBox1.Width = ClientSize.Width;
  78. if (pictureBox1.Height < ClientSize.Height) pictureBox1.Height = ClientSize.Height;
  79. if (pictureBox1.Right < ClientSize.Width) pictureBox1.Left += ClientSize.Width - pictureBox1.Right;
  80. if (pictureBox1.Bottom < ClientSize.Height) pictureBox1.Top += ClientSize.Height - pictureBox1.Bottom;
  81. if (pictureBox1.Left > 0) pictureBox1.Left = 0;
  82. if (pictureBox1.Top > 0) pictureBox1.Top = 0;
  83. }
  84. public void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
  85. {
  86. if (pictureBox1.Image == null) return;
  87. int x = e.Location.X;
  88. int y = e.Location.Y;
  89. double oldW = pictureBox1.Width;
  90. double oldH = pictureBox1.Height;
  91. int width,height;
  92. double ratio;
  93. if (e.Delta > 0) //放大
  94. {
  95. ratio = 1 + ratioStep;
  96. width = Convert.ToInt32(pictureBox1.Width * ratio);
  97. height = Convert.ToInt32(pictureBox1.Height * ratio);
  98. //if (width * height > 45800000)
  99. // return;
  100. if (width / ClientSize.Width > 10)
  101. return;
  102. pictureBox1.Width = width;
  103. pictureBox1.Height = height;
  104. }
  105. if (e.Delta < 0) //缩小
  106. {
  107. //防止一直缩成负值
  108. if (pictureBox1.Width < this.Width || pictureBox1.Height < this.Height)
  109. return;
  110. ratio = 1.0 - ratioStep;
  111. width = (int)(pictureBox1.Width * ratio);
  112. height= (int)(pictureBox1.Height * ratio);
  113. pictureBox1.Width = width;
  114. pictureBox1.Height = height;
  115. }
  116. this.Text = $"{e.X},{e.Y}";
  117. //求因缩放产生的位移,进行补偿,实现锚点缩放的效果
  118. int VX = (int)((oldW - pictureBox1.Width) / oldW * x);
  119. int VY = (int)((oldH - pictureBox1.Height) / oldH * y);
  120. pictureBox1.Location = new Point(pictureBox1.Location.X + VX, pictureBox1.Location.Y + VY);
  121. checkBorderLine();
  122. }
  123. //移动
  124. private int xPos;
  125. private int yPos;
  126. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  127. {
  128. if (pictureBox1.Image == null) return;
  129. try
  130. {
  131. // 鼠标按下拖拽图片
  132. if (e.Button == MouseButtons.Left)
  133. {
  134. this.Parent.Text = $"{e.X}:{e.Y};{xPos}:{yPos}";
  135. // 限制拖拽出框
  136. if (pictureBox1.Width > ClientSize.Width
  137. || pictureBox1.Height > ClientSize.Height)
  138. {
  139. int moveX = e.X - xPos;
  140. int moveY = e.Y - yPos;
  141. if ((pictureBox1.Left + moveX) <= 0
  142. && (pictureBox1.Top + moveY) <= 0
  143. && (pictureBox1.Right + moveX) >= ClientSize.Width
  144. && (pictureBox1.Bottom + moveY) >= ClientSize.Height)
  145. {
  146. pictureBox1.Left += moveX;//设置x坐标.
  147. pictureBox1.Top += moveY;//设置y坐标.
  148. checkBorderLine();
  149. }
  150. }
  151. }
  152. }
  153. catch (Exception dd)
  154. {
  155. MessageBox.Show(dd.Message);
  156. }
  157. }
  158. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  159. {
  160. if (pictureBox1.Image == null) return;
  161. xPos = e.X;//当前x坐标.
  162. yPos = e.Y;//当前y坐标.
  163. }
  164. private void UCImageView_ClientSizeChanged(object sender, EventArgs e)
  165. {
  166. checkBorderLine();
  167. }
  168. }
  169. }