// rendering protected override void OnRender(DrawingContext drawingContext) { // draw background drawingContext.PushClip(new RectangleGeometry(new Rect(0, 0, this.ActualWidth, this.ActualHeight))); drawingContext.DrawRectangle(BackgroundBrush, new Pen(), new Rect(0, 0, this.ActualWidth, this.ActualHeight)); // do the formatting FormattedText ft = new FormattedText( getRAMText(), System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily.Source), this.FontSize, ForegroundBrush); var left_margin = 4.0 + this.BorderThickness.Left; var top_margin = 2.0 + this.BorderThickness.Top; const int line_length = (3 * (COLUMNS + 1)) + 2; // Line Number + Columns + newline // Set the color of the first line ft.SetForegroundBrush(HeaderBrush, 0, line_length); // Set the colors of the first 2 chars in every line for (int i = 1; i < (COLUMNS + 1); i++) { ft.SetForegroundBrush(HeaderBrush, i * line_length, 2); } // Calculate the start positions of SP and IP // line + header + item in line int ip_start = (((ip / COLUMNS) + 1) * line_length) + ((ip % COLUMNS) + 1) * 3; int sp_start = (((sp / COLUMNS) + 1) * line_length) + ((sp % COLUMNS) + 1) * 3; // Set foreground and background of the IP Geometry geom = ft.BuildHighlightGeometry(new Point(left_margin, top_margin), ip_start, IsHex ? 2 : 1); if (geom != null) { drawingContext.DrawGeometry(IPBrush, null, geom); } ft.SetForegroundBrush(HighlightedForegroundBrush, ip_start, IsHex ? 2 : 1); // Set foreground and background of the SP Geometry geom2 = ft.BuildHighlightGeometry(new Point(left_margin, top_margin), sp_start, IsHex ? 2 : 1); if (geom2 != null) { drawingContext.DrawGeometry(SPBrush, null, geom2); } ft.SetForegroundBrush(HighlightedForegroundBrush, sp_start, IsHex ? 2 : 1); // draw text drawingContext.DrawText(ft, new Point(left_margin, top_margin)); }
protected override void OnRender(DrawingContext dc) { if (text != null) { if (rawStart < rawEnd) { Geometry g = text.BuildHighlightGeometry(renderPos, rawStart, rawEnd - rawStart); dc.DrawGeometry(rawError ? Brushes.Red : Brushes.Blue, null, g); } dc.DrawText(text, renderPos); } }
public void DrawString(string Text, Font aFont, Pen aPen, Brush aBrush, real x, real y) { if (PointOutside.Check(ref x, ref y)) { return; } Text = Text.Replace('\u000a', '\u0000'); //Replace newlines with empty characters if (aBrush != null) { DrawString(Text, aFont, aBrush, x, y); } if (aPen != null) { Typeface TextTypeface = new Typeface(aFont.Family, aFont.Style, aFont.Weight, FontStretches.Normal); FormattedText fmtText = new FormattedText(Text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, TextTypeface, aFont.SizeInPix, Brushes.Black); //Brush doesn't matter here. fmtText.SetTextDecorations(aFont.Decorations); Geometry TxtGeometry = fmtText.BuildHighlightGeometry(new Point(x, y)); if (TxtGeometry.CanFreeze) { TxtGeometry.Freeze(); } FCanvas.DrawGeometry(null, aPen, TxtGeometry); } }
private void RenderWithNoLeftElipsis(DrawingContext dc, PatternMatchResult matchingText, Rect bounds, FormattedText allText, int matchPosRightOffset) { var highlightBrush = GetHighlightBrush(); allText.Trimming = TextTrimming.CharacterEllipsis; allText.MaxTextWidth = bounds.Width; foreach (var matchPos in matchingText.MatchStarts) { // The offset position is used when the formatted text does not start at the beginning of the string var offsetPos = matchPos - matchPosRightOffset; var length = matchingText.Pattern.Length; if (offsetPos < 0) { length -= offsetPos; offsetPos = 0; } if (length > 0) { var geo = allText.BuildHighlightGeometry(bounds.TopLeft, offsetPos, length); dc.DrawGeometry(highlightBrush, null, geo); if (HighlightForegroundBrush != null) { allText.SetForegroundBrush(HighlightForegroundBrush, offsetPos, length); } } } dc.DrawText(allText, bounds.TopLeft); }
public static BitmapSource DrawSomething(BitmapSource baseImage, string text, Point location) { if (string.IsNullOrEmpty(text)) { return(baseImage); } Action <DrawingContext, double> drawAction = (drawingContext, annotationScale) => { FormattedText ft = new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, s_typeface, 16 * annotationScale, Brushes.Black); var pad = 3 * annotationScale; var ypad = pad; var xpad = pad + 4 * annotationScale; var rect = ft.BuildHighlightGeometry(location).GetRenderBounds(null); rect.Inflate(xpad, ypad); drawingContext.DrawRectangle(s_lineBrush, null, rect); drawingContext.DrawText(ft, location); }; return(DrawOverlay(baseImage, drawAction, true)); }
internal void RenderLines(DrawingContext dc) { for (int i = 0; i < _lineResults.Length; i++) { FixedLineResult lineResult = _lineResults[i]; Pen pen = new Pen(Brushes.Red, 1); Rect layoutBox = lineResult.LayoutBox; dc.DrawRectangle(null, pen, layoutBox); CultureInfo EnglishCulture = System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS; FixedPage fp = _fixedTextBuilder.FixedTextContainer.FixedDocument.GetFixedPage(PageIndex); FormattedText ft = new FormattedText(i.ToString(), EnglishCulture, FlowDirection.LeftToRight, new Typeface("Arial"), 10, Brushes.White, fp.GetDpi().PixelsPerDip); Point labelLocation = new Point(layoutBox.Left - 25, (layoutBox.Bottom + layoutBox.Top) / 2 - 10); Geometry geom = ft.BuildHighlightGeometry(labelLocation); Pen backgroundPen = new Pen(Brushes.Black, 1); dc.DrawGeometry(Brushes.Black, backgroundPen, geom); dc.DrawText(ft, labelLocation); } }
public void CreateText() { if (this.TextContent == null) { throw new NullReferenceException("TextContent value cannot be null."); } if (_geometryObjectChanged) { _formattedText = new FormattedText( this.TextContent, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface(new FontFamily(this.FontFamily), this.FontStyle, this.FontWeight, _fontStretch), this.FontSize, Brushes.Black); _textGeometry = _formattedText.BuildGeometry(new Point(0, this.Offset)); if (_highlight) { _textHighLightGeometry = _formattedText.BuildHighlightGeometry(new Point(0, 0)); } _geometryObjectChanged = false; } }
public static BitmapSource DrawFaces(BitmapSource baseImage, Microsoft.ProjectOxford.Face.Contract.Face[] faces, Microsoft.ProjectOxford.Face.Contract.Person person) { if (faces == null) { return(baseImage); } Action <DrawingContext, double> drawAction = (drawingContext, annotationScale) => { for (int i = 0; i < faces.Length; i++) { var face = faces[i]; if (face.FaceRectangle == null) { continue; } Rect faceRect = new Rect( face.FaceRectangle.Left, face.FaceRectangle.Top, face.FaceRectangle.Width, face.FaceRectangle.Height); string text = ""; if (person != null) { text += person.Name; } faceRect.Inflate(6 * annotationScale, 6 * annotationScale); double lineThickness = 4 * annotationScale; drawingContext.DrawRectangle( Brushes.Transparent, new Pen(s_lineBrush, lineThickness), faceRect); if (text != "") { FormattedText ft = new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, s_typeface, 16 * annotationScale, Brushes.Black); var pad = 3 * annotationScale; var ypad = pad; var xpad = pad + 4 * annotationScale; var origin = new System.Windows.Point( faceRect.Left + xpad - lineThickness / 2, faceRect.Top - ft.Height - ypad + lineThickness / 2); var rect = ft.BuildHighlightGeometry(origin).GetRenderBounds(null); rect.Inflate(xpad, ypad); drawingContext.DrawRectangle(s_lineBrush, null, rect); drawingContext.DrawText(ft, origin); } } }; return(DrawOverlay(baseImage, drawAction)); }
private void getformattedText(string str) { System.Windows.FontStyle fontStyle = FontStyles.Normal; FontWeight fontWeight = FontWeights.Normal; // Create the formatted text based on the properties set. FormattedText formattedText = new FormattedText( str, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface( FontFamily, FontStyle, FontWeight, FontStretch), FontSize, System.Windows.Media.Brushes.Black // This brush does not matter since we use the geometry of the text. ); this.Width = formattedText.Width; this.Height = formattedText.Height; // Build the geometry object that represents the text. //pg.AddGeometry(formattedText.BuildGeometry(new System.Windows.Point(5, 5))); TextGeometry = formattedText.BuildGeometry(new System.Windows.Point(0, 0)); // Build the geometry object that represents the text hightlight. if (Highlight == true) { TextHighLightGeometry = formattedText.BuildHighlightGeometry(new System.Windows.Point(0, 0)); } }
public static Size MeasureSize(string text, FontFamily fontFamily, double fontSize) { if (text == null) { text = ""; } System.Windows.FontStyle fontStyle = FontStyles.Normal; FontWeight fontWeight = FontWeights.Medium; var formattedText = new FormattedText( text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface( fontFamily, fontStyle, fontWeight, FontStretches.Normal), fontSize, System.Windows.Media.Brushes.Black ); Geometry _textGeometry = formattedText.BuildGeometry(new System.Windows.Point(0, 0)); Geometry _textHighLightGeometry = formattedText.BuildHighlightGeometry(new System.Windows.Point(0, 0)); return(_textHighLightGeometry == null ? new Size(0, 0) : _textHighLightGeometry.Bounds.Size); }
public void DrawText(Color?fillColor, DrawingPen pen, string textToDraw, Typeface typeface, double emSize, Point origin) { _needFilpCoordinate = false; origin = new Point(origin.X, _visual.Panel.ImageHeight - origin.Y); var textFormat = new FormattedText(textToDraw, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, emSize, null); for (int i = 0; i < textToDraw.Length; i++) { var charFormat = new FormattedText(textToDraw[i].ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, emSize, Brushes.White); _DrawPathGeometry(fillColor, pen, charFormat.BuildGeometry(textFormat.BuildHighlightGeometry(origin, i, 1).Bounds.TopLeft)); } _needFilpCoordinate = true; }
/// <summary> /// Creates the Geometry for the Strikethrough decoration, used with the GeometryMakerDelegate. /// </summary> /// <param name="text">The FormattedText used for the decoration</param> /// <param name="p">The pair defining the begining character and the length of the character range</param> /// <returns></returns> private Geometry StrikethroughGeometryMaker(FormattedText text, Pair p) { Geometry geom = text.BuildHighlightGeometry(new Point(0, 0), p.Start, p.Length); if (geom != null) { StackedRectangleGeometryHelper srgh = new StackedRectangleGeometryHelper(geom); return(srgh.CenterLineRectangleGeometry()); } else { return(null); } }
/// <summary> /// Creates the Geometry for the Underline decoration, used with the GeometryMakerDelegate. /// </summary> /// <param name="text">The FormattedText used for the decoration</param> /// <param name="p">The pair defining the begining character and the length of the character range</param> /// <returns></returns> private Geometry UnderlineGeometryMaker(FormattedText text, Pair p) { Geometry geom = text.BuildHighlightGeometry(new Point(0, 0), p.Start, p.Length); if (geom != null) { StackedRectangleGeometryHelper srgh = new StackedRectangleGeometryHelper(geom); return(srgh.BottomEdgeRectangleGeometry()); } else { return(null); } }
public void RenderLabel(DrawingContext dc, string label) { FormattedText ft = new FormattedText(label, System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS, FlowDirection.LeftToRight, new Typeface("Arial"), 10, Brushes.White); Point labelLocation = new Point(_boundingRect.Left - 25, (_boundingRect.Bottom + _boundingRect.Top) / 2 - 10); Geometry geom = ft.BuildHighlightGeometry(labelLocation); Pen backgroundPen = new Pen(Brushes.Black, 1); dc.DrawGeometry(Brushes.Black, backgroundPen, geom); dc.DrawText(ft, labelLocation); }
protected override void OnRender(DrawingContext dc) { if (text != null) { text.SetForegroundBrush(Brushes.Black); if (hiEnd > hiStart) { Geometry g = text.BuildHighlightGeometry(RenderPos, hiStart, hiEnd - hiStart); text.SetForegroundBrush(Brushes.White, hiStart, hiEnd - hiStart); dc.DrawGeometry(Brushes.Blue, null, g); } dc.DrawText(text, RenderPos); } }
private FormattedText FormatText(string str, System.Windows.Media.Brush brush, Typeface tf) { // function that is work to prepare for display function as to help compartmentalize all the necessary steps in making text changes to display in realtime FormattedText text = new FormattedText( str, CultureInfo.InvariantCulture, System.Windows.FlowDirection.LeftToRight, tf, bitmap.PixelHeight / 10, brush); // this nugget is necessary period to create a situation where no mods to the text are necessary to view on any pic background _textGeometry = text.BuildGeometry(new System.Windows.Point((bitmap.PixelWidth / 2) - (text.Width / 2), bitmap.PixelHeight - text.Height)); // this nugget is unnecessary and probably should be removed cause it really provides no feature or worth to any thing but is different in a manner of speaking _textHighlightGeometry = text.BuildHighlightGeometry(new System.Windows.Point((bitmap.PixelWidth / 2) - (text.Width / 2), bitmap.PixelHeight - text.Height)); return(text); }
private void InitTextInfo() { //Create the formatted text based on the properties set. var formattedText = new FormattedText( Text, Thread.CurrentThread.CurrentUICulture, FlowDirection.LeftToRight, new Typeface(FontFamily, FontStyle, FontWeight, FontStretches.Normal), FontSize, Brushes.Black); //This brush does not matter since we use the geometry of the text. //Build the geometry object that represents the text. _textGeometry = formattedText.BuildGeometry(new Point(0, 0)); //Build the geometry object that represents the text hightlight. _textHighLightGeometry = formattedText.BuildHighlightGeometry(new Point(0, 0)); }
public TickLabelVisual3D(string text, Brush textColor, bool bDoubleSided, double modelHeight, Point3D locationPoint, Location location, Vector3D textDirection, Vector3D up) { _up = up; _textDirection = textDirection; _doubleSide = bDoubleSided; FormattedText ft = new FormattedText(text, new CultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface(new FontFamily("Arial"), FontStyles.Normal, FontWeights.Normal, default(FontStretch)), 20.0, textColor, null, TextFormattingMode.Display); DrawingVisual ftVisual = new DrawingVisual(); DrawingContext drawingContext = ftVisual.RenderOpen(); drawingContext.DrawText(ft, new Point(1.0, 1.0)); drawingContext.Close(); _text = text; _screenHeight = 12.0; this.ScreenWidth = (double)text.Length * _screenHeight; double width = (double)text.Length * modelHeight; this.ModelHeight = modelHeight; this.ModelWidth = width; _locationPt = locationPoint; _location = location; _center = locationPoint; switch (location) { case Location.Start: _center += textDirection * width / 2.0; break; case Location.End: _center -= textDirection * width / 2.0; break; } Rect rect = ft.BuildHighlightGeometry(new Point(0.0, 0.0)).Bounds; RenderTargetBitmap bitmap = new RenderTargetBitmap((int)Math.Ceiling(rect.Width) + 1, (int)Math.Ceiling(rect.Height) + 1, 96.0, 96.0, PixelFormats.Pbgra32); bitmap.Render(ftVisual); bitmap.Freeze(); this.mat = new DiffuseMaterial(); ImageBrush imageBrush = new ImageBrush(bitmap); imageBrush.ViewportUnits = BrushMappingMode.RelativeToBoundingBox; this.mat.Brush = imageBrush; base.SubscribeToRenderingEvent(); }
public void RenderLabel(DrawingContext dc, string label) { // This code only runs in DEBUG mode, and looks like has been abandoned for a while. // Initializing PixelsPerDip to system dpi as a safeguard, however, doesn't look like this is going to be used at all. FormattedText ft = new FormattedText(label, System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS, FlowDirection.LeftToRight, new Typeface("Arial"), 10, Brushes.White, MS.Internal.FontCache.Util.PixelsPerDip); Point labelLocation = new Point(_boundingRect.Left - 25, (_boundingRect.Bottom + _boundingRect.Top) / 2 - 10); Geometry geom = ft.BuildHighlightGeometry(labelLocation); Pen backgroundPen = new Pen(Brushes.Black, 1); dc.DrawGeometry(Brushes.Black, backgroundPen, geom); dc.DrawText(ft, labelLocation); }
//</SnippetOnRender> //<SnippetCreateText> /// <summary> /// Create the outline geometry based on the formatted text. /// </summary> public void CreateText() { System.Windows.FontStyle fontStyle = FontStyles.Normal; FontWeight fontWeight = FontWeights.Medium; if (Bold == true) { fontWeight = FontWeights.Bold; } if (ExtraBold == true) { fontWeight = FontWeights.ExtraBold; } if (Italic == true) { fontStyle = FontStyles.Italic; } // Create the formatted text based on the properties set. FormattedText formattedText = new FormattedText( Text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface( Font, fontStyle, fontWeight, FontStretches.Normal), FontSize, System.Windows.Media.Brushes.Black // This brush does not matter since we use the geometry of the text. ); // Build the geometry object that represents the text. _textGeometry = formattedText.BuildGeometry(new System.Windows.Point(0, 0)); // Build the geometry object that represents the text highlight. if (Highlight == true) { _textHighLightGeometry = formattedText.BuildHighlightGeometry(new System.Windows.Point(0, 0)); } }
// Create the outline geometry based on the formatted text. public void CreateText() { // Create the formatted text based on the properties set. _formattedText = new FormattedText( Text, CultureInfo.GetCultureInfo(Culture), FlowDirection.LeftToRight, new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize, Brushes.Black // This brush does not matter since we use the geometry of the text. ); // Build the geometry object that represents the text. _textGeometry = _formattedText.BuildGeometry(new Point(0, 0)); // Build the geometry object that represents the text hightlight. if (Highlight) { _textHighLightGeometry = _formattedText.BuildHighlightGeometry(new Point(0, 0)); } }
public static BitmapSource DrawTime(BitmapSource baseImage, int time) { Action <DrawingContext, double> drawAction = (drawingContext, annotationScale) => { int posX = Convert.ToInt32(baseImage.Width / 2); int posY = Convert.ToInt32(baseImage.Height / 10); int width = Convert.ToInt32(baseImage.Width / 10); int height = Convert.ToInt32(baseImage.Height / 10); double lineThickness = 4 * annotationScale; var pad = 3 * annotationScale; var ypad = pad; var xpad = pad + 4 * annotationScale; var origin = new System.Windows.Point(posX, 0); var size = new System.Windows.Size(width, height); string strScore = string.Format("Tiempo Restante: {0}", time); FormattedText ft = new FormattedText(strScore, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, s_typeface, 16 * annotationScale, Brushes.Black); Rect imageRect = new Rect(origin, size); var rect = ft.BuildHighlightGeometry(origin).GetRenderBounds(null); rect.Inflate(xpad, ypad); drawingContext.DrawRectangle(s_lineBrush, null, rect); drawingContext.DrawText(ft, origin); }; return(DrawOverlay(baseImage, drawAction)); }
List <GeometryData> GetHighlightWordGeometry(FormattedText formattedText, Point textPoint) { var geometryList = new List <GeometryData>(HighlightWords.Count * 2); foreach (var word in HighlightWords) { var txtEnd = Text.Length; var curentIndex = 0; var lastIndex = Text.LastIndexOf(word, StringComparison.OrdinalIgnoreCase); while (curentIndex <= lastIndex) { curentIndex = Text.IndexOf(word, curentIndex, StringComparison.OrdinalIgnoreCase); var geometry = formattedText.BuildHighlightGeometry(textPoint, curentIndex, word.Length); if (geometry != null) { geometryList.Add(new GeometryData(geometry, curentIndex, word.Length)); } curentIndex += word.Length; } } return(geometryList); }
private void RenderLeftRightTrimmedText(DrawingContext dc, Rect bounds, PatternMatchResult matchingText, FormattedText allText) { var leadingElipsisText = FormatText("...", bounds.Height); var leadingElipsisGeo = leadingElipsisText.BuildHighlightGeometry(bounds.TopLeft); var leadingGeo = allText.BuildHighlightGeometry(bounds.TopLeft, 0, matchingText.MatchStarts[0]); var highlightGeo = allText.BuildHighlightGeometry(bounds.TopLeft, matchingText.MatchStarts[0], matchingText.Pattern.Length); // We know it does not fit and we know it does not start with a match if (leadingGeo == null || highlightGeo == null || leadingElipsisGeo == null || (leadingGeo.Bounds.Width < leadingElipsisGeo.Bounds.Width)) { // the leading text is shorter than the elipsis text // Just write the leading text, the match, and then the trailtext w/elipsis // No left elipsis needed RenderWithNoLeftElipsis(dc, matchingText, bounds, allText, 0); return; } var availableWidth = bounds.Width; availableWidth -= leadingElipsisGeo.Bounds.Width * 2; availableWidth -= highlightGeo.Bounds.Width; var leadingTextSource = matchingText.GetTextBefore(0); var trailingTextSource = matchingText.GetTextAfter(0); var leadingText = ""; var trailingText = ""; var shrunkTrailing = FormatText(trailingText, bounds.Height); var shrunkLeading = FormatText(leadingText, bounds.Height); var initialLeadingSize = FormatText(leadingTextSource, bounds.Height).WidthIncludingTrailingWhitespace; var appendLettersPassCount = 0; var leadingPassPadding = (matchingText.Pattern.Length) / 2; while ((shrunkLeading.WidthIncludingTrailingWhitespace + shrunkTrailing.WidthIncludingTrailingWhitespace) < availableWidth) { // Add characters until the width is as wide as the column // This roughly centers the highlighted text between the leading and trailing parts if (appendLettersPassCount > leadingPassPadding) { if (leadingText.Length < leadingTextSource.Length) { leadingText = leadingTextSource.Substring(leadingTextSource.Length - leadingText.Length - 1, leadingText.Length + 1); shrunkLeading = FormatText(leadingText, bounds.Height); } if (shrunkLeading.WidthIncludingTrailingWhitespace + shrunkTrailing.WidthIncludingTrailingWhitespace >= availableWidth) { break; } } if (trailingText.Length < trailingTextSource.Length) { trailingText = trailingTextSource.Substring(0, trailingText.Length + 1); shrunkTrailing = FormatText(trailingText, bounds.Height); } if (leadingText == leadingTextSource && trailingText == trailingTextSource) { // Somehow it managed to fit break; } appendLettersPassCount++; } if (leadingText == leadingTextSource || initialLeadingSize - shrunkLeading.WidthIncludingTrailingWhitespace < leadingElipsisText.WidthIncludingTrailingWhitespace) { // The whole leading text is there, no elipsis required RenderWithNoLeftElipsis(dc, matchingText, bounds, allText, 0); return; } // Draw the elipsis dc.DrawText(leadingElipsisText, bounds.TopLeft); var shrunkText = leadingText + matchingText.GetTextFromBeginningOfMatchOn(0); // Draw the remainder as a new match //var remainingMatch = new PatternMatchResult(matchingText.Pattern, shrunkText, 100); if (bounds.Width - leadingElipsisText.Width > 0) { var remainingBounds = new Rect(bounds.Left + leadingElipsisText.Width, bounds.Top, bounds.Width - leadingElipsisText.Width, bounds.Height); int matchPosOffset = leadingTextSource.Length - leadingText.Length; RenderWithNoLeftElipsis(dc, matchingText, remainingBounds, FormatText(shrunkText, bounds.Height), matchPosOffset); } }
public static BitmapSource DrawFaces(BitmapSource baseImage, FaceAPI.Face[] faces, ObservableCollection <Microsoft.ProjectOxford.Face.Controls.Face> targetFaces, EmotionScores[] emotionScores, string[] celebName, List <PersonData> personData, DataTable dataTable, ImageWall imageWall) { if (faces == null) { return(baseImage); } Action <DrawingContext, BitmapSource, double> drawAction = (drawingContext, oriImage, annotationScale) => { for (int i = 0; i < faces.Length; i++) { if (targetFaces[i].PersonName == "Unknown") { continue; } var face = faces[i]; imageWall.colorful[imageWall.id.IndexOf(targetFaces[i].FaceId)] = true; if (face.FaceRectangle == null) { continue; } PersonData pD = new PersonData(); try { pD = personData.Find(x => x.ID == targetFaces[i].FaceId); pD.Times++; } catch (Exception) { personData.Find(x => x.ID == targetFaces[i].FaceId); } Rect faceRect = new Rect( face.FaceRectangle.Left, face.FaceRectangle.Top, face.FaceRectangle.Width, face.FaceRectangle.Height); Int32Rect faceRectInt32 = new Int32Rect( face.FaceRectangle.Left, face.FaceRectangle.Top, face.FaceRectangle.Width, face.FaceRectangle.Height); string text = ""; drawingContext.DrawImage(new CroppedBitmap(oriImage, faceRectInt32), faceRect); if (face.FaceAttributes != null) { text += Aggregation.SummarizeFaceAttributes(face.FaceAttributes, targetFaces[i].PersonName, pD); } if (emotionScores?[i] != null) { text += Aggregation.SummarizeEmotion(emotionScores[i]); } if (celebName?[i] != null) { text += celebName[i]; } faceRect.Inflate(6 * annotationScale, 6 * annotationScale); double lineThickness = 4 * annotationScale; drawingContext.DrawRectangle( Brushes.Transparent, new Pen(s_lineBrush, lineThickness), faceRect); if (text != "") { FormattedText ft = new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, s_typeface, 16 * annotationScale, Brushes.Black); var pad = 3 * annotationScale; var ypad = pad; var xpad = pad + 4 * annotationScale; var origin = new System.Windows.Point( faceRect.Left + xpad - lineThickness / 2, faceRect.Top - ft.Height - ypad + lineThickness / 2); var rect = ft.BuildHighlightGeometry(origin).GetRenderBounds(null); rect.Inflate(xpad, ypad); drawingContext.DrawRectangle(s_lineBrush, null, rect); drawingContext.DrawText(ft, origin); } } dataTable.dataGrid.ItemsSource = personData; imageWall.UpdateCanvas(); }; return(DrawOverlay(baseImage, drawAction)); }
public static BitmapSource DrawFaces(BitmapSource baseImage, FaceAPI.Models.DetectedFace[] faces, string[] celebName) { if (faces == null) { return(baseImage); } Action <DrawingContext, double> drawAction = (drawingContext, annotationScale) => { for (int i = 0; i < faces.Length; i++) { var face = faces[i]; if (face.FaceRectangle == null) { continue; } Rect faceRect = new Rect( face.FaceRectangle.Left, face.FaceRectangle.Top, face.FaceRectangle.Width, face.FaceRectangle.Height); var summary = new StringBuilder(); if (face.FaceAttributes != null) { summary.Append(Aggregation.SummarizeFaceAttributes(face.FaceAttributes)); } if (face.FaceAttributes?.Emotion != null) { summary.Append(Aggregation.SummarizeEmotion(face.FaceAttributes.Emotion)); } if (celebName?[i] != null) { summary.Append(celebName[i]); } faceRect.Inflate(6 * annotationScale, 3 * annotationScale); double lineThickness = 3 * annotationScale; drawingContext.DrawRectangle( Brushes.Transparent, new Pen(s_lineBrush, lineThickness), faceRect); if (summary.Length > 0) { FormattedText ft = new FormattedText(summary.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, s_typeface, 16 * annotationScale, Brushes.Black); var pad = 3 * annotationScale; var ypad = pad; var xpad = pad + 4 * annotationScale; var origin = new System.Windows.Point( faceRect.Left + xpad - lineThickness / 2, faceRect.Top - ft.Height - ypad + lineThickness / 2); var rect = ft.BuildHighlightGeometry(origin).GetRenderBounds(null); rect.Inflate(xpad, ypad); drawingContext.DrawRectangle(s_lineBrush, null, rect); drawingContext.DrawText(ft, origin); } } }; return(DrawOverlay(baseImage, drawAction)); }
public static BitmapSource DrawResults(BitmapSource baseImage, List <Player> players) { if (players == null) { return(baseImage); } int playerMaxScore = (players.Count > 0) ? players.Max(p => p.Score) : 0; Action <DrawingContext, double> drawAction = (drawingContext, annotationScale) => { for (int i = 0; i < players.Count; i++) { var player = players[i]; if (player.Position == null) { continue; } Rect faceRect = new Rect( player.Position.Left, player.Position.Top, player.Position.Width, player.Position.Height); string msgDefault = "Menor Puntaje: {0}"; faceRect.Inflate(6 * annotationScale, 6 * annotationScale); double lineThickness = 4 * annotationScale; var brush = s_lineBrush; if (player.Score == playerMaxScore) { brush = s_winLineBrush; msgDefault = "Puntaje Ganador: {0}"; } string score = string.Format(msgDefault, player.Score); drawingContext.DrawRectangle( Brushes.Transparent, new Pen(brush, lineThickness), faceRect); FormattedText ft = new FormattedText(score, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, s_typeface, 16 * annotationScale, Brushes.Black); var pad = 3 * annotationScale; var ypad = pad; var xpad = pad + 4 * annotationScale; var origin = new System.Windows.Point( faceRect.Left + xpad - lineThickness / 2, faceRect.Top - ft.Height - ypad + lineThickness / 2); var rect = ft.BuildHighlightGeometry(origin).GetRenderBounds(null); rect.Inflate(xpad, ypad); drawingContext.DrawRectangle(brush, null, rect); drawingContext.DrawText(ft, origin); } }; return(DrawOverlay(baseImage, drawAction)); }
public static BitmapSource DrawFaces(BitmapSource baseImage, Microsoft.ProjectOxford.Face.Contract.Face[] faces, Scores[] emotionScores, string[] celebName) { if (faces == null) { return(baseImage); } AsyncContext.Run(() => MainWindow.identify(faces)); Action <DrawingContext, double> drawAction = (drawingContext, annotationScale) => { for (int i = 0; i < faces.Length; i++) { var face = faces[i]; if (face.FaceRectangle == null) { continue; } Rect faceRect = new Rect( face.FaceRectangle.Left, face.FaceRectangle.Top, face.FaceRectangle.Width, face.FaceRectangle.Height); string text = ""; if (names[i] != null) { text = names[i] + " "; } if (face.FaceAttributes != null) { text += Aggregation.SummarizeFaceAttributes(face.FaceAttributes); } if (emotionScores?[i] != null) { text += Aggregation.SummarizeEmotion(emotionScores[i]); } if (celebName?[i] != null) { text += celebName[i]; } faceRect.Inflate(6 * annotationScale, 6 * annotationScale); double lineThickness = 4 * annotationScale; drawingContext.DrawRectangle( Brushes.Transparent, new Pen(s_lineBrush, lineThickness), faceRect); if (text != "") { FormattedText ft = new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, s_typeface, 16 * annotationScale, Brushes.Black); var pad = 3 * annotationScale; var ypad = pad; var xpad = pad + 4 * annotationScale; var origin = new Point( faceRect.Left + xpad - lineThickness / 2, faceRect.Top - ft.Height - ypad + lineThickness / 2); var rect = ft.BuildHighlightGeometry(origin).GetRenderBounds(null); rect.Inflate(xpad, ypad); drawingContext.DrawRectangle(s_lineBrush, null, rect); drawingContext.DrawText(ft, origin); } } }; return(DrawOverlay(baseImage, drawAction)); }
/// <summary> /// Create the outline geometry based on the formatted text. /// </summary> public void CreateText() { FontStyle fontStyle = FontStyles.Normal; FontWeight fontWeight = FontWeights.Medium; var fontDecoration = new TextDecorationCollection(); if (Bold == true) { fontWeight = FontWeights.Bold; } if (Italic == true) { fontStyle = FontStyles.Italic; } if (Underline) { fontDecoration.Add(TextDecorations.Underline); } // Create the formatted text based on the properties set. FormattedText formattedText = new FormattedText( string.IsNullOrEmpty(Text) ? string.Empty : Text.Trim(), CultureInfo.GetCultureInfo(System.Threading.Thread.CurrentThread.CurrentUICulture.Name), System.Threading.Thread.CurrentThread.CurrentUICulture.TextInfo.IsRightToLeft ? FlowDirection.RightToLeft : FlowDirection.LeftToRight, new Typeface(Font, fontStyle, fontWeight, FontStretches.Normal), FontSize, Brushes.Black // This brush does not matter since we use the geometry of the text. ); formattedText.TextAlignment = TextAlignment.Center; //if (this.Parent is FrameworkElement) //{ // var parentWidth = GetParentWidth(this); // if (!double.IsNaN(parentWidth)) // { // if (this.Parent is System.Windows.Controls.Border) // { // parentWidth -= (this.Parent as System.Windows.Controls.Border).BorderThickness.Left; // parentWidth -= (this.Parent as System.Windows.Controls.Border).BorderThickness.Right; // parentWidth -= (this.Parent as System.Windows.Controls.Border).Padding.Left; // parentWidth -= (this.Parent as System.Windows.Controls.Border).Padding.Right; // } // formattedText.MaxTextWidth = parentWidth - this.Margin.Left - this.Margin.Right; // } //} formattedText.SetTextDecorations(fontDecoration); // Build the geometry object that represents the text. _textGeometry = formattedText.BuildGeometry(new Point(0, 0)); _textHighlightGeometry = formattedText.BuildHighlightGeometry(new Point(0, 0)); var transform = new TranslateTransform(-1 * _textGeometry.Bounds.X, 0); //var enlarge = new ScaleTransform(1.1, 0, 0.5, 0.5); // TranslateTransform(-1 * _textGeometry.Bounds.X, 0); if (_textGeometry != null) { _textGeometry = Geometry.Combine(_textGeometry, _textGeometry, GeometryCombineMode.Intersect, transform); } if (_textHighlightGeometry != null) { _textHighlightGeometry = Geometry.Combine(_textHighlightGeometry, _textHighlightGeometry, GeometryCombineMode.Intersect, transform); } //if (_textHighlightGeometry != null) _textHighlightGeometry = Geometry.Combine(_textHighlightGeometry, _textHighlightGeometry, GeometryCombineMode.Union, enlarge); //set the size of the custome control based on the size of the text //this.MinWidth = formattedText.Width; //this.MinHeight = formattedText.Height; if (!string.IsNullOrEmpty(Text)) { this.Width = _textGeometry.Bounds.Width; // formattedText.Width; this.Height = _textGeometry.Bounds.Height; // formattedText.Height; } }
protected override void OnRender(System.Windows.Media.DrawingContext drawingContext) { //base.OnRender(drawingContext); if (this.Text != "") { FormattedText formattedText = null; double leftMargin = 4.0 + this.BorderThickness.Left; double topMargin = 2 + this.BorderThickness.Top; if (IsAllowFormatSqlContent) { #region MyRendering EnsureScrolling(); formattedText = new FormattedText( this.Text, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface(this.FontFamily.Source), this.FontSize, BaseForeground); //Text that matches the textbox's formattedText.MaxTextWidth = this.ViewportWidth; // space for scrollbar formattedText.MaxTextHeight = Math.Max(this.ActualHeight + this.VerticalOffset, 0); //Adjust for scrolling drawingContext.PushClip(new RectangleGeometry(new Rect(0, 0, this.ActualWidth, this.ActualHeight))); //restrict text to textbox //Background hilight foreach (Decoration dec in mDecorations) { if (dec.DecorationType == EDecorationType.Hilight) { List <Pair> ranges = dec.Ranges(this.Text); foreach (Pair p in ranges) { Geometry geom = formattedText.BuildHighlightGeometry(new Point(leftMargin, topMargin - this.VerticalOffset), p.Start, p.Length); if (geom != null) { #if DEBUG Debug.WriteLine("Draw geometry"); #endif drawingContext.DrawGeometry(dec.Brush, null, geom); } } } } //Underline foreach (Decoration dec in mDecorations) { if (dec.DecorationType == EDecorationType.Underline) { List <Pair> ranges = dec.Ranges(this.Text); foreach (Pair p in ranges) { Geometry geom = formattedText.BuildHighlightGeometry(new Point(leftMargin, topMargin - this.VerticalOffset), p.Start, p.Length); if (geom != null) { StackedRectangleGeometryHelper srgh = new StackedRectangleGeometryHelper(geom); foreach (Geometry g in srgh.BottomEdgeRectangleGeometries()) { #if DEBUG Debug.WriteLine("Draw geometry2"); #endif drawingContext.DrawGeometry(dec.Brush, null, g); } } } } } //Strikethrough foreach (Decoration dec in mDecorations) { if (dec.DecorationType == EDecorationType.Strikethrough) { List <Pair> ranges = dec.Ranges(this.Text); foreach (Pair p in ranges) { Geometry geom = formattedText.BuildHighlightGeometry(new Point(leftMargin, topMargin - this.VerticalOffset), p.Start, p.Length); if (geom != null) { StackedRectangleGeometryHelper srgh = new StackedRectangleGeometryHelper(geom); foreach (Geometry g in srgh.CenterLineRectangleGeometries()) { #if DEBUG Debug.WriteLine("Draw geometry3"); #endif drawingContext.DrawGeometry(dec.Brush, null, g); } } } } } #region TextColor foreach (Decoration dec in mDecorations) { if (dec.DecorationType == EDecorationType.TextColor) { List <Pair> ranges = dec.Ranges(this.Text); foreach (Pair p in ranges) { #if DEBUG Debug.WriteLine("Set background brush" + p.Start.ToString()); #endif //this method will paint the text ,it will cost many ,many times, //if the sql script large enough (more than 100k) //So I'd rather use simplest textbox with no formatted sql text replace this control. formattedText.SetForegroundBrush(dec.Brush, p.Start, p.Length); } } } #endregion #if DEBUG Debug.WriteLine("End drawing"); #endif #endregion } if (formattedText == null) { this.Foreground = new SolidColorBrush(Colors.Black); } drawingContext.DrawText(formattedText, new Point(leftMargin, topMargin - this.VerticalOffset)); } }