Graphics.DrawString所输出的字符串 有办法自动换行吗
2017-05-06 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
相关代码如下:
/// <summary>
/// 绘制文本自动换行(超出截断)
/// </summary>
/// <param name=\"graphic\">绘图图面</param>
/// <param name=\"font\">字体</param>
/// <param name=\"text\">文本</param>
/// <param name=\"recangle\">绘制范围</param>
private void DrawStringWrap(Graphics graphic, Font font, string text, Rectangle recangle)
{
List<string> textRows = GetStringRows(graphic, font, text, recangle.Width);
int rowHeight = (int)(Math.Ceiling(graphic.MeasureString(\"测试\", font).Height));
int maxRowCount = recangle.Height / rowHeight;
int drawRowCount = (maxRowCount < textRows.Count) ? maxRowCount : textRows.Count;
int top = (recangle.Height - rowHeight * drawRowCount) / 2;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;
for (int i = 0; i < drawRowCount; i++)
{
Rectangle fontRectanle = new Rectangle(recangle.Left, top + rowHeight * i, recangle.Width, rowHeight);
graphic.DrawString(textRows, font, new SolidBrush(Color.Black), fontRectanle, sf);
}
}
/// <summary>
/// 绘制文本自动换行(超出截断)
/// </summary>
/// <param name=\"graphic\">绘图图面</param>
/// <param name=\"font\">字体</param>
/// <param name=\"text\">文本</param>
/// <param name=\"recangle\">绘制范围</param>
private void DrawStringWrap(Graphics graphic, Font font, string text, Rectangle recangle)
{
List<string> textRows = GetStringRows(graphic, font, text, recangle.Width);
int rowHeight = (int)(Math.Ceiling(graphic.MeasureString(\"测试\", font).Height));
int maxRowCount = recangle.Height / rowHeight;
int drawRowCount = (maxRowCount < textRows.Count) ? maxRowCount : textRows.Count;
int top = (recangle.Height - rowHeight * drawRowCount) / 2;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;
for (int i = 0; i < drawRowCount; i++)
{
Rectangle fontRectanle = new Rectangle(recangle.Left, top + rowHeight * i, recangle.Width, rowHeight);
graphic.DrawString(textRows, font, new SolidBrush(Color.Black), fontRectanle, sf);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询