using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; namespace PDF_Master.CustomControl.ScanViewControl { public class CustomDraw : DrawingVisual { public Rect PaintRect; public Rect RawRect; public string PaintText; public double FontSize = 0; private Pen DrawPen; private SolidColorBrush CoverBrush = Brushes.Transparent;// new SolidColorBrush(Color.FromArgb(0x01, 0xFF, 0xFF, 0xFF)); public CustomDraw() { DrawPen = new Pen(Brushes.Red, 1); } public void Draw() { DrawingContext dc = RenderOpen(); dc.DrawRectangle(null, DrawPen, PaintRect); if (string.IsNullOrEmpty(PaintText) == false) { double fontSize = 1; FormattedText formatText = new FormattedText(PaintText, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(new FontFamily("Arial"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal), fontSize, Brushes.Green, Helper.DpiHelpers.Dpi / 96F); while (formatText.Width + fontSize < PaintRect.Width && formatText.Height < PaintRect.Height) { fontSize += 1; formatText.SetFontSize(fontSize); } if (fontSize > 1) { formatText.SetFontSize(fontSize - 1); } FontSize = fontSize; dc.DrawRectangle(Brushes.White, null, PaintRect); Array x = PaintText.ToCharArray(); double w = 0; List drawFormatList = new List(); for (int i = 0; i < x.Length; i++) { FormattedText formatText1 = new FormattedText(x.GetValue(i).ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(new FontFamily("Arial"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal), fontSize, Brushes.Green, Helper.DpiHelpers.Dpi / 96F); formatText1.SetFontSize(fontSize); w += formatText1.Width; drawFormatList.Add(formatText1); } Point startPos = new Point(PaintRect.Left, (int)(PaintRect.Top)); double subLength = PaintRect.Width - w; double offset = subLength / x.Length; foreach (FormattedText subFormat in drawFormatList) { dc.DrawText(subFormat, startPos); startPos.X += subFormat.Width + offset; } } dc.Close(); } public void DrawBounds() { DrawingContext dc = RenderOpen(); dc.DrawRectangle(CoverBrush, DrawPen, PaintRect); dc.Close(); } } }