SelectedRect.protected.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Windows.Media;
  5. namespace ComPDFKit.Tool.DrawTool
  6. {
  7. public partial class SelectedRect
  8. {
  9. #region Properties
  10. /// <summary>
  11. /// Current control point drawing style.
  12. /// </summary>
  13. protected DrawPointType currentDrawPointType { get;
  14. set; }
  15. /// <summary>
  16. /// Current drag drawing style.
  17. /// </summary>
  18. protected DrawMoveType currentDrawMoveType { get; set; }
  19. /// <summary>
  20. /// Current click hit control point.
  21. /// </summary>
  22. protected PointControlType hitControlType { get; set; }
  23. /// <summary>
  24. /// Mouse down position information.
  25. /// </summary>
  26. protected Point mouseDownPoint { get; set; }
  27. /// <summary>
  28. /// Whether the mouse is pressed.
  29. /// </summary>
  30. protected bool isMouseDown { get; set; }
  31. /// <summary>
  32. /// Whether proportional scaling is required.
  33. /// </summary>
  34. protected bool isProportionalScaling { get; set; } = false;
  35. /// <summary>
  36. /// Current control point size.
  37. /// </summary>
  38. protected int pointSize { get; set; } = 4;
  39. /// <summary>
  40. /// Rectangular minimum width.
  41. /// </summary>
  42. protected int rectMinWidth { get; set; } = 10;
  43. /// <summary>
  44. /// Rectangular minimum height.
  45. /// </summary>
  46. protected int RectMinHeight { get; set; } = 10;
  47. /// <summary>
  48. /// Current set of ignore points.
  49. /// </summary>
  50. protected List<PointControlType> ignorePoints { get; set; } = new List<PointControlType>();
  51. /// <summary>
  52. /// Current set of drawing rectangles (original data).
  53. /// </summary>
  54. protected Rect SetDrawRect { get; set; } = new Rect(0, 0, 0, 0);
  55. /// <summary>
  56. /// Current drawing rectangle (calculated during operation).
  57. /// </summary>
  58. protected Rect drawRect { get; set; } = new Rect(0, 0, 0, 0);
  59. /// <summary>
  60. /// Maximum range that can be drawn.
  61. /// </summary>
  62. protected Rect maxRect { get; set; } = new Rect(0, 0, 0, 0);
  63. /// <summary>
  64. /// Current center point of the drawing rectangle.
  65. /// </summary>
  66. protected Point drawCenterPoint { get; private set; } = new Point(0, 0);
  67. /// <summary>
  68. /// When the mouse is pressed, the cached rectangle.
  69. /// </summary>
  70. protected Rect cacheRect { get; set; } = new Rect(0, 0, 0, 0);
  71. /// <summary>
  72. /// Current control point coordinates.
  73. /// </summary>
  74. protected List<Point> controlPoints { get; set; } = new List<Point>();
  75. /// <summary>
  76. /// Move offset during movement.
  77. /// </summary>
  78. protected Point moveOffset { get; set; } = new Point(0, 0);
  79. /// <summary>
  80. /// Current drawing rectangle (calculated during operation).
  81. /// </summary>
  82. protected Rect clipRect { get; set; } = new Rect(0, 0, 0, 0);
  83. private Pen editPen { get; set; } = new Pen(new SolidColorBrush(Color.FromRgb(71, 126, 222)), 2) { DashStyle = DashStyles.Dash };
  84. /// <summary>
  85. /// Current actual display width and height of PDFVIewer.
  86. /// </summary>
  87. protected double PDFViewerActualWidth { get; set; } = 0;
  88. protected double PDFViewerActualHeight { get; set; } = 0;
  89. protected double rectPadding = 6;
  90. protected double currentZoom = 1;
  91. protected SelectedAnnotData selectedRectData = new SelectedAnnotData();
  92. protected bool disable = false;
  93. #endregion
  94. #region Functions
  95. /// <summary>
  96. /// Calcuate the control points
  97. /// </summary>
  98. /// <param name="currentRect">
  99. /// Control points in the target rectangle
  100. /// </param>
  101. protected void CalcControlPoint(Rect currentRect)
  102. {
  103. controlPoints.Clear();
  104. int centerX = (int)(currentRect.Left + currentRect.Right) / 2;
  105. int centerY = (int)(currentRect.Top + currentRect.Bottom) / 2;
  106. controlPoints.Add(new Point(currentRect.Left, currentRect.Top));
  107. controlPoints.Add(new Point(currentRect.Left, centerY));
  108. controlPoints.Add(new Point(currentRect.Left, currentRect.Bottom));
  109. controlPoints.Add(new Point(centerX, currentRect.Bottom));
  110. controlPoints.Add(new Point(currentRect.Right, currentRect.Bottom));
  111. controlPoints.Add(new Point(currentRect.Right, centerY));
  112. controlPoints.Add(new Point(currentRect.Right, currentRect.Top));
  113. controlPoints.Add(new Point(centerX, currentRect.Top));
  114. }
  115. protected List<Point> GetControlPoint(Rect currentRect)
  116. { List<Point> controlCurrentPoints = new List<Point>();
  117. controlCurrentPoints.Clear();
  118. int centerX = (int)(currentRect.Left + currentRect.Right) / 2;
  119. int centerY = (int)(currentRect.Top + currentRect.Bottom) / 2;
  120. controlCurrentPoints.Add(new Point(currentRect.Left, currentRect.Top));
  121. controlCurrentPoints.Add(new Point(currentRect.Left, centerY));
  122. controlCurrentPoints.Add(new Point(currentRect.Left, currentRect.Bottom));
  123. controlCurrentPoints.Add(new Point(centerX, currentRect.Bottom));
  124. controlCurrentPoints.Add(new Point(currentRect.Right, currentRect.Bottom));
  125. controlCurrentPoints.Add(new Point(currentRect.Right, centerY));
  126. controlCurrentPoints.Add(new Point(currentRect.Right, currentRect.Top));
  127. controlCurrentPoints.Add(new Point(centerX, currentRect.Top));
  128. return controlCurrentPoints;
  129. }
  130. /// <summary>
  131. /// Calcuate the offset of the current rectangle in the maximum rectangle range
  132. /// </summary>
  133. /// <param name="currentRect">
  134. /// The rectangle cached when pressed
  135. /// </param>
  136. /// <param name="offsetPoint">
  137. /// Equivalent to the offset value when pressed
  138. /// </param>
  139. /// <param name="maxRect">
  140. /// The maximum rectangle range.
  141. /// </param>
  142. /// <returns></returns>
  143. protected Point CalcMoveBound(Rect currentRect, Point offsetPoint, Rect maxRect)
  144. {
  145. double cLeft = currentRect.Left;
  146. double cRight = currentRect.Right;
  147. double cUp = currentRect.Top;
  148. double cDown = currentRect.Bottom;
  149. double TmpLeft = cLeft + offsetPoint.X;
  150. double TmpRight = cRight + offsetPoint.X;
  151. double TmpUp = cUp + offsetPoint.Y;
  152. double TmpDown = cDown + offsetPoint.Y;
  153. if (TmpLeft < maxRect.Left)
  154. {
  155. TmpRight = (cRight - cLeft) + maxRect.Left;
  156. TmpLeft = maxRect.Left;
  157. }
  158. if (TmpUp < maxRect.Top)
  159. {
  160. TmpDown = (cDown - cUp) + maxRect.Top;
  161. TmpUp = maxRect.Top;
  162. }
  163. if (TmpRight > maxRect.Right)
  164. {
  165. TmpLeft = maxRect.Right - (cRight - cLeft);
  166. TmpRight = maxRect.Right;
  167. }
  168. if (TmpDown > maxRect.Bottom)
  169. {
  170. TmpUp = maxRect.Bottom - (cDown - cUp);
  171. TmpDown = maxRect.Bottom;
  172. }
  173. offsetPoint = new Point(TmpLeft - cLeft, TmpUp - cUp);
  174. return offsetPoint;
  175. }
  176. /// <summary>
  177. /// Calculate the movement of the hit point
  178. /// </summary>
  179. /// <param name="mousePoint">
  180. /// Current mouse position
  181. /// </param>
  182. /// <returns>
  183. /// Whether the movement is successful
  184. /// </returns>
  185. protected bool CalcHitPointMove(Point mousePoint)
  186. {
  187. if (isMouseDown == false || hitControlType == PointControlType.None)
  188. {
  189. return false;
  190. }
  191. return NormalScaling(mousePoint);
  192. }
  193. private Size GetProportionalScalingSize(double width, double height)
  194. {
  195. double minHeight = RectMinHeight + 2 * rectPadding * currentZoom;
  196. double minWidth = rectMinWidth + 2 * rectPadding * currentZoom;
  197. if (minWidth > width || minHeight > height)
  198. {
  199. if (cacheRect.Width >= cacheRect.Height)
  200. {
  201. width = cacheRect.Width * minHeight / cacheRect.Height;
  202. height = minHeight;
  203. }
  204. else
  205. {
  206. height = cacheRect.Height * minWidth / cacheRect.Width;
  207. width = minWidth;
  208. }
  209. }
  210. return new Size(width, height);
  211. }
  212. /// <summary>
  213. /// Draw the algorithm in the form of normal scaling (drag a point, only scale in one direction).
  214. /// </summary>
  215. /// <param name="mousePoint">Current mouse position.</param>
  216. /// <returns></returns>
  217. protected bool NormalScaling(Point mousePoint)
  218. {
  219. try
  220. {
  221. double left = 0, right = 0, top = 0, bottom = 0;
  222. double minHeight = RectMinHeight + 2 * rectPadding * currentZoom;
  223. double minWidth = rectMinWidth + 2 * rectPadding * currentZoom;
  224. Point centerPoint = new Point((cacheRect.Right + cacheRect.Left) / 2, (cacheRect.Bottom + cacheRect.Top) / 2);
  225. Point moveVector = (Point)(mousePoint - centerPoint);
  226. moveVector = ProportionalScalingOffsetPos(moveVector);
  227. switch (hitControlType)
  228. {
  229. case PointControlType.LeftTop:
  230. {
  231. left = centerPoint.X + moveVector.X;
  232. right = cacheRect.Right;
  233. top = centerPoint.Y + moveVector.Y;
  234. bottom = cacheRect.Bottom;
  235. if (isProportionalScaling)
  236. {
  237. Size size = GetProportionalScalingSize(right - left, bottom - top);
  238. left = right - size.Width;
  239. top = bottom - size.Height;
  240. if (left < maxRect.Left)
  241. {
  242. double tmpWidth = right - left;
  243. left = maxRect.Left;
  244. double width = right - left;
  245. double height = (bottom - top) * width / tmpWidth;
  246. top = bottom - height;
  247. }
  248. if (top < maxRect.Top)
  249. {
  250. double tmpHeight = bottom - top;
  251. top = maxRect.Top;
  252. double height = bottom - top;
  253. double width = (right - left) * height / tmpHeight;
  254. left = right - width;
  255. }
  256. }
  257. else
  258. {
  259. if (left + minWidth > right)
  260. {
  261. left = right - minWidth;
  262. }
  263. if (top + minHeight > bottom)
  264. {
  265. top = bottom - minHeight;
  266. }
  267. }
  268. }
  269. break;
  270. case PointControlType.LeftMiddle:
  271. {
  272. left = centerPoint.X + moveVector.X;
  273. right = cacheRect.Right;
  274. top = cacheRect.Top;
  275. bottom = cacheRect.Bottom;
  276. if (left + minWidth > right)
  277. {
  278. left = right - minWidth;
  279. }
  280. }
  281. break;
  282. case PointControlType.LeftBottom:
  283. {
  284. left = centerPoint.X + moveVector.X;
  285. right = cacheRect.Right;
  286. top = cacheRect.Top;
  287. bottom = centerPoint.Y + moveVector.Y;
  288. if (isProportionalScaling)
  289. {
  290. Size size = GetProportionalScalingSize(right - left, bottom - top);
  291. left = right - size.Width;
  292. bottom = top + size.Height;
  293. if (left < maxRect.Left)
  294. {
  295. double tmpWidth = right - left;
  296. left = maxRect.Left;
  297. double width = right - left;
  298. double height = (bottom - top) * width / tmpWidth;
  299. bottom = top + height;
  300. }
  301. if (bottom > maxRect.Bottom)
  302. {
  303. double tmpHeight = bottom - top;
  304. bottom = maxRect.Bottom;
  305. double height = bottom - top;
  306. double width = (right - left) * height / tmpHeight;
  307. left = right - width;
  308. }
  309. }
  310. else
  311. {
  312. if (left + minWidth > right)
  313. {
  314. left = right - minWidth;
  315. }
  316. if (top + minHeight > bottom)
  317. {
  318. bottom = top + minHeight;
  319. }
  320. }
  321. }
  322. break;
  323. case PointControlType.MiddlBottom:
  324. {
  325. left = cacheRect.Left;
  326. right = cacheRect.Right;
  327. top = cacheRect.Top;
  328. bottom = centerPoint.Y + moveVector.Y;
  329. if (top + minHeight > bottom)
  330. {
  331. bottom = top + minHeight;
  332. }
  333. }
  334. break;
  335. case PointControlType.RightBottom:
  336. {
  337. left = cacheRect.Left;
  338. right = centerPoint.X + moveVector.X;
  339. top = cacheRect.Top;
  340. bottom = centerPoint.Y + moveVector.Y;
  341. if (isProportionalScaling)
  342. {
  343. Size size = GetProportionalScalingSize(right - left, bottom - top);
  344. right = left + size.Width;
  345. bottom = top + size.Height;
  346. if (right > maxRect.Right)
  347. {
  348. double tmpWidth = right - left;
  349. right = maxRect.Right;
  350. double width = right - left;
  351. double height = (bottom - top) * width / tmpWidth;
  352. bottom = top + height;
  353. }
  354. if (bottom > maxRect.Bottom)
  355. {
  356. double tmpHeight = bottom - top;
  357. bottom = maxRect.Bottom;
  358. double height = bottom - top;
  359. double width = (right - left) * height / tmpHeight;
  360. right = left + width;
  361. }
  362. }
  363. else
  364. {
  365. if (left + minWidth > right)
  366. {
  367. right = left + minWidth;
  368. }
  369. if (top + minHeight > bottom)
  370. {
  371. bottom = top + minHeight;
  372. }
  373. }
  374. }
  375. break;
  376. case PointControlType.RightMiddle:
  377. {
  378. left = cacheRect.Left;
  379. right = centerPoint.X + moveVector.X;
  380. top = cacheRect.Top;
  381. bottom = cacheRect.Bottom;
  382. if (left + minWidth > right)
  383. {
  384. right = left + minWidth;
  385. }
  386. }
  387. break;
  388. case PointControlType.RightTop:
  389. {
  390. left = cacheRect.Left;
  391. right = centerPoint.X + moveVector.X;
  392. top = centerPoint.Y + moveVector.Y;
  393. bottom = cacheRect.Bottom;
  394. if (isProportionalScaling)
  395. {
  396. Size size = GetProportionalScalingSize(right - left, bottom - top);
  397. right = left + size.Width;
  398. top = bottom - size.Height;
  399. if (right > maxRect.Right)
  400. {
  401. double tmpWidth = right - left;
  402. right = maxRect.Right;
  403. double width = right - left;
  404. double height = (bottom - top) * width / tmpWidth;
  405. top = bottom - height;
  406. }
  407. if (top < maxRect.Top)
  408. {
  409. double tmpHeight = bottom - top;
  410. top = maxRect.Top;
  411. double height = bottom - top;
  412. double width = (right - left) * height / tmpHeight;
  413. right = left + width;
  414. }
  415. }
  416. else
  417. {
  418. if (left + minWidth > right)
  419. {
  420. right = left + minWidth;
  421. }
  422. if (top + minHeight > bottom)
  423. {
  424. top = bottom - minHeight;
  425. }
  426. }
  427. }
  428. break;
  429. case PointControlType.MiddleTop:
  430. {
  431. left = cacheRect.Left;
  432. right = cacheRect.Right;
  433. top = centerPoint.Y + moveVector.Y;
  434. bottom = cacheRect.Bottom;
  435. if (top + minHeight > bottom)
  436. {
  437. top = bottom - minHeight;
  438. }
  439. }
  440. break;
  441. case PointControlType.Body:
  442. case PointControlType.Line:
  443. {
  444. Point OffsetPos = CalcMoveBound(cacheRect, ((Point)(mousePoint - mouseDownPoint)), maxRect);
  445. left = cacheRect.Left + OffsetPos.X;
  446. right = cacheRect.Right + OffsetPos.X;
  447. top = cacheRect.Top + OffsetPos.Y;
  448. bottom = cacheRect.Bottom + OffsetPos.Y;
  449. }
  450. break;
  451. default:
  452. break;
  453. }
  454. if (left < maxRect.Left)
  455. {
  456. left = maxRect.Left;
  457. }
  458. if (top < maxRect.Top)
  459. {
  460. top = maxRect.Top;
  461. }
  462. if (right > maxRect.Right)
  463. {
  464. right = maxRect.Right;
  465. }
  466. if (bottom > maxRect.Bottom)
  467. {
  468. bottom = maxRect.Bottom;
  469. }
  470. drawRect = new Rect(left, top, right - left, bottom - top);
  471. moveOffset = new Point(drawRect.X - cacheRect.X, drawRect.Y - cacheRect.Y);
  472. return true;
  473. }
  474. catch (Exception ex)
  475. {
  476. }
  477. return false;
  478. }
  479. /// <summary>
  480. /// Proportional scaling offset calibration
  481. /// </summary>
  482. /// <param name="movePoint">
  483. /// The current movement point
  484. /// </param>
  485. /// <returns>
  486. /// The offset point after the proportional scaling
  487. /// </returns>
  488. protected Point ProportionalScalingOffsetPos(Point movePoint)
  489. {
  490. if (isProportionalScaling)
  491. {
  492. Point offsetPos = movePoint;
  493. double ratioX = cacheRect.Width > 0 ? cacheRect.Height / cacheRect.Width : 1;
  494. double ratioY = cacheRect.Height > 0 ? cacheRect.Width / cacheRect.Height : 1;
  495. switch (hitControlType)
  496. {
  497. case PointControlType.LeftTop:
  498. case PointControlType.RightBottom:
  499. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? -1 : 1));
  500. break;
  501. case PointControlType.LeftBottom:
  502. case PointControlType.RightTop:
  503. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? 1 : -1));
  504. break;
  505. case PointControlType.LeftMiddle:
  506. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? 1 : -1));
  507. break;
  508. case PointControlType.RightMiddle:
  509. offsetPos = new Point(movePoint.X, Math.Abs(movePoint.X) * ratioX * (movePoint.X < 0 ? -1 : 1));
  510. break;
  511. case PointControlType.MiddlBottom:
  512. offsetPos = new Point(Math.Abs(movePoint.Y) * ratioY * (movePoint.Y < 0 ? 1 : -1), movePoint.Y);
  513. break;
  514. case PointControlType.MiddleTop:
  515. offsetPos = new Point(Math.Abs(movePoint.Y) * ratioY * (movePoint.Y < 0 ? -1 : 1), movePoint.Y);
  516. break;
  517. default:
  518. break;
  519. }
  520. return offsetPos;
  521. }
  522. else
  523. {
  524. return movePoint;
  525. }
  526. }
  527. /// <summary>
  528. /// Inner drawing circle point
  529. /// </summary>
  530. /// <param name="drawingContext">
  531. /// Drawing context
  532. /// </param>
  533. /// <param name="ignoreList">
  534. /// Collection of positions that need to be drawn
  535. /// </param>
  536. /// <param name="PointSize">
  537. /// Size of the point
  538. /// </param>
  539. /// <param name="PointPen">
  540. /// Brush for drawing points
  541. /// </param>
  542. /// <param name="BorderBrush">
  543. /// Border brush for drawing points
  544. /// </param>
  545. protected void DrawCirclePoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  546. {
  547. GeometryGroup controlGroup = new GeometryGroup();
  548. controlGroup.FillRule = FillRule.Nonzero;
  549. List<Point> ignorePointsList = new List<Point>();
  550. // Get specific points
  551. foreach (PointControlType type in ignoreList)
  552. {
  553. if ((int)type < controlPoints.Count)
  554. {
  555. ignorePointsList.Add(controlPoints[(int)type]);
  556. }
  557. }
  558. for (int i = 0; i < controlPoints.Count; i++)
  559. {
  560. Point controlPoint = controlPoints[i];
  561. if (ignorePointsList.Contains(controlPoint))
  562. {
  563. continue;
  564. }
  565. EllipseGeometry circlPoint = new EllipseGeometry(controlPoint, PointSize, PointSize);
  566. controlGroup.Children.Add(circlPoint);
  567. }
  568. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  569. }
  570. /// <summary>
  571. /// Inner drawing square
  572. /// </summary>
  573. /// <param name="drawingContext">
  574. /// Drawing context
  575. /// </param>
  576. /// <param name="ControlPoints">
  577. /// Collection of positions that need to be drawn
  578. /// </param>
  579. /// <param name="PointSize">
  580. /// Size of the point
  581. /// </param>
  582. /// <param name="PointPen">
  583. /// Brush for drawing points
  584. /// </param>
  585. /// <param name="BorderBrush">
  586. /// Border brush for drawing points
  587. /// </param>
  588. protected void DrawSquarePoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  589. {
  590. GeometryGroup controlGroup = new GeometryGroup();
  591. controlGroup.FillRule = FillRule.Nonzero;
  592. List<Point> ignorePointsList = new List<Point>();
  593. // Get specific points
  594. foreach (PointControlType type in ignoreList)
  595. {
  596. if ((int)type < controlPoints.Count)
  597. {
  598. ignorePointsList.Add(controlPoints[(int)type]);
  599. }
  600. }
  601. for (int i = 0; i < controlPoints.Count; i++)
  602. {
  603. Point controlPoint = controlPoints[i];
  604. if (ignorePointsList.Contains(controlPoint))
  605. {
  606. continue;
  607. }
  608. RectangleGeometry rectPoint = new RectangleGeometry(new Rect(controlPoint.X - PointSize, controlPoint.Y - PointSize,
  609. PointSize * 2, PointSize * 2), 1, 1);
  610. controlGroup.Children.Add(rectPoint);
  611. }
  612. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  613. }
  614. protected void DrawCropPoint(DrawingContext drawingContext, List<PointControlType> ignoreList, int PointSize, Pen PointPen, SolidColorBrush BorderBrush)
  615. {
  616. //GeometryGroup controlGroup = new GeometryGroup();
  617. //controlGroup.FillRule = FillRule.Nonzero;
  618. clipRect = drawRect;
  619. List<Point> controlCurrentPoints=GetControlPoint(drawRect);
  620. CombinedGeometry controlGroup = new CombinedGeometry();
  621. RectangleGeometry paintGeometry = new RectangleGeometry();
  622. paintGeometry.Rect = SetDrawRect;
  623. controlGroup.Geometry1 = paintGeometry;
  624. RectangleGeometry moveGeometry = new RectangleGeometry();
  625. Rect clippedBorder = drawRect;
  626. if (clippedBorder.IsEmpty == false)
  627. {
  628. moveGeometry.Rect = drawRect;
  629. }
  630. controlGroup.Geometry2 = moveGeometry;
  631. controlGroup.GeometryCombineMode = GeometryCombineMode.Exclude;
  632. //Left Top Corner
  633. if (!ignoreList.Contains(PointControlType.LeftTop))
  634. {
  635. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[0].X - PointSize, controlCurrentPoints[0].Y - PointSize, PointSize, PointSize * 4));
  636. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[0].X - PointSize, controlCurrentPoints[0].Y - PointSize, PointSize * 4, PointSize));
  637. }
  638. //Left Center
  639. if (!ignoreList.Contains(PointControlType.LeftMiddle))
  640. {
  641. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[1].X - PointSize, (controlCurrentPoints[1].Y + controlCurrentPoints[1].Y - PointSize * 5) / 2, PointSize, PointSize * 5));
  642. }
  643. //Left Bottom Corner
  644. if (!ignoreList.Contains(PointControlType.LeftBottom))
  645. {
  646. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[2].X - PointSize, controlCurrentPoints[2].Y - PointSize * 3, PointSize, PointSize * 4));
  647. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[2].X - PointSize, controlCurrentPoints[2].Y, PointSize * 4, PointSize));
  648. }
  649. //Bottom Center
  650. if (!ignoreList.Contains(PointControlType.MiddlBottom))
  651. {
  652. drawingContext?.DrawRectangle(BorderBrush, null, new Rect((controlCurrentPoints[3].X + controlCurrentPoints[3].X - PointSize * 5) / 2, controlCurrentPoints[3].Y, PointSize * 5, PointSize));
  653. }
  654. //Bottom Right Corner
  655. if (!ignoreList.Contains(PointControlType.RightBottom))
  656. {
  657. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[4].X, controlCurrentPoints[4].Y - PointSize * 3, PointSize, PointSize * 4));
  658. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[4].X - PointSize * 3, controlCurrentPoints[4].Y, PointSize * 4, PointSize));
  659. }
  660. //Right Center
  661. if (!ignoreList.Contains(PointControlType.RightMiddle))
  662. {
  663. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[5].X, (controlCurrentPoints[5].Y + controlCurrentPoints[5].Y - PointSize * 5) / 2, PointSize, PointSize * 5));
  664. }
  665. //Right Top Corner
  666. if (!ignoreList.Contains(PointControlType.RightTop))
  667. {
  668. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[6].X, controlCurrentPoints[6].Y - PointSize, PointSize, PointSize * 4));
  669. drawingContext?.DrawRectangle(BorderBrush, null, new Rect(controlCurrentPoints[6].X - PointSize * 4, controlCurrentPoints[6].Y - PointSize, PointSize * 4, PointSize));
  670. }
  671. //Top Center
  672. if (!ignoreList.Contains(PointControlType.MiddleTop))
  673. {
  674. drawingContext?.DrawRectangle(BorderBrush, null, new Rect((controlCurrentPoints[7].X + controlCurrentPoints[7].X - PointSize * 5) / 2, controlCurrentPoints[7].Y - PointSize, PointSize * 5, PointSize));
  675. }
  676. BorderBrush = new SolidColorBrush(Color.FromArgb(0x3F, 0x00, 0x00, 0x00));
  677. drawingContext?.DrawGeometry(BorderBrush, PointPen, controlGroup);
  678. }
  679. /// <summary>
  680. /// Draw the reference line in the moving state
  681. /// </summary>
  682. /// <param name="drawDc">
  683. /// Draw context handle
  684. /// </param>
  685. /// <param name="controltype">
  686. /// Current selected control point type
  687. /// </param>
  688. /// <param name="activePen">
  689. /// Brush for drawing lines
  690. /// </param>
  691. /// <param name="moveBrush">
  692. /// Brush for drawing rectangles
  693. /// </param>
  694. /// <param name="moveRect">
  695. /// Current rectangle to draw
  696. /// </param>
  697. protected void DrawMoveBounds(DrawingContext drawDc, PointControlType controltype, Pen activePen, Brush moveBrush, Rect moveRect)
  698. {
  699. switch (controltype)
  700. {
  701. case PointControlType.LeftTop:
  702. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  703. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  704. break;
  705. case PointControlType.LeftMiddle:
  706. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  707. break;
  708. case PointControlType.LeftBottom:
  709. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  710. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  711. break;
  712. case PointControlType.MiddlBottom:
  713. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  714. break;
  715. case PointControlType.RightBottom:
  716. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  717. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  718. break;
  719. case PointControlType.RightMiddle:
  720. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  721. break;
  722. case PointControlType.RightTop:
  723. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  724. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  725. break;
  726. case PointControlType.MiddleTop:
  727. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  728. break;
  729. case PointControlType.Rotate:
  730. break;
  731. case PointControlType.Body:
  732. case PointControlType.Line:
  733. drawDc?.DrawLine(activePen, new Point(0, moveRect.Top), new Point(PDFViewerActualWidth, moveRect.Top));
  734. drawDc?.DrawLine(activePen, new Point(0, moveRect.Bottom), new Point(PDFViewerActualWidth, moveRect.Bottom));
  735. drawDc?.DrawLine(activePen, new Point(moveRect.Left, 0), new Point(moveRect.Left, PDFViewerActualHeight));
  736. drawDc?.DrawLine(activePen, new Point(moveRect.Right, 0), new Point(moveRect.Right, PDFViewerActualHeight));
  737. break;
  738. default:
  739. break;
  740. }
  741. drawDc?.DrawRectangle(moveBrush, null, moveRect);
  742. }
  743. /// <summary>
  744. /// Notify the event during/after the drawing data
  745. /// </summary>
  746. /// <param name="isFinish">
  747. /// Identifies whether the data change is complete
  748. /// </param>
  749. protected void InvokeDataChangEvent(bool isFinish)
  750. {
  751. selectedRectData.Square = GetRect();
  752. if (isFinish)
  753. {
  754. DataChanged?.Invoke(this, selectedRectData);
  755. }
  756. else
  757. {
  758. DataChanging?.Invoke(this, selectedRectData);
  759. }
  760. }
  761. /// <summary>
  762. /// Align the rectangle drawing
  763. /// </summary>
  764. /// <param name="RectMovePoint">
  765. /// Move distance required for the aligned algorithm to obtain the rectangle
  766. /// </param>
  767. private void DrawAlignRect(Point RectMovePoint)
  768. {
  769. double TmpLeft, TmpRight, TmpUp, TmpDown;
  770. Point OffsetPos = CalcMoveBound(drawRect, RectMovePoint, maxRect);
  771. TmpLeft = drawRect.Left + OffsetPos.X;
  772. TmpRight = drawRect.Right + OffsetPos.X;
  773. TmpUp = drawRect.Top + OffsetPos.Y;
  774. TmpDown = drawRect.Bottom + OffsetPos.Y;
  775. SetDrawRect = drawRect = new Rect(TmpLeft, TmpUp, TmpRight - TmpLeft, TmpDown - TmpUp);
  776. Draw();
  777. }
  778. /// <summary>
  779. /// Get the current set of ignore points
  780. /// </summary>
  781. /// <returns>
  782. /// Data set of ignored points
  783. /// </returns>
  784. private List<PointControlType> GetIgnorePoints()
  785. {
  786. List<PointControlType> IgnorePointsList = new List<PointControlType>();
  787. foreach (PointControlType type in ignorePoints)
  788. {
  789. IgnorePointsList.Add(type);
  790. }
  791. return IgnorePointsList;
  792. }
  793. #endregion
  794. }
  795. }