革博士程序V1仓库
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
7.1 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. pictureBox1.Width = this.Width;
  72. pictureBox1.Height = this.Height;
  73. }
  74. private void UCImageView_Load(object sender, EventArgs e)
  75. {
  76. pictureBox1.Width = this.Width;
  77. pictureBox1.Height = this.Height;
  78. }
  79. private void checkBorderLine()
  80. {
  81. if (pictureBox1.Width < ClientSize.Width) pictureBox1.Width = ClientSize.Width;
  82. if (pictureBox1.Height < ClientSize.Height) pictureBox1.Height = ClientSize.Height;
  83. if (pictureBox1.Right < ClientSize.Width) pictureBox1.Left += ClientSize.Width - pictureBox1.Right;
  84. if (pictureBox1.Bottom < ClientSize.Height) pictureBox1.Top += ClientSize.Height - pictureBox1.Bottom;
  85. if (pictureBox1.Left > 0) pictureBox1.Left = 0;
  86. if (pictureBox1.Top > 0) pictureBox1.Top = 0;
  87. }
  88. public void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
  89. {
  90. if (pictureBox1.Image == null) return;
  91. int x = e.Location.X;
  92. int y = e.Location.Y;
  93. double oldW = pictureBox1.Width;
  94. double oldH = pictureBox1.Height;
  95. int width,height;
  96. double ratio;
  97. if (e.Delta > 0) //放大
  98. {
  99. ratio = 1 + ratioStep;
  100. width = Convert.ToInt32(pictureBox1.Width * ratio);
  101. height = Convert.ToInt32(pictureBox1.Height * ratio);
  102. //if (width * height > 45800000)
  103. // return;
  104. if (width / ClientSize.Width > 10)
  105. return;
  106. pictureBox1.Width = width;
  107. pictureBox1.Height = height;
  108. }
  109. if (e.Delta < 0) //缩小
  110. {
  111. //防止一直缩成负值
  112. if (pictureBox1.Width < this.Width || pictureBox1.Height < this.Height)
  113. return;
  114. ratio = 1.0 - ratioStep;
  115. width = (int)(pictureBox1.Width * ratio);
  116. height= (int)(pictureBox1.Height * ratio);
  117. pictureBox1.Width = width;
  118. pictureBox1.Height = height;
  119. }
  120. this.Text = $"{e.X},{e.Y}";
  121. //求因缩放产生的位移,进行补偿,实现锚点缩放的效果
  122. int VX = (int)((oldW - pictureBox1.Width) / oldW * x);
  123. int VY = (int)((oldH - pictureBox1.Height) / oldH * y);
  124. pictureBox1.Location = new Point(pictureBox1.Location.X + VX, pictureBox1.Location.Y + VY);
  125. checkBorderLine();
  126. }
  127. //移动
  128. private int xPos;
  129. private int yPos;
  130. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  131. {
  132. if (pictureBox1.Image == null) return;
  133. try
  134. {
  135. // 鼠标按下拖拽图片
  136. if (e.Button == MouseButtons.Left)
  137. {
  138. this.Parent.Text = $"{e.X}:{e.Y};{xPos}:{yPos}";
  139. // 限制拖拽出框
  140. if (pictureBox1.Width > ClientSize.Width
  141. || pictureBox1.Height > ClientSize.Height)
  142. {
  143. int moveX = e.X - xPos;
  144. int moveY = e.Y - yPos;
  145. if ((pictureBox1.Left + moveX) <= 0
  146. && (pictureBox1.Top + moveY) <= 0
  147. && (pictureBox1.Right + moveX) >= ClientSize.Width
  148. && (pictureBox1.Bottom + moveY) >= ClientSize.Height)
  149. {
  150. pictureBox1.Left += moveX;//设置x坐标.
  151. pictureBox1.Top += moveY;//设置y坐标.
  152. checkBorderLine();
  153. }
  154. }
  155. }
  156. }
  157. catch (Exception dd)
  158. {
  159. MessageBox.Show(dd.Message);
  160. }
  161. }
  162. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  163. {
  164. if (pictureBox1.Image == null) return;
  165. xPos = e.X;//当前x坐标.
  166. yPos = e.Y;//当前y坐标.
  167. }
  168. private void UCImageView_ClientSizeChanged(object sender, EventArgs e)
  169. {
  170. checkBorderLine();
  171. }
  172. }
  173. }