你好,
你可以通过设置FormattedText.TextAlignment 属性来设置文本的对齐方式。
formattedText.TextAlignment = TextAlignment.Justify;// 设置文本为两端对齐
这是一段测试代码,最终将画出的东西保存为图片,你可以尝试一下:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
DrawingVisual drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
string testString = "对图像进行编码,以便进行保存对图像进行编码,以便进行保存对图像进行编码,以便进行保存对图像进行编码,以便进行保存";
FormattedText formattedText = new FormattedText(
testString,
CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface("Verdana"),
32,
Brushes.Black);
// Set a maximum width and height. If the text overflows these values, an ellipsis "..." appears.
formattedText.MaxTextWidth = 300;
formattedText.MaxTextHeight = 240;
formattedText.TextAlignment = TextAlignment.Justify;
formattedText.SetFontSize(36 * (96.0 / 72.0), 0, 20);
formattedText.SetForegroundBrush( new LinearGradientBrush(
Colors.Orange,
Colors.Teal,
90.0),
0, 20);
drawingContext.DrawText(formattedText, new Point(10, 0));
drawingContext.Close();
//保存为图片
RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)this.Width, (int)this.Height, 96, 96, PixelFormats.Pbgra32);
renderBitmap.Render(drawingVisual);
// 利用JpegBitmapEncoder,对图像进行编码,以便进行保存
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
// 保存文件
FileStream fileStream = new FileStream("test", FileMode.Create, FileAccess.ReadWrite);
encoder.Save(fileStream);
fileStream.Close();
}
Lisa Zhu [MSFT]
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.