星火微课系统客户端
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ImageOperationUtil.cs 16KB

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