/// <summary> /// Draws rectangle annotation. /// </summary> /// <param name="image">Image.</param> /// <param name="rect">User specified area to annotate.</param> /// <param name="text">Label.</param> /// <param name="annotationWidth">Width of annotation rectangle.</param> /// <param name="color">Color for rectangle. Label area is filled. Default color is yellow-green.</param> /// <param name="textColor">Label color. Default color is black.</param> /// <param name="font">Font to use. Default is "Arial" of size 10, style: Bold.</param> /// <param name="thickness">Rectangle thickness.</param> public static void DrawAnnotation(this Image <Bgr, byte> image, Rectangle rect, string text, int annotationWidth = 100, Bgr color = default(Bgr), Bgr textColor = default(Bgr), Font font = null, int thickness = 1) { color = color.Equals(default(Bgr)) ? Color.YellowGreen.ToBgr() : color; textColor = textColor.Equals(default(Bgr)) ? Color.Black.ToBgr() : color; font = font ?? new Font("Arial", 8, System.Drawing.FontStyle.Bold); var nLines = text.Where(x => x.Equals('\n')).Count() + 1; var annotationHeight = (int)(3 + (font.SizeInPoints + 3) * nLines + 3); var xOffset = (annotationWidth - rect.Width) / 2; var annotationRect = new Rectangle(rect.X - xOffset, rect.Y - annotationHeight, annotationWidth, annotationHeight); image.Draw(annotationRect, color, thickness); image.Draw(rect, color, thickness); image.Draw(annotationRect, color, -1, 80); image.Draw(text, font, annotationRect, textColor); }