CustomDraw.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Media;
  9. namespace PDF_Master.CustomControl.ScanViewControl
  10. {
  11. public class CustomDraw : DrawingVisual
  12. {
  13. public Rect PaintRect;
  14. public Rect RawRect;
  15. public string PaintText;
  16. public double FontSize = 0;
  17. private Pen DrawPen;
  18. private SolidColorBrush CoverBrush = Brushes.Transparent;// new SolidColorBrush(Color.FromArgb(0x01, 0xFF, 0xFF, 0xFF));
  19. public CustomDraw()
  20. {
  21. DrawPen = new Pen(Brushes.Red, 1);
  22. }
  23. public void Draw()
  24. {
  25. DrawingContext dc = RenderOpen();
  26. dc.DrawRectangle(null, DrawPen, PaintRect);
  27. if (string.IsNullOrEmpty(PaintText) == false)
  28. {
  29. double fontSize = 1;
  30. FormattedText formatText = new FormattedText(PaintText,
  31. CultureInfo.CurrentCulture,
  32. FlowDirection.LeftToRight,
  33. new Typeface(new FontFamily("Arial"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
  34. fontSize,
  35. Brushes.Green, Helper.DpiHelpers.Dpi / 96F);
  36. while (formatText.Width + fontSize < PaintRect.Width && formatText.Height < PaintRect.Height)
  37. {
  38. fontSize += 1;
  39. formatText.SetFontSize(fontSize);
  40. }
  41. if (fontSize > 1)
  42. {
  43. formatText.SetFontSize(fontSize - 1);
  44. }
  45. FontSize = fontSize;
  46. dc.DrawRectangle(Brushes.White, null, PaintRect);
  47. Array x = PaintText.ToCharArray();
  48. double w = 0;
  49. List<FormattedText> drawFormatList = new List<FormattedText>();
  50. for (int i = 0; i < x.Length; i++)
  51. {
  52. FormattedText formatText1 = new FormattedText(x.GetValue(i).ToString(),
  53. CultureInfo.CurrentCulture,
  54. FlowDirection.LeftToRight,
  55. new Typeface(new FontFamily("Arial"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
  56. fontSize,
  57. Brushes.Green, Helper.DpiHelpers.Dpi / 96F);
  58. formatText1.SetFontSize(fontSize);
  59. w += formatText1.Width;
  60. drawFormatList.Add(formatText1);
  61. }
  62. Point startPos = new Point(PaintRect.Left, (int)(PaintRect.Top));
  63. double subLength = PaintRect.Width - w;
  64. double offset = subLength / x.Length;
  65. foreach (FormattedText subFormat in drawFormatList)
  66. {
  67. dc.DrawText(subFormat, startPos);
  68. startPos.X += subFormat.Width + offset;
  69. }
  70. }
  71. dc.Close();
  72. }
  73. public void DrawBounds()
  74. {
  75. DrawingContext dc = RenderOpen();
  76. dc.DrawRectangle(CoverBrush, DrawPen, PaintRect);
  77. dc.Close();
  78. }
  79. }
  80. }