CustomPanel.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using PDF_Office.Helper;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Imaging;
  11. namespace PDF_Office.CustomControl.ScanViewControl
  12. {
  13. class CustomPanel : Panel
  14. {
  15. public SolidColorBrush CoverBrush { get; set; } = new SolidColorBrush(Color.FromArgb(0xCC, 0xFF, 0xFF, 0xFF));
  16. public bool DrawCover { get; set; } = false;
  17. private WriteableBitmap BackgroundImage = null;
  18. private int BackgroundWidth = 0;
  19. private int BackgroundHeight = 0;
  20. private Point OffsetPos = new Point(0, 0);
  21. private double ScaleRate = 1;
  22. private CustomDraw HoverChild = null;
  23. public TextBlock ParentBlock;
  24. private int RawWidth = 0;
  25. private int RawHeight = 0;
  26. /// <summary>
  27. /// 展示用的背景图
  28. /// </summary>
  29. public WriteableBitmap BGImage
  30. {
  31. get { return (WriteableBitmap)GetValue(BGImageProperty); }
  32. set
  33. {
  34. SetValue(BGImageProperty, value);
  35. }
  36. }
  37. // Using a DependencyProperty as the backing store for BGImage. This enables animation, styling, binding, etc...
  38. public static readonly DependencyProperty BGImageProperty =
  39. DependencyProperty.Register("BGImage", typeof(WriteableBitmap), typeof(CustomPanel), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnImageChanged)));
  40. private static void OnImageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  41. {
  42. if ((e.NewValue as WriteableBitmap) != null)
  43. {
  44. (d as CustomPanel).SetBackgroundImage(e.NewValue as WriteableBitmap);
  45. }
  46. }
  47. /// <summary>
  48. /// 当前页的OCR结果
  49. /// </summary>
  50. public List<KeyValuePair<Rect, string>> OCRTextRectList
  51. {
  52. get { return (List<KeyValuePair<Rect, string>>)GetValue(OCRTextRectListProperty); }
  53. set { SetValue(OCRTextRectListProperty, value); }
  54. }
  55. // Using a DependencyProperty as the backing store for OCRTextRectList. This enables animation, styling, binding, etc...
  56. public static readonly DependencyProperty OCRTextRectListProperty =
  57. DependencyProperty.Register("OCRTextRectList", typeof(List<KeyValuePair<Rect, string>>), typeof(CustomPanel), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnOCRChanged)));
  58. private static void OnOCRChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  59. {
  60. if ((e.NewValue as List<KeyValuePair<Rect, string>>) != null)
  61. {
  62. (d as CustomPanel).SetTextsRects(e.NewValue as List<KeyValuePair<Rect, string>>);
  63. }
  64. }
  65. private List<CustomDraw> VisualControlList = new List<CustomDraw>();
  66. protected override int VisualChildrenCount => VisualControlList.Count;
  67. protected override Visual GetVisualChild(int index)
  68. {
  69. return VisualControlList[index];
  70. }
  71. protected override void OnRender(DrawingContext dc)
  72. {
  73. Rect drawRect = new Rect(0, 0, ActualWidth, ActualHeight);
  74. dc.DrawRectangle(Brushes.LightGray, null, drawRect);
  75. DrawBackground(dc);
  76. }
  77. protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
  78. {
  79. double scale = Math.Min(sizeInfo.NewSize.Width / BackgroundWidth, sizeInfo.NewSize.Height / BackgroundHeight);
  80. int drawWidth = (int)(scale * BackgroundWidth);
  81. int drawHeight = (int)(scale * BackgroundHeight);
  82. if (drawWidth < 10 || drawHeight < 10)
  83. {
  84. return;
  85. }
  86. int offsetX = (int)((sizeInfo.NewSize.Width - drawWidth) / 2);
  87. int offsetY = (int)((sizeInfo.NewSize.Height - drawHeight) / 2);
  88. foreach (CustomDraw drawVisual in VisualControlList)
  89. {
  90. Rect paintRect = drawVisual.RawRect;
  91. drawVisual.PaintRect = new Rect((int)(paintRect.Left * scale + offsetX),
  92. (int)(paintRect.Top * scale + offsetY),
  93. (int)(paintRect.Width * scale),
  94. (int)(paintRect.Height * scale));
  95. drawVisual.Draw();
  96. }
  97. }
  98. private void DrawBackground(DrawingContext dc)
  99. {
  100. if (BackgroundImage != null && BackgroundWidth > 0 && BackgroundHeight > 0)
  101. {
  102. double scale = Math.Min(ActualWidth / BackgroundWidth, ActualHeight / BackgroundHeight);
  103. int drawWidth = (int)(scale * BackgroundWidth);
  104. int drawHeight = (int)(scale * BackgroundHeight);
  105. if (drawWidth < 10 || drawHeight < 10)
  106. {
  107. return;
  108. }
  109. ScaleRate = scale;
  110. OffsetPos.X = (int)((ActualWidth - drawWidth) / 2);
  111. OffsetPos.Y = (int)((ActualHeight - drawHeight) / 2);
  112. Rect drawRect = new Rect(OffsetPos.X, OffsetPos.Y, drawWidth, drawHeight);
  113. dc.DrawImage(BackgroundImage, drawRect);
  114. if (DrawCover)
  115. {
  116. dc.DrawRectangle(CoverBrush, null, drawRect);
  117. }
  118. }
  119. }
  120. private void CleanChild()
  121. {
  122. foreach (DrawingVisual child in VisualControlList)
  123. {
  124. RemoveLogicalChild(child);
  125. RemoveVisualChild(child);
  126. }
  127. VisualControlList.Clear();
  128. }
  129. public void SetBackgroundImage(WriteableBitmap bgImage)
  130. {
  131. DrawCover = false;
  132. BackgroundImage = bgImage;
  133. BackgroundWidth = 0;
  134. BackgroundHeight = 0;
  135. RawWidth = 0;
  136. RawHeight = 0;
  137. OffsetPos = new Point(0, 0);
  138. CleanChild();
  139. if (BackgroundImage != null && BackgroundImage.PixelHeight > 0 && BackgroundImage.PixelWidth > 0)
  140. {
  141. RawWidth = BackgroundImage.PixelWidth;
  142. RawHeight = BackgroundImage.PixelHeight;
  143. BackgroundWidth = DpiHelpers.GetDpiUnrelatedNum(BackgroundImage.PixelWidth);
  144. BackgroundHeight = DpiHelpers.GetDpiUnrelatedNum(BackgroundImage.PixelHeight);
  145. double scale = Math.Min(ActualWidth / BackgroundWidth, ActualHeight / BackgroundHeight);
  146. int drawWidth = (int)(scale * BackgroundWidth);
  147. int drawHeight = (int)(scale * BackgroundHeight);
  148. if (drawWidth < 10 || drawHeight < 10)
  149. {
  150. return;
  151. }
  152. ScaleRate = scale;
  153. OffsetPos.X = (int)((ActualWidth - drawWidth) / 2);
  154. OffsetPos.Y = (int)((ActualHeight - drawHeight) / 2);
  155. InvalidateVisual();
  156. }
  157. }
  158. public void DisplayContent(CustomDraw custom)
  159. {
  160. if (custom != null)
  161. {
  162. if (HoverChild == null)
  163. {
  164. HoverChild = custom;
  165. HoverChild.DrawBounds();
  166. if (ParentBlock != null)
  167. {
  168. ParentBlock.Text = HoverChild.PaintText;
  169. }
  170. }
  171. else
  172. {
  173. if (HoverChild != custom)
  174. {
  175. HoverChild?.Draw();
  176. HoverChild = custom;
  177. HoverChild.DrawBounds();
  178. }
  179. }
  180. }
  181. else
  182. {
  183. if (HoverChild != null)
  184. {
  185. HoverChild.Draw();
  186. HoverChild = null;
  187. }
  188. }
  189. }
  190. public int GetBackgroundImageWidth()
  191. {
  192. if (BackgroundImage != null)
  193. {
  194. return RawWidth;
  195. }
  196. return 0;
  197. }
  198. public int GetBackgroundImageHeight()
  199. {
  200. if (BackgroundImage != null)
  201. {
  202. return RawHeight;
  203. }
  204. return 0;
  205. }
  206. public void SetTextsRects(List<KeyValuePair<Rect, string>> textsRect)
  207. {
  208. CleanChild();
  209. foreach (KeyValuePair<Rect, string> textRect in textsRect)
  210. {
  211. CustomDraw drawVisual = new CustomDraw();
  212. drawVisual.PaintText = textRect.Value;
  213. Rect paintRect = DpiHelpers.GetDpiUnrelatedRect(textRect.Key);
  214. drawVisual.RawRect = paintRect;
  215. drawVisual.PaintRect = new Rect((int)(paintRect.Left * ScaleRate + OffsetPos.X),
  216. (int)(paintRect.Top * ScaleRate + OffsetPos.Y),
  217. (int)(paintRect.Width * ScaleRate),
  218. (int)(paintRect.Height * ScaleRate));
  219. drawVisual.Draw();
  220. VisualControlList.Add(drawVisual);
  221. AddLogicalChild(drawVisual);
  222. AddVisualChild(drawVisual);
  223. }
  224. }
  225. public void RePaint()
  226. {
  227. InvalidateVisual();
  228. }
  229. }
  230. }