为什么80%的码农都做不了架构师?>>>   

java版:https://my.oschina.net/HJCui/blog/804955

将文本写在图片上,自定义字体,自动换行,自定义行间距。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;namespace StringToImg_costomerFont
{class Program{static void Main(string[] args){string text = @"张家口市地处京晋冀蒙交界处,距北京约180公里,这里地势险要,有“塞外山城”之称。冬季白雪皑皑,一片北国风光,山上积雪数尺,整冬不化,是滑雪旅游的天然胜地。全市地势西北高、东南低,阴山山脉横贯中部,将全市划分为坝上、坝下两个自然地理区域。坝上地区地势......";//string text = @"永和九年,岁在癸丑,暮春之初,会于会稽山阴之兰亭,修禊事也。群贤毕至,少长咸集。此地有崇山峻岭,茂林修竹;又有清流激湍,映带左右,引以为流觞曲水,列坐其次。虽无丝竹管弦之盛,一觞一咏,亦足以畅叙幽情。是日也,天朗气清,惠风和畅,仰观宇宙之大,俯察品类之盛,所以游目骋怀,足以极视听之娱,信可乐也。//夫人之相与,俯仰一世,或取诸怀抱,悟言一室之内;或因寄所托,放浪形骸之外。虽趣舍万殊,静躁不同,当其欣于所遇,暂得于己,快然自足,曾不知老之将至。及其所之既倦,情随事迁,感慨系之矣。向之所欣,俯仰之间,已为陈迹,犹不能不以之兴怀。况修短随化,终期于尽。古人云:“死生亦大矣。”岂不痛哉!//每览昔人兴感之由,若合一契,未尝不临文嗟悼,不能喻之于怀。固知一死生为虚诞,齐彭殇为妄作。后之视今,亦犹今之视昔。悲夫!故列叙时人,录其所述,虽世殊事异,所以兴怀,其致一也。后之览者,亦将有感于斯文。--Edit by hjcui on 2016-12-8";string strSavePath = "C:/test/chn.jpeg";addStringToImg(strSavePath, text);text = "Why I Love Beijing";strSavePath = "C:/test/Eng.jpeg";AddTextToImg(strSavePath, text);}private static void AddTextToImg(string SavePath, string text){int phWidth = 642;int phHeight = 177;#region 绘制图片System.IO.Directory.CreateDirectory(Path.GetDirectoryName(SavePath));System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Jpeg;//Image initImage = (Image)Img.Clone();//生成新图//新建一个bmp图片System.Drawing.Image newImage = new System.Drawing.Bitmap(phWidth, phHeight);//新建一个画板System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(newImage);//设置质量g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//置背景色g.Clear(Color.White);//画图//g.DrawImage(newImage, new Rectangle(0, 0, phWidth, phHeight), 0, 0, phWidth, phHeight, GraphicsUnit.Pixel);g.DrawImage(newImage, 0, 0);//路径       //string path = @"C:\德彪钢笔行书字库.TTF";string path = @"G:\项目需求\13品牌主页\PingFang Bold.ttf";//读取字体文件             PrivateFontCollection pfc = new PrivateFontCollection();pfc.AddFontFile(path);//实例化字体float fontSize = 28;//关于自定义行间距,stackoverflow 中的解释 //Line spacing is a result of the Font you are using.//You may need to break your DrawString commands up into multiple calls if you need custom line spacing.int a = pfc.Families[0].GetLineSpacing(FontStyle.Regular);Font font = new Font(pfc.Families[0], fontSize);//设置字体  //下面定义一个矩形区域,以后在这个矩形里画上白底黑字  float rectX = -1;float rectY = -1;float rectWidth = phWidth;float rectHeight = phHeight;//声明矩形域  RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight);//g.FillRectangle(whiteBrush, rectX, rectY, rectWidth, rectHeight);StringFormat drawFormat = new StringFormat();//drawFormat.FormatFlags = StringFormatFlags.FitBlackBox;drawFormat.Alignment = StringAlignment.Center;//drawFormat.FormatFlags = StringFormatFlags.DisplayFormatControl;drawFormat.LineAlignment = StringAlignment.Center;Brush brush = new SolidBrush(Color.FromArgb(135, 135, 135));g.DrawString(text, font, brush, textArea, drawFormat);int level = 100; //图像质量 1-100的范围ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();ImageCodecInfo ici = null;foreach (ImageCodecInfo codec in codecs){if (codec.MimeType == "image/jpeg")ici = codec;}EncoderParameters ep = new EncoderParameters();ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)level);//保存图newImage.Save(SavePath, ici, ep);#endregion//释放资源g.Dispose();newImage.Dispose();//MemoryStream ms = new MemoryStream();保存为Jpg类型  //bitmap.Save(ms, ImageFormat.Jpeg);//Response.Clear();//Response.ContentType = "image/jpeg";//Response.BinaryWrite(ms.ToArray());}private static void addStringToImg(string SavePath, string text, string strFontPath = null){int phWidth = 642;int phHeight = 377;//System.IO.Directory.CreateDirectory(Path.GetDirectoryName(SavePath));System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Jpeg;//新建一个bmp图片Image newImage = new Bitmap(phWidth, phHeight);//新建一个画板Graphics g = Graphics.FromImage(newImage);//设置质量g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//置背景色g.Clear(Color.White);//画图//g.DrawImage(newImage, new Rectangle(0, 0, phWidth, phHeight), 0, 0, phWidth, phHeight, GraphicsUnit.Pixel);g.DrawImage(newImage, 0, 0);#region 自定义字体//路径       //string path = @"C:\德彪钢笔行书字库.TTF";strFontPath = strFontPath ?? @"C:\华康瘦金体W3.TTF";//读取字体文件             PrivateFontCollection pfc = new PrivateFontCollection();pfc.AddFontFile(strFontPath);//实例化字体float fontSize = 28;Font font = new Font(pfc.Families[0], fontSize);#endregion// 自定义行间距SizeF fit = new SizeF(phWidth, font.Height);StringFormat fmt = StringFormat.GenericTypographic;//fmt.FormatFlags = StringFormatFlags.LineLimit;//int spacing = (int)(1.5 * font.Height);int spacing = (int)(10 + font.Height);//自定义字体颜色Brush brush = new SolidBrush(Color.FromArgb(65, 65, 65));int line = 0;for (int ix = 0; ix < text.Length; ){int chars, lines;g.MeasureString(text.Substring(ix), font, fit, fmt, out chars, out lines);g.DrawString(text.Substring(ix, chars), font, brush, 0, spacing * line);++line;ix += chars;}//保存图newImage.Save(SavePath, ImageFormat.Jpeg);//释放资源g.Dispose();newImage.Dispose();}}}

关于设置行间距有一点要吐槽,vs提示说StringFormat可以设置行间距,但是翻遍了MSDN愣是没找到相关设置。被微软忽悠了一把。

微软支持自动换行,但是不能设置行间距,效果图

自定义行距效果图

转载于:https://my.oschina.net/HJCui/blog/804981

将文本写在图片上,自定义字体,自动换行,自定义行间距相关推荐

  1. Java 创建背景透明的文字图片、将文本写到图片上

    Java 创建背景透明的文字图片.将文本写到图片上: import sun.font.FontDesignMetrics;import javax.imageio.ImageIO; import ja ...

  2. springboot 整合文本编辑器(图片上传)

    Ueditor 文件上传配置: ueditor.config.js serverUrl:填写自己的路径 jsp->config.json: imageActionName:填写自己写的文件上传的 ...

  3. python怎么在图片上写字_python如何实现内容写在图片上

    本文实例为大家分享了python将内容写在图片上的具体代码,供大家参考,具体内容如下 # -*- coding: utf-8 -*- # Created on 2018/3/20 import bas ...

  4. 在线分析图片上的字体

    在线分析图片上的字体 新闻来源:呐喊网络部落格 当我们看到一张图片,上面有很漂亮的字体,可是这是什么字体呢?去浩瀚的英文字体库一一对照不是完全没有准确找到的可能,只是,谁有那样的耐力?不是标榜自己,本 ...

  5. Ueditor富文本编辑器修改图片上传路径

    Ueditor富文本编辑器修改图片上传路径 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/QZPHP_L 由于网站后台使用Zend framewor ...

  6. vue3中使用vue-quill富文本编辑器 重写图片上传功能

    最近开发的一个vue3+element-plus的项目,需要用到富文本编辑器,最终选择了VueQuill这一款轻便型的富文本编辑器. 说说遇到的问题:富文本编辑器的图片上传,默认是直接以base64图 ...

  7. springboot生成图片并在图片上添加字体进行换行

    话不多说直接上代码,本次返回的生成图片的base64字符 /*** @param message 图片上要添加的文字* @param width 画布的宽度* @param height 画布的高度* ...

  8. vue-quill-editor富文本编辑器及图片上传到服务器

    最近在做后台管理平台,该项目是使用vue+element-ui框架,有一个界面就是关于消息推送的,在网上找了一圈,比较适合的就是vue-quill-editor富文本编辑器 1.需求完成图: 此项目就 ...

  9. 关于移动手机端富文本编辑器qeditor图片上传改造

    日前项目需要在移动端增加富文本编辑,上网找了下,大多数都是针对pc版的,不太兼容手机,当然由于手机屏幕小等原因也限制富文本编辑器的众多强大功能,所以要找的编辑器功能必须是精简的. 找了好久,发现qed ...

  10. ASP.NET MVC应用程序把文字写在图片上

    Insus.NET实现这篇<MVC把随机产生的字符串转换为图片>http://www.cnblogs.com/insus/p/3624235.html 之后,把字符串转换为图片,不如尝试, ...

最新文章

  1. 【TCP/IP】IP路由选择
  2. 《移动数据挖掘》—— 第1章 引言 1.1 移动数据及其价值
  3. 真相!没项目经验高薪就无望?
  4. 剑指Offer #06 旋转数组的最小数字(二分查找)| 图文详解
  5. 用VMWARE学习组网(四)
  6. JavaScript可否多线程? 深入理解JavaScript定时机制
  7. Android学习笔记:TabHost 和 FragmentTabHost
  8. JavaScript学习笔记:类型检测
  9. 50年间,高水平论文数量国家排名是怎样变化的?| 可视化数据
  10. Eclipse启动Web项目 Tomcat中webapps中没有项目文件夹
  11. JAVA中list,set,数组之间的转换详解
  12. 人工智能或可称之为人造智能
  13. 怎么用计算机画画图片,电脑的画图工具怎么用
  14. 初步了解java虚拟机
  15. STM32 OSAL操作系统抽象层的移植
  16. 微信小程序微信授权登录
  17. telnet指令输入账户密码后出现“句柄无效”无法连接的问题
  18. Windows10专业版重装系统教程
  19. Vuex-状态管理(24)
  20. python疲劳检测代码_【君奉天|开发日记】疲劳驾驶检测

热门文章

  1. Pycharm中进行Python远程开发
  2. numpy教程:数学函数和基本统计函数
  3. 编程之美读书笔记1.3 —— 一摞烙饼的排序
  4. Python输入输出详解
  5. C#中public函数的用法_python中的lambda函数用法
  6. java后台生成分页_Java实现分页的前台页面和后台代码
  7. 新型消防机器人作文_说一说消防机器人
  8. idea改类名快捷键_IDEA使用之快捷键(default设置)
  9. python和matlab矩阵运算效率_如何写出比 MATLAB 更快的矩阵运算程序?
  10. ztree 后台异步加载_zTree树形插件异步加载方法详解