asp.net将文字绘制成图片
发布日期:2021-10-12 02:13:36
浏览次数:1
分类:技术文章
本文共 3567 字,大约阅读时间需要 11 分钟。
可以将字符串输出为位图,并在前台页面显示。同时支持自动换行。
调用代码:其中,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(); }绘制和分行函数:
///展示效果:/// 绘制文本自动换行(超出截断) /// /// 绘图图面 /// 字体 /// 文本 /// 绘制范围 private void DrawStringWrap(Graphics graphic, Font font, string text, Rectangle recangle) { ListtextRows = 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); } } /// /// 将文本分行 /// /// 绘图图面 /// 字体 /// 文本 /// 行宽 ///private List GetStringRows(Graphics graphic, Font font, string text, int width) { int RowBeginIndex = 0; int rowEndIndex = 0; int textLength = text.Length; List textRows = new List (); 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/
转载地址:https://blog.csdn.net/qq_36747842/article/details/78133023 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年04月18日 07时33分24秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
我在MTK平台下调试音频ALSA
2021-06-30
被该死的Openssl编译嘲讽了一个下午
2021-06-30
暴力修改SElinux权限
2021-06-30
camera-open-log
2019-04-27
让这个该死的服务跑起来了~
2019-04-27
LinuxGPIO操作和MTK平台GPIO
2019-04-27
Linux cpu亲和力
2019-04-27
C语言指定初始化器解析及其应用
2019-04-27
安卓9.0添加服务修改SELinux
2019-04-27
深入理解Linux内核链表
2019-04-27
Linux一定需要文件系统吗?
2019-04-27
手机是怎么确定位置信息的?
2019-04-27
使用Adobe Audition生成基本音频
2019-04-27
思必驰算法库录音日志~
2019-04-27
苹果uwb定位技术
2019-04-27
为什么需要超出48K的音频采样率,以及PCM到DSD的演进
2019-04-27
广东,就是这么横?
2019-04-27
手机可以连接多少个蓝牙设备?
2019-04-27
10大黑客专用的 Linux 操作系统,你了解哪些?
2019-04-27
你为什么喜欢VIM?
2019-04-27