PicturePreviewAdjustControl.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using Resources;
  2. using System;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. namespace SmartCoalApplication.Core.CustomControl
  6. {
  7. public partial class PicturePreviewAdjustControl : BaseUserControl
  8. {
  9. System.Drawing.Point mouseDownPoint;
  10. bool isSelected = false;
  11. public bool isShown = false;
  12. /// <summary>
  13. /// 是否使用原图取色
  14. /// </summary>
  15. private bool item3Checked;
  16. /// <summary>
  17. /// 原图取色-鼠标点击事件
  18. /// </summary>
  19. public event MouseEventHandler item3MouseDown;
  20. private void InitializeLanguageText()
  21. {
  22. this.box_Picture.Text = PdnResources.GetString("Menu.Preview.text");
  23. }
  24. /// <summary>
  25. /// 图像处理的预览Control
  26. /// 包括放大、缩小、选择、移动
  27. /// </summary>
  28. public PicturePreviewAdjustControl()
  29. {
  30. InitializeComponent();
  31. if (!DesignMode)
  32. {
  33. InitializeLanguageText();
  34. this.ZoomInButton.Image = PdnResources.GetImageResource("Icons.MenuViewZoomInIcon.png").Reference;
  35. this.ZoomOutButton.Image = PdnResources.GetImageResource("Icons.MenuViewZoomOutIcon.png").Reference;
  36. this.MoveSelectionButton.Image = PdnResources.GetImageResource("Icons.MoveSelectionToolIcon.png").Reference;
  37. this.PanButton.Image = PdnResources.GetImageResource("Icons.PanToolIcon.png").Reference;
  38. }
  39. }
  40. //放大
  41. private void ZoomInButton_Click(object sender, EventArgs e)
  42. {
  43. ZoomInImpl();
  44. }
  45. //放大
  46. private void ZoomInImpl()
  47. {
  48. double eDelta = pictureBox1.Height * 0.25;
  49. double scale = 1;
  50. if (pictureBox1.Height > 0)
  51. {
  52. scale = (double)pictureBox1.Width / (double)pictureBox1.Height;
  53. }
  54. pictureBox1.Width += (int)(eDelta * scale * 2);
  55. pictureBox1.Height += (int)(eDelta * 2);
  56. pictureBox1.Left -= (int)(eDelta * scale * 1);
  57. pictureBox1.Top -= (int)(eDelta * 1);
  58. }
  59. //缩小
  60. private void ZoomOutButton_Click(object sender, EventArgs e)
  61. {
  62. double eDelta = pictureBox1.Height * 0.16;// 0.25;
  63. double scale = 1;
  64. if (pictureBox1.Height > 0)
  65. {
  66. scale = (double)pictureBox1.Width / (double)pictureBox1.Height;
  67. }
  68. pictureBox1.Width -= (int)(eDelta * scale * 2);
  69. pictureBox1.Height -= (int)(eDelta * 2);
  70. pictureBox1.Left += (int)(eDelta * scale * 1);
  71. pictureBox1.Top += (int)(eDelta * 1);
  72. }
  73. //选择
  74. private void MoveSelectionButton_Click(object sender, EventArgs e)
  75. {
  76. if (this.item3Checked)
  77. {
  78. pictureBox1.Cursor = Cursors.Cross;
  79. }
  80. else
  81. {
  82. pictureBox1.Cursor = Cursors.Default;
  83. }
  84. }
  85. //移动
  86. private void PanButton_Click(object sender, EventArgs e)
  87. {
  88. pictureBox1.Cursor = new Cursor(PdnResources.GetResourceStream("Cursors.PanToolCursor.cur"));
  89. //Cursors.Hand;
  90. }
  91. private void pictureBox1_Click(object sender, EventArgs e)
  92. {
  93. }
  94. private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
  95. {
  96. double scale = 1;
  97. if (pictureBox1.Height > 0)
  98. {
  99. scale = (double)pictureBox1.Width / (double)pictureBox1.Height;
  100. }
  101. if (pictureBox1.Width + (int)(e.Delta * scale) > 0 && pictureBox1.Height + e.Delta > 0)
  102. {
  103. pictureBox1.Width += (int)(e.Delta * scale);
  104. pictureBox1.Height += e.Delta;
  105. pictureBox1.Left -= (int)(e.Delta * scale * 0.5);
  106. pictureBox1.Top -= (int)(e.Delta * 0.5);
  107. }
  108. }
  109. //在MouseDown处获知鼠标是否按下,并记录下此时的鼠标坐标值;
  110. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  111. {
  112. if (e.Button == MouseButtons.Left && pictureBox1.Cursor == Cursors.Hand)
  113. {
  114. mouseDownPoint.X = Cursor.Position.X; //注:全局变量mouseDownPoint前面已定义为Point类型
  115. mouseDownPoint.Y = Cursor.Position.Y;
  116. isSelected = true;
  117. }
  118. else if (this.item3Checked && e.Button == MouseButtons.Left && pictureBox1.Cursor == Cursors.Cross)
  119. {
  120. //获取当前位置坐标
  121. Rectangle rc = this.pictureBox1.ClientRectangle;
  122. int width = (int)this.pictureBox1.Width;// (this.compositionSurface.Width * this.scaleFactor.Ratio);
  123. int height = (int)this.pictureBox1.Height;// (this.compositionSurface.Height * this.scaleFactor.Ratio);
  124. int x = 0;
  125. int y = 0;
  126. double radio = 1;
  127. //垂直方向有空白
  128. if ((this.pictureBox1.Image.Width / (double)this.pictureBox1.Width) > (this.pictureBox1.Image.Height / (double)this.pictureBox1.Height))
  129. {
  130. y = (int)((height - width * this.pictureBox1.Image.Height / (double)this.pictureBox1.Image.Width) / 2.0);
  131. radio = this.pictureBox1.Image.Width / (double)this.pictureBox1.Width;
  132. }
  133. else if ((this.pictureBox1.Image.Width / (double)this.pictureBox1.Width) < (this.pictureBox1.Image.Height / (double)this.pictureBox1.Height))
  134. {
  135. x = (int)((width - height * this.pictureBox1.Image.Width / (double)this.pictureBox1.Image.Height) / 2.0);
  136. radio = this.pictureBox1.Image.Height / (double)this.pictureBox1.Height;
  137. }
  138. //int x = (rc.Width < width) ? this.pictureBox1.Location.X/*this.panel.AutoScrollPosition.X + offsetHalfW*/ : (rc.Width - width) / 2;
  139. //int y = (rc.Height < height) ? this.pictureBox1.Location.Y/*this.panel.AutoScrollPosition.Y + offsetHalfH*/ : (rc.Height - height) / 2;
  140. Point point = e.Location;
  141. point.X -= x;
  142. point.Y -= y;
  143. Point newPoint = new Point((int)((long)point.X * radio), (int)((long)point.Y * radio));
  144. this.item3MouseDown?.Invoke(sender, new MouseEventArgs(e.Button, e.Clicks, newPoint.X, newPoint.Y, e.Delta));
  145. }
  146. }
  147. public void selectColorMode(bool selectColor)
  148. {
  149. this.item3Checked = selectColor;
  150. if (selectColor)
  151. {
  152. pictureBox1.Cursor = Cursors.Cross;
  153. this.MoveSelectionButton.Focus();
  154. }
  155. else
  156. {
  157. pictureBox1.Cursor = Cursors.Default;
  158. }
  159. }
  160. //在MouseUp处获知鼠标是否松开,终止拖动操作;
  161. private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
  162. {
  163. isSelected = false;
  164. }
  165. private bool IsMouseInPanel()
  166. {
  167. if (this.panel_Picture.Left + this.box_Picture.Left < PointToClient(Cursor.Position).X
  168. && PointToClient(Cursor.Position).X < this.panel_Picture.Left + this.box_Picture.Left
  169. + this.panel_Picture.Width && this.panel_Picture.Top + this.box_Picture.Top
  170. < PointToClient(Cursor.Position).Y && PointToClient(Cursor.Position).Y
  171. < this.panel_Picture.Top + this.box_Picture.Top + this.panel_Picture.Height)
  172. {
  173. return true;
  174. }
  175. else
  176. {
  177. return false;
  178. }
  179. }
  180. //图片平移,在MouseMove处添加拖动函数操作
  181. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  182. {
  183. if (isSelected && IsMouseInPanel())//确定已经激发MouseDown事件,和鼠标在picturebox的范围内
  184. {
  185. this.pictureBox1.Left = this.pictureBox1.Left + (Cursor.Position.X - mouseDownPoint.X);
  186. this.pictureBox1.Top = this.pictureBox1.Top + (Cursor.Position.Y - mouseDownPoint.Y);
  187. mouseDownPoint.X = Cursor.Position.X;
  188. mouseDownPoint.Y = Cursor.Position.Y;
  189. }
  190. }
  191. private void picturePreviewAdjustControl_Resize(object sender, EventArgs e)
  192. {
  193. if (isShown)
  194. {
  195. this.pictureBox1.Left = 0;
  196. this.pictureBox1.Top = 0;
  197. this.pictureBox1.Width = panel_Picture.Width;
  198. this.pictureBox1.Height = panel_Picture.Height;
  199. }
  200. }
  201. private void box_Picture_Enter(object sender, EventArgs e)
  202. {
  203. }
  204. }
  205. }