CustomPanel.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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(0xA0, 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. scale = Math.Min(scale, 1);
  81. int drawWidth = (int)(scale * BackgroundWidth);
  82. int drawHeight = (int)(scale * BackgroundHeight);
  83. if (drawWidth < 10 || drawHeight < 10)
  84. {
  85. return;
  86. }
  87. int offsetX = (int)((sizeInfo.NewSize.Width - drawWidth) / 2);
  88. int offsetY = (int)((sizeInfo.NewSize.Height - drawHeight) / 2);
  89. foreach (CustomDraw drawVisual in VisualControlList)
  90. {
  91. Rect paintRect = drawVisual.RawRect;
  92. drawVisual.PaintRect = new Rect((int)(paintRect.Left * scale + offsetX),
  93. (int)(paintRect.Top * scale + offsetY),
  94. (int)(paintRect.Width * scale),
  95. (int)(paintRect.Height * scale));
  96. drawVisual.Draw();
  97. }
  98. }
  99. private void DrawBackground(DrawingContext dc)
  100. {
  101. if (BackgroundImage != null && BackgroundWidth > 0 && BackgroundHeight > 0)
  102. {
  103. double scale = Math.Min(ActualWidth / BackgroundWidth, ActualHeight / BackgroundHeight);
  104. //scale = Math.Min(scale, 1);
  105. int drawWidth = (int)(scale * BackgroundWidth);
  106. int drawHeight = (int)(scale * BackgroundHeight);
  107. if (drawWidth < 10 || drawHeight < 10)
  108. {
  109. return;
  110. }
  111. ScaleRate = scale;
  112. OffsetPos.X = (int)((ActualWidth - drawWidth) / 2);
  113. OffsetPos.Y = (int)((ActualHeight - drawHeight) / 2);
  114. Rect drawRect = new Rect(OffsetPos.X, OffsetPos.Y, drawWidth, drawHeight);
  115. dc.DrawImage(BackgroundImage, drawRect);
  116. if (DrawCover)
  117. {
  118. dc.DrawRectangle(CoverBrush, null, drawRect);
  119. }
  120. }
  121. }
  122. private void CleanChild()
  123. {
  124. foreach (DrawingVisual child in VisualControlList)
  125. {
  126. RemoveLogicalChild(child);
  127. RemoveVisualChild(child);
  128. }
  129. VisualControlList.Clear();
  130. }
  131. public void SetBackgroundImage(WriteableBitmap bgImage)
  132. {
  133. DrawCover = false;
  134. BackgroundImage = bgImage;
  135. BackgroundWidth = 0;
  136. BackgroundHeight = 0;
  137. RawWidth = 0;
  138. RawHeight = 0;
  139. OffsetPos = new Point(0, 0);
  140. CleanChild();
  141. if (BackgroundImage != null && BackgroundImage.PixelHeight > 0 && BackgroundImage.PixelWidth > 0)
  142. {
  143. RawWidth = BackgroundImage.PixelWidth;
  144. RawHeight = BackgroundImage.PixelHeight;
  145. BackgroundWidth = DpiHelpers.GetDpiUnrelatedNum(BackgroundImage.PixelWidth);
  146. BackgroundHeight = DpiHelpers.GetDpiUnrelatedNum(BackgroundImage.PixelHeight);
  147. double scale = Math.Min(ActualWidth / BackgroundWidth, ActualHeight / BackgroundHeight);
  148. scale = Math.Min(scale, 1);
  149. int drawWidth = (int)(scale * BackgroundWidth);
  150. int drawHeight = (int)(scale * BackgroundHeight);
  151. if (drawWidth < 10 || drawHeight < 10)
  152. {
  153. return;
  154. }
  155. ScaleRate = scale;
  156. OffsetPos.X = (int)((ActualWidth - drawWidth) / 2);
  157. OffsetPos.Y = (int)((ActualHeight - drawHeight) / 2);
  158. InvalidateVisual();
  159. }
  160. }
  161. public int GetBackgroundImageWidth()
  162. {
  163. if (BackgroundImage != null)
  164. {
  165. return RawWidth;
  166. }
  167. return 0;
  168. }
  169. public int GetBackgroundImageHeight()
  170. {
  171. if (BackgroundImage != null)
  172. {
  173. return RawHeight;
  174. }
  175. return 0;
  176. }
  177. public void SetTextsRects(List<KeyValuePair<Rect, string>> textsRect)
  178. {
  179. CleanChild();
  180. foreach (KeyValuePair<Rect, string> textRect in textsRect)
  181. {
  182. CustomDraw drawVisual = new CustomDraw();
  183. drawVisual.PaintText = textRect.Value;
  184. Rect paintRect = DpiHelpers.GetDpiUnrelatedRect(textRect.Key);
  185. drawVisual.RawRect = paintRect;
  186. drawVisual.PaintRect = new Rect((int)(paintRect.Left * ScaleRate + OffsetPos.X),
  187. (int)(paintRect.Top * ScaleRate + OffsetPos.Y),
  188. (int)(paintRect.Width * ScaleRate),
  189. (int)(paintRect.Height * ScaleRate));
  190. drawVisual.Draw();
  191. VisualControlList.Add(drawVisual);
  192. AddLogicalChild(drawVisual);
  193. AddVisualChild(drawVisual);
  194. }
  195. }
  196. public void RePaint()
  197. {
  198. InvalidateVisual();
  199. }
  200. }
  201. }