Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

UCImageView.cs 6.8 KiB

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