星火微课系统客户端
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ImageOperationUtil.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. using System;
  2. using System.Drawing;
  3. using System.Linq;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Controls.Primitives;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. namespace XHWK.WKTool.Utils
  10. {
  11. internal class ImageOperationUtil
  12. {
  13. #region 图片拉伸移动
  14. private System.Windows.Point _initialPoint;
  15. private Thumb _rectLeftUp;
  16. private Thumb _rectRightUp;
  17. private Thumb _rectLeftDown;
  18. private Thumb _rectRightDown;
  19. private System.Windows.Shapes.Rectangle _rectImgBorder;
  20. private System.Windows.Controls.Image _imgCanvas;
  21. private Grid _gridM;
  22. private bool _mouseDown;
  23. public void SetMouseDown(bool mouseDown)
  24. {
  25. this._mouseDown = mouseDown;
  26. }
  27. public ImageOperationUtil
  28. (
  29. Thumb rectLeftUp,
  30. Thumb rectRightUp,
  31. Thumb rectLeftDown,
  32. Thumb rectRightDown,
  33. System.Windows.Shapes.Rectangle rectImgBorder,
  34. System.Windows.Controls.Image imgCanvas,
  35. Grid gridM
  36. )
  37. {
  38. this._rectLeftUp = rectLeftUp;
  39. this._rectRightUp = rectRightUp;
  40. this._rectLeftDown = rectLeftDown;
  41. this._rectRightDown = rectRightDown;
  42. this._rectImgBorder = rectImgBorder;
  43. this._imgCanvas = imgCanvas;
  44. this._gridM = gridM;
  45. }
  46. /// <summary>
  47. /// 隐藏图片四个点和线
  48. /// </summary>
  49. public void HideAngleBorder()
  50. {
  51. _rectLeftUp.Visibility = Visibility.Hidden;
  52. _rectRightUp.Visibility = Visibility.Hidden;
  53. _rectLeftDown.Visibility = Visibility.Hidden;
  54. _rectRightDown.Visibility = Visibility.Hidden;
  55. _rectImgBorder.Visibility = Visibility.Hidden;
  56. }
  57. private void ShowAngleBorder()
  58. {
  59. PointLocation();
  60. _rectLeftUp.Visibility = Visibility.Visible;
  61. _rectRightUp.Visibility = Visibility.Visible;
  62. _rectLeftDown.Visibility = Visibility.Visible;
  63. _rectRightDown.Visibility = Visibility.Visible;
  64. _rectImgBorder.Visibility = Visibility.Visible;
  65. }
  66. /// <summary>
  67. /// 确定四个点和边框的位置大小
  68. /// </summary>
  69. private void PointLocation()
  70. {
  71. _rectImgBorder.Width = _imgCanvas.ActualWidth + 10.0;
  72. _rectImgBorder.Height = _imgCanvas.ActualHeight + 10.0;
  73. _rectImgBorder.Margin = new Thickness(
  74. _imgCanvas.Margin.Left - 5.0,
  75. _imgCanvas.Margin.Top - 5.0,
  76. 0,
  77. 0
  78. );
  79. Canvas.SetLeft(_rectLeftUp, _imgCanvas.Margin.Left - 10.0);
  80. Canvas.SetTop(_rectLeftUp, _imgCanvas.Margin.Top - 10.0);
  81. Canvas.SetLeft(_rectRightUp, _imgCanvas.Margin.Left + _imgCanvas.ActualWidth - 10.0);
  82. Canvas.SetTop(_rectRightUp, _imgCanvas.Margin.Top - 10.0);
  83. Canvas.SetLeft(_rectLeftDown, _imgCanvas.Margin.Left - 10.0);
  84. Canvas.SetTop(_rectLeftDown, _imgCanvas.Margin.Top + _imgCanvas.ActualHeight - 10.0);
  85. Canvas.SetLeft(_rectRightDown, _imgCanvas.Margin.Left + _imgCanvas.ActualWidth - 10.0);
  86. Canvas.SetTop(_rectRightDown, _imgCanvas.Margin.Top + _imgCanvas.ActualHeight - 10.0);
  87. }
  88. public void PicEMap_MouseDown(object sender, MouseButtonEventArgs e)
  89. {
  90. System.Windows.Point point = e.GetPosition(_imgCanvas);
  91. _initialPoint = point;
  92. HideAngleBorder();
  93. }
  94. public void imgCanvas_MouseMove(object sender, MouseEventArgs e)
  95. {
  96. if (e.LeftButton == MouseButtonState.Pressed && _mouseDown)
  97. {
  98. System.Windows.Point point = e.GetPosition(_imgCanvas);
  99. _imgCanvas.Margin = new Thickness(
  100. _imgCanvas.Margin.Left + (point.X - _initialPoint.X),
  101. _imgCanvas.Margin.Top + (point.Y - _initialPoint.Y),
  102. 0,
  103. 0
  104. );
  105. App.PageDrawList[App.PageContextData.currpage - 1].ImageLocation = new TranslateTransform { X = _imgCanvas.Margin.Left, Y = _imgCanvas.Margin.Top };
  106. App.PageDrawList[App.PageContextData.currpage - 1].ImageSizes = new ScaleTransform { CenterX = _imgCanvas.ActualWidth, CenterY = _imgCanvas.ActualHeight };
  107. App.PageDrawList[App.PageContextData.currpage - 1].IsImageLocation = false;
  108. }
  109. }
  110. public void imgCanvas_MouseUp()
  111. {
  112. if (_mouseDown)
  113. {
  114. ShowAngleBorder();
  115. }
  116. }
  117. private PointF _imgRightDown;
  118. /// <summary>
  119. /// 设置控件最上层
  120. /// </summary>
  121. /// <param name="element">
  122. /// </param>
  123. public void BringToFront(Thumb element) //图片置于最顶层显示
  124. {
  125. if (element == null)
  126. {
  127. return;
  128. }
  129. Canvas parent = element.Parent as Canvas;
  130. if (parent == null)
  131. {
  132. return;
  133. }
  134. int maxZ = parent.Children.OfType<UIElement>() //linq语句,取Zindex的最大值
  135. .Where(x => x != element)
  136. .Select(x => Panel.GetZIndex(x))
  137. .Max();
  138. Panel.SetZIndex(element, maxZ + 1);
  139. }
  140. public void RectRightUp_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
  141. {
  142. try
  143. {
  144. Thumb thu = (Thumb)sender;
  145. BringToFront(thu);
  146. _imgRightDown = new System.Drawing.PointF((float)(_imgCanvas.Margin.Left + _imgCanvas.ActualWidth), (float)(_imgCanvas.Margin.Top + _imgCanvas.ActualHeight));
  147. HideAngleBorder();
  148. switch (thu.Name)
  149. {
  150. case "RectLeftUp":
  151. _rectLeftUp.Visibility = Visibility.Visible;
  152. break;
  153. case "RectRightUp":
  154. _rectRightUp.Visibility = Visibility.Visible;
  155. break;
  156. case "RectLeftDown":
  157. _rectLeftDown.Visibility = Visibility.Visible;
  158. break;
  159. case "RectRightDown":
  160. _rectRightDown.Visibility = Visibility.Visible;
  161. break;
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. MessageWindow.Show(ex.Message);
  167. }
  168. }
  169. public void RectRightUp_DragDelta(object sender, DragDeltaEventArgs e)
  170. {
  171. try
  172. {
  173. if (_mouseDown)
  174. {
  175. Thumb thu = (Thumb)sender;
  176. double imgW;
  177. double imgH;
  178. double imgX;
  179. double imgY;
  180. switch (thu.Name)
  181. {
  182. case "RectLeftUp":
  183. #region 左上
  184. imgW = _imgRightDown.X - (Mouse.GetPosition(_gridM).X);
  185. imgH = _imgRightDown.Y - (Mouse.GetPosition(_gridM).Y);
  186. if (imgW < 50)
  187. {
  188. _imgCanvas.Width = 50;
  189. imgX = _imgCanvas.Margin.Left;
  190. Canvas.SetLeft(thu, Canvas.GetLeft(_rectRightDown) - 50);
  191. }
  192. else
  193. {
  194. _imgCanvas.Width = imgW;
  195. imgX = Mouse.GetPosition(_gridM).X;
  196. Canvas.SetLeft(thu, Canvas.GetLeft(thu) + e.HorizontalChange);
  197. }
  198. if (imgH < 50)
  199. {
  200. _imgCanvas.Height = 50;
  201. imgY = _imgCanvas.Margin.Top;
  202. Canvas.SetTop(thu, Canvas.GetTop(_rectRightDown) - 50);
  203. }
  204. else
  205. {
  206. _imgCanvas.Height = imgH;
  207. imgY = Mouse.GetPosition(_gridM).Y;
  208. Canvas.SetTop(thu, Canvas.GetTop(thu) + e.VerticalChange);
  209. }
  210. _imgCanvas.Margin = new Thickness(
  211. imgX,
  212. imgY,
  213. 0,
  214. 0
  215. );
  216. #endregion 左上
  217. break;
  218. case "RectRightUp":
  219. #region 右上
  220. imgW = Mouse.GetPosition(_gridM).X - _imgCanvas.Margin.Left;
  221. imgH = _imgRightDown.Y - (Mouse.GetPosition(_gridM).Y);
  222. if (imgW < 50)
  223. {
  224. _imgCanvas.Width = 50;
  225. imgX = _imgCanvas.Margin.Left;
  226. Canvas.SetLeft(thu, Canvas.GetLeft(_rectLeftUp) + 50);
  227. }
  228. else
  229. {
  230. _imgCanvas.Width = imgW;
  231. imgX = _imgCanvas.Margin.Left;
  232. Canvas.SetLeft(thu, Canvas.GetLeft(thu) + e.HorizontalChange);
  233. }
  234. if (imgH < 50)
  235. {
  236. _imgCanvas.Height = 50;
  237. imgY = _imgCanvas.Margin.Top;
  238. Canvas.SetTop(thu, Canvas.GetTop(_rectRightDown) - 50);
  239. }
  240. else
  241. {
  242. _imgCanvas.Height = imgH;
  243. imgY = Mouse.GetPosition(_gridM).Y;
  244. Canvas.SetTop(thu, Canvas.GetTop(thu) + e.VerticalChange);
  245. }
  246. _imgCanvas.Margin = new Thickness(
  247. imgX,
  248. imgY,
  249. 0,
  250. 0
  251. );
  252. #endregion 右上
  253. break;
  254. case "RectLeftDown":
  255. #region 左下
  256. imgW = _imgRightDown.X - (Mouse.GetPosition(_gridM).X);
  257. imgH = Mouse.GetPosition(_gridM).Y - _imgCanvas.Margin.Top;
  258. if (imgW < 50)
  259. {
  260. _imgCanvas.Width = 50;
  261. imgX = _imgCanvas.Margin.Left;
  262. Canvas.SetLeft(thu, Canvas.GetLeft(_rectRightDown) - 50);
  263. }
  264. else
  265. {
  266. _imgCanvas.Width = imgW;
  267. imgX = Mouse.GetPosition(_gridM).X;
  268. Canvas.SetLeft(thu, Canvas.GetLeft(thu) + e.HorizontalChange);
  269. }
  270. if (imgH < 50)
  271. {
  272. _imgCanvas.Height = 50;
  273. imgY = _imgCanvas.Margin.Top;
  274. Canvas.SetTop(thu, Canvas.GetTop(_rectLeftUp) + 50);
  275. }
  276. else
  277. {
  278. _imgCanvas.Height = imgH;
  279. imgY = _imgCanvas.Margin.Top;
  280. Canvas.SetTop(thu, Canvas.GetTop(thu) + e.VerticalChange);
  281. }
  282. _imgCanvas.Margin = new Thickness(
  283. imgX,
  284. imgY,
  285. 0,
  286. 0
  287. );
  288. //imgCanvas.Width = imgRightDown.X - (Mouse.GetPosition(GridM).X);
  289. //imgCanvas.Height = Mouse.GetPosition(GridM).Y - imgCanvas.Margin.Top;
  290. //imgCanvas.Margin = new Thickness(Mouse.GetPosition(GridM).X, imgCanvas.Margin.Top, 0, 0);
  291. #endregion 左下
  292. break;
  293. case "RectRightDown":
  294. #region 右下
  295. imgW = Mouse.GetPosition(_gridM).X - _imgCanvas.Margin.Left;
  296. imgH = Mouse.GetPosition(_gridM).Y - _imgCanvas.Margin.Top;
  297. if (imgW < 50)
  298. {
  299. _imgCanvas.Width = 50;
  300. Canvas.SetLeft(thu, Canvas.GetLeft(_rectLeftUp) + 50);
  301. }
  302. else
  303. {
  304. _imgCanvas.Width = imgW;
  305. Canvas.SetLeft(thu, Canvas.GetLeft(thu) + e.HorizontalChange);
  306. }
  307. if (imgH < 50)
  308. {
  309. _imgCanvas.Height = 50;
  310. Canvas.SetTop(thu, Canvas.GetTop(_rectLeftUp) + 50);
  311. }
  312. else
  313. {
  314. _imgCanvas.Height = imgH;
  315. Canvas.SetTop(thu, Canvas.GetTop(thu) + e.VerticalChange);
  316. }
  317. //imgCanvas.Margin = new Thickness(imgX, imgY, 0, 0);
  318. //imgCanvas.Width += e.HorizontalChange;
  319. //imgCanvas.Height += e.VerticalChange;
  320. #endregion 右下
  321. break;
  322. }
  323. App.PageDrawList[App.PageContextData.currpage - 1].ImageLocation = new TranslateTransform { X = _imgCanvas.Margin.Left, Y = _imgCanvas.Margin.Top };
  324. App.PageDrawList[App.PageContextData.currpage - 1].ImageSizes = new ScaleTransform { CenterX = _imgCanvas.ActualWidth, CenterY = _imgCanvas.ActualHeight };
  325. App.PageDrawList[App.PageContextData.currpage - 1].IsImageLocation = false;
  326. }
  327. }
  328. catch (Exception)
  329. {
  330. MessageWindow.Show("图片过小!");
  331. }
  332. }
  333. public void RectRightUp_DragCompleted()
  334. {
  335. ShowAngleBorder();
  336. }
  337. /// <summary>
  338. /// 点击标题栏 隐藏截图的四个点和线
  339. /// </summary>
  340. public void Grid_MouseDown()
  341. {
  342. HideAngleBorder();
  343. }
  344. /// <summary>
  345. /// 鼠标左键点击事件
  346. /// </summary>
  347. public void Window_MouseLeftButtonDown_1()
  348. {
  349. if (_rectImgBorder.Visibility != Visibility.Hidden)
  350. {
  351. HideAngleBorder();
  352. }
  353. }
  354. #endregion 图片拉伸移动
  355. }
  356. }