asp.net将文字绘制成图片
来源:互联网 发布:阿里云搭建web服务器 编辑:程序博客网 时间:2024/06/04 17:49
可以将字符串输出为位图,并在前台页面显示。同时支持自动换行。
调用代码:其中,str为要输出的文本。
Bitmap newBitmap = null; Graphics g = null; string str = allTextTrans; try { Font strFont = new Font("宋体", 12); newBitmap = new Bitmap(800, 1000, PixelFormat.Format32bppArgb); g = Graphics.FromImage(newBitmap); Rectangle recangle = new Rectangle(0, 0, 800, 1000); DrawStringWrap(g, strFont, allTextTrans, recangle); MemoryStream tempStream = new MemoryStream(); newBitmap.Save(tempStream, ImageFormat.Png); Response.ClearContent(); Response.ContentType = "image/png"; Response.BinaryWrite(tempStream.ToArray()); Response.End(); } finally { if (g != null) g.Dispose(); if (newBitmap != null) newBitmap.Dispose(); }绘制和分行函数:
/// <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; int top = 0; 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[i], font, new SolidBrush(Color.Black), fontRectanle, sf); } } /// <summary> /// 将文本分行 /// </summary> /// <param name=\"graphic\">绘图图面</param> /// <param name=\"font\">字体</param> /// <param name=\"text\">文本</param> /// <param name=\"width\">行宽</param> /// <returns></returns> private List<string> GetStringRows(Graphics graphic, Font font, string text, int width) { int RowBeginIndex = 0; int rowEndIndex = 0; int textLength = text.Length; List<string> textRows = new List<string>(); for (int index = 0; index < textLength; index++) { rowEndIndex = index; if (index == textLength - 1) { textRows.Add(text.Substring(RowBeginIndex)); } else if (rowEndIndex + 1 < text.Length && text.Substring(rowEndIndex,1) == "\n") { textRows.Add(text.Substring(RowBeginIndex, rowEndIndex - RowBeginIndex)); rowEndIndex = index += 1; RowBeginIndex = rowEndIndex; } else if (graphic.MeasureString(text.Substring(RowBeginIndex, rowEndIndex - RowBeginIndex + 1), font).Width > width) { textRows.Add(text.Substring(RowBeginIndex, rowEndIndex - RowBeginIndex)); RowBeginIndex = rowEndIndex; } } return textRows; } }
展示效果:
参考资料:http://blog.163.com/gsrwsh@126/blog/static/75794506201061492755492/
阅读全文
0 0
- asp.net将文字绘制成图片
- asp.net 2.0 将文字转换成图片
- asp.net 2.0 将文字转成图片
- asp.net 2.0 将文字转成图片(文字生成图片)
- ASP.net生成文字图片
- ASP.net生成文字图片
- ASP.net生成文字图片
- ASP.net生成文字图片
- ASP.net生成文字图片
- ASP.net生成文字图片
- ASP.net生成文字图片
- Asp.net将图片转换成二进制
- IOS 将文字写绘制成图片并转换为像素数据
- ASP.NET:图片添加文字信息
- Asp.net 中将文字写入图片中
- Asp.net 中将文字写入图片中
- ASP.NET:图片添加文字信息
- Asp.net 中将文字写入图片中
- SSH TCP 多路复用
- 第一次写php 推荐文章
- 【C#】求点到线段的垂线距离
- python从放弃到入门1---装一套环境
- 基于ssm框架的个人博客(2)--基于maven的ssm框架的整合
- asp.net将文字绘制成图片
- 全概率公式
- convolution nn+图像卷积
- POJ
- vegas抠图小技巧(一)
- 使用AlertDialog报错 You need to use a Theme.AppCompat theme (or descendant) with this activity.之解决
- JVM调优总结 -Xms -Xmx -Xmn -Xss
- 数据库索引
- Python基础小笔记《2017-09-27》