123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- 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<FormattedText> drawFormatList = new List<FormattedText>();
- 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();
- }
- }
- }
|