C#给图片增加文字实例代码

发布时间:2022-05-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了C#给图片增加文字实例代码脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
业务需要动态给图片增加文字(书本的封面图片),修改字体大小、字体、颜色、控制位置

测试代码:

 1             string path = @"E:\cover.png"; 2  3             BITmap bmp = new Bitmap(path); 4             Graphics g = Graphics.FromImage(bmp); 5             String str = "贤愚经"; 6             Font font = new Font("仿宋_GB2312", 14, FontStyle.Bold);//设置字体,大小,粗细 7             Solidbrush sbrush = new SolidBrush(Color.White);//设置颜色 8             int base_left = 10; 9             int left_space = 30;10 11             int base_top = 27;12             int top_space = 27;13             for (int i = 0; i < str.Length; i++)14             {15                 if (i > 13)16                 {17                     continue;18                     g.DrawString(str[i] + "", font, sbrush, new PointF(base_left + (left_space * 2), base_top + (top_space * (i - 14))));19                 }20                 else if (i > 6)21                 {22                     g.DrawString(str[i] + "", font, sbrush, new PointF(base_left + (left_space * 1), base_top + (top_space * (i - 7))));23                 }24                 else25                 {26                     g.DrawString(str[i] + "", font, sbrush, new PointF(base_left, base_top + (top_space * i)));27                 }28 29             }30 31             //MemoryStream ms = new MemoryStream();32             //bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);33             bmp.Save(@"E:\cover1.png");

字体选择:

黑体:SimHei
宋体:SimSun
新宋体:NSimSun
仿宋:fangSong
楷体:KaiTi
仿宋_GB2312:FangSong_GB2312
楷体_GB2312:KaiTi_GB2312
微软雅黑体:Microsoft YaHei

完结。

以上就是C#给图片增加文字实例代码的详细内容,更多请关注脚本宝典其它相关文章

脚本宝典总结

以上是脚本宝典为你收集整理的C#给图片增加文字实例代码全部内容,希望文章能够帮你解决C#给图片增加文字实例代码所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。