/// <summary> /// Generates the drawing of the measurements. /// </summary> /// <param name="sourceWidth">The width of the source image.</param> /// <param name="sourceHeight">The height of the source image.</param> /// <param name="displayWidth">The width of the container to put the bitmap. Scales the size of the drawings.</param> /// <param name="displayHeight">The height of the container to put the bitmap. Scales the size of the drawings.</param> /// <param name="adornerLayerManager">The adorner information object.</param> /// <param name="background">The background to create the drawing on.</param> public static DrawingVisual GetMeasurementDrawing(double sourceWidth, double sourceHeight, double displayWidth, double displayHeight, AdornerLayerManager adornerLayerManager, Brush background) { DrawingVisual dw = new DrawingVisual(); using (DrawingContext dc = dw.RenderOpen()) { Size sz = new Size((int)sourceWidth, (int)sourceHeight); Pen p = new Pen(); Point pt = new Point(1, 1); dc.DrawRectangle(background, p, new System.Windows.Rect(pt, sz)); double widthRatio = sourceWidth / displayWidth; double heightRatio = sourceHeight / displayHeight; double Ratio = (widthRatio < heightRatio) ? widthRatio : heightRatio; MeasureAdorner measAdorner = (MeasureAdorner)adornerLayerManager.GetAdorner(AdornerLayerManager.MEASUREMENT_ADORNER); foreach (MeasurementLine lineObj in measAdorner.GetMeasurementLines()) { SolidColorBrush lscb = Brushes.Green; Pen lp1 = new Pen(lscb, 5d * Ratio); dc.DrawLine(lp1, lineObj.StartPoint, lineObj.EndPoint); float lineLength = (lineObj.Length * measAdorner.SamplingSpace) / 1000; FormattedText formattedText = new FormattedText(lineLength.ToString("F") + "m", CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Veranda"), 32, Brushes.Green); //formattedText.MaxTextWidth = 300; //formattedText.MaxTextHeight = 240; //formattedText.SetFontSize(32 * (96.0 / 72.0)); PrintDialog tempPrnDialog = new PrintDialog(); if (sourceWidth > sourceHeight) { formattedText.SetFontSize((sourceWidth / tempPrnDialog.PrintableAreaWidth) * 12); } else formattedText.SetFontSize((sourceHeight / tempPrnDialog.PrintableAreaHeight) * 15); Point textMidPoint = new Point(lineObj.MidPoint.X - (formattedText.Width / 2), lineObj.MidPoint.Y - (formattedText.Height / 2)); Rect LegendRect; LegendRect = new Rect(textMidPoint, new Size(formattedText.Width, formattedText.Height)); SolidColorBrush scb = Brushes.Green; Pen p1 = new Pen(scb, (5D * Ratio)); RectangleGeometry rg = new RectangleGeometry(LegendRect, 0, 0); dc.DrawGeometry(Brushes.White, null, rg); dc.DrawText(formattedText, textMidPoint); } } return dw; }
/// <summary> /// Generates the drawing of the annotations. /// </summary> /// <param name="sourceWidth">The width of the source image.</param> /// <param name="sourceHeight">The height of the source image.</param> /// <param name="displayWidth">The width of the container to put the bitmap. Scales the size of the drawings.</param> /// <param name="displayHeight">The height of the container to put the bitmap. Scales the size of the drawings.</param> /// <param name="adornerLayerManager">The adorner information object.</param> /// <param name="background">The background to create the drawing on.</param> public static DrawingVisual GetAnnotationDrawing(double sourceWidth, double sourceHeight, double displayWidth, double displayHeight, AdornerLayerManager adornerLayerManager, Brush background) { DrawingVisual dw = new DrawingVisual(); using (DrawingContext dc = dw.RenderOpen()) { Size sz = new Size((int)sourceWidth, (int)sourceHeight); Pen p = new Pen(); Point pt = new Point(1, 1); dc.DrawRectangle(background, p, new System.Windows.Rect(pt, sz)); double widthRatio = sourceWidth / displayWidth; double heightRatio = sourceHeight / displayHeight; double Ratio = (widthRatio < heightRatio) ? widthRatio : heightRatio; char[] letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; int count = 0; AnnotationAdorner annotAdorner = (AnnotationAdorner)adornerLayerManager.GetAdorner(AdornerLayerManager.ANNOTATION_ADORNER); foreach (Annotation annotation in annotAdorner.GetAnnotations()) { Rect rect = annotation.Marking.Rect; RectangleGeometry rg = new RectangleGeometry(rect, annotation.Marking.RadiusX, annotation.Marking.RadiusY); //rg. SolidColorBrush scb = Brushes.Green; Pen p1 = new Pen(scb, (5D * Ratio)); dc.DrawGeometry(null, p1, rg); string index = string.Empty; int div = (count / 26) - 1; int rem = count % 26; if (div >= 0) { index += letters[div]; } index += letters[rem]; FormattedText formattedText = new FormattedText(index, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface("Aharoni"), 32, Brushes.Green); //formattedText.MaxTextWidth = 300; //formattedText.SetFontSize(32 * (96.0 / 72.0)); PrintDialog tempPrnDialog = new PrintDialog(); if (sourceWidth > sourceHeight) { formattedText.SetFontSize((sourceWidth / tempPrnDialog.PrintableAreaWidth) * 11); } else formattedText.SetFontSize((sourceHeight / tempPrnDialog.PrintableAreaHeight) * 13); Rect LegendRect; if (annotation.Marking.RadiusX > 0) LegendRect = new Rect(annotation.Marking.Rect.TopLeft.X + (annotation.Marking.Rect.Width / 2) - ((formattedText.WidthIncludingTrailingWhitespace + 10) / 2), annotation.Marking.Rect.TopLeft.Y - formattedText.Height - 5, formattedText.WidthIncludingTrailingWhitespace + 12, formattedText.Height + 5); else LegendRect = new Rect(annotation.Marking.Rect.TopLeft.X, annotation.Marking.Rect.TopLeft.Y - formattedText.Height - 5, formattedText.WidthIncludingTrailingWhitespace + 12, formattedText.Height + 5); rg = new RectangleGeometry(LegendRect, 0, 0); dc.DrawGeometry(Brushes.LightYellow, p1, rg); dc.DrawText(formattedText, new Point(LegendRect.TopLeft.X + 5, LegendRect.TopLeft.Y + 5)); count++; } } return dw; }