是否可以在c#中将垂直文本对齐设置为格式化文本 例如 我的格式化文本是
var formattedtext=new FormattedText("some string", CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, this.GetTypeface(), 12.0, Brushes.Black);
我需要将垂直对齐中心的格式化文本与特定的rect区域对齐。
答案 0 :(得分:0)
您可以尝试这样的事情:
// e is the PaintEventArgs, which is passed as a parameter
// construct a new Rectangle .
Rectangle displayRectangle =
new Rectangle (new Point(40, 40), new Size (80, 80));
// construct new StringFormat object
var format = new StringFormat(StringFormatFlags.DirectionVertical);
// set the LineAlignment and Alignment properties for
format.LineAlignment = StringAlignment.Near;
format.Alignment = StringAlignment.Center;
// draw the bounding rectangle and a string for the StringFormat object.
e.Graphics.DrawRectangle(Pens.Black, displayRectangle);
e.Graphics.DrawString("Showing Format", this.Font,
Brushes.Red, (RectangleF)displayRectangle, format);
答案 1 :(得分:0)
以下是如何实现这一目标的方法。 在文本溢出限制的情况下,我的方法也将缩小以适应边界框。
/// <summary>
/// Use to draw some text using font file location.
/// </summary>
/// <param name="font">Font file location</param>
/// <param name="someText"></param>
/// <param name="fontSize"></param>
/// <param name="width">bitmap width</param>
/// <param name="height">bitmap height</param>
/// <returns>new instance of RenderTargetBitmap</returns>
private static RenderTargetBitmap DrawText(string font, string someText, int fontSize, int width = 700, int height = 300)
{
var name = Path.GetFileName(font);
var glyphTypeface = new GlyphTypeface(new Uri(font));
string family = String.Join(" ", glyphTypeface.FamilyNames.Values.ToArray<string>());
var style = glyphTypeface.Style;
var weight = glyphTypeface.Weight;
var fontStretch = glyphTypeface.Stretch;
string fontUri = new Uri(font.Replace(name, ""), UriKind.RelativeOrAbsolute).AbsoluteUri + "/#" + family;
var fontFamily = new FontFamily(fontUri);
var typeface = new Typeface(fontFamily, style, weight, fontStretch);
var formattedText = new FormattedText(someText, System.Globalization.CultureInfo.InvariantCulture,
FlowDirection.LeftToRight,
typeface, fontSize,
Brushes.Black);
formattedText.TextAlignment = TextAlignment.Center;
var drawingVisual = new DrawingVisual();
RenderOptions.SetBitmapScalingMode(drawingVisual, BitmapScalingMode.HighQuality);
RenderOptions.SetEdgeMode(drawingVisual, EdgeMode.Aliased);
TextOptions.SetTextRenderingMode(drawingVisual, TextRenderingMode.Aliased);
using (var drawingContext = drawingVisual.RenderOpen())
{
drawingContext.DrawRectangle(Brushes.White, null, new Rect(0, 0, width, height));
var geometry = formattedText.BuildGeometry(new Point(0, 0));
var bounds = geometry.Bounds;
var sW = width / bounds.Width;
var sH = height / bounds.Height;
var ratio = sH <= sW ? sH : sW;
if (ratio > 1) ratio = 1;
var translateX = (width - bounds.Width * ratio) / 2;
var translateY = (height - bounds.Height * ratio) / 2;
var transformGroup = new TransformGroup();
transformGroup.Children.Add(new ScaleTransform(ratio, ratio));
transformGroup.Children.Add(new TranslateTransform(-bounds.X * ratio + translateX, -bounds.Y * ratio + translateY));
geometry.Transform = transformGroup;
drawingContext.DrawGeometry(Brushes.Black, null, geometry);
}
var renderTargetBitmap = new RenderTargetBitmap(
width, height,
0, 0,
PixelFormats.Pbgra32);
renderTargetBitmap.Render(drawingVisual);
return renderTargetBitmap;
}
用法:
//Setting System.Windows.Control.Image source
image.Source = DrawText(@"C:\Windows\Fonts\ariali.ttf", "Lorem ipsum dolor sit amet,\nconsectetur adipisicing elit,\nsed do eiusmod tempor", 50);