读取字体.ttf文件,生成艺术字图片代码

做了个艺术字在线制作网站,整理一下技术代码

System.Drawing.Text.PrivateFontCollection FM = new PrivateFontCollection();
FM.AddFontFile(Server.MapPath("字体文件路径"));
FontFamily FML = FM.Families[0];

这样我们就可以直接读取字体了

我们可以通过

FontStyle fontStyle = FontStyle.Regular;
fontStyle |= FontStyle.Italic;
fontStyle |= FontStyle.Underline;

...

  fontStyle -= FontStyle.Regular;
  fontStyle |= FontStyle.Bold;

 

  Font font = new Font(FML, 字体大小, fontStyle, GraphicsUnit.Point);

这个我们可以设置字体加粗,斜体,下划线的功能

 

Color color = ColorTranslator.FromHtml("#ff0000");  //设置字体颜色

  

Bitmap image = new Bitmap(width, height);
            
Graphics g = Graphics.FromImage(image);


//这里设置图片质量
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.AssumeLinear;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;


RectangleF rect = new RectangleF(1, 1, width, height);

SolidBrush brush = new SolidBrush(color);//绘制图片

g.DrawString("这里要生成的文字", font, brush, rect);
brush.Dispose();



MemoryStream msBG = new MemoryStream();

  //保存图片

image.Save(msBG, ImageFormat.Png);

 

最后不要忘了释放资源

FML.Dispose();
font.Dispose();
g.Dispose();
image.Dispose();

return File(msBG.ToArray(), "image/png");

  

具体的demo演示大家可以去我网站上查看  http://www.shiwusui.com

 

转载于:https://www.cnblogs.com/chuanxincao/p/8340772.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值