/// <summary> /// Draw a string with the given content alignment /// </summary> /// <param name="g"></param> /// <param name="str">String to draw</param> /// <param name="font">Font to use to draw this string</param> /// <param name="brush">Brush to use to draw this string</param> /// <param name="rect">Rectangle with the string (<b>no cutting!</b>)</param> /// <param name="alignment">The alignment of string to the given rectangle</param> public static void DrawString(this Graphics g, String str, Font font, Brush brush, RectangleF rect, ContentAlignment alignment) { SizeF size = font.GetStringSize(str); float x = rect.X; float y = rect.Y; switch (alignment) { case ContentAlignment.BottomCenter: x = GetAlignment(Alignment.Center, rect.X, rect.Width, size.Width); y = GetAlignment(Alignment.RightBottom, rect.Y, rect.Height, size.Height); break; case ContentAlignment.BottomLeft: x = GetAlignment(Alignment.LeftTop, rect.X, rect.Width, size.Width); y = GetAlignment(Alignment.RightBottom, rect.Y, rect.Height, size.Height); break; case ContentAlignment.BottomRight: x = GetAlignment(Alignment.RightBottom, rect.X, rect.Width, size.Width); y = GetAlignment(Alignment.RightBottom, rect.Y, rect.Height, size.Height); break; case ContentAlignment.MiddleCenter: x = GetAlignment(Alignment.Center, rect.X, rect.Width, size.Width); y = GetAlignment(Alignment.Center, rect.Y, rect.Height, size.Height); break; case ContentAlignment.MiddleLeft: x = GetAlignment(Alignment.LeftTop, rect.X, rect.Width, size.Width); y = GetAlignment(Alignment.Center, rect.Y, rect.Height, size.Height); break; case ContentAlignment.MiddleRight: x = GetAlignment(Alignment.RightBottom, rect.X, rect.Width, size.Width); y = GetAlignment(Alignment.Center, rect.Y, rect.Height, size.Height); break; case ContentAlignment.TopCenter: x = GetAlignment(Alignment.Center, rect.X, rect.Width, size.Width); y = GetAlignment(Alignment.LeftTop, rect.Y, rect.Height, size.Height); break; case ContentAlignment.TopLeft: x = GetAlignment(Alignment.LeftTop, rect.X, rect.Width, size.Width); y = GetAlignment(Alignment.LeftTop, rect.Y, rect.Height, size.Height); break; case ContentAlignment.TopRight: x = GetAlignment(Alignment.RightBottom, rect.X, rect.Width, size.Width); y = GetAlignment(Alignment.LeftTop, rect.Y, rect.Height, size.Height); break; default: throw new NotImplementedException(); } g.DrawString(str, font, brush, new PointF(x, y)); }