读取字体.ttf文件,生成艺术字图片代码
做了个艺术字在线制作网站,整理一下技术代码
System.Drawing.Text.PrivateFontCollection FM = new PrivateFontCollection(); FM.AddFontFile(Server.MapPath("字体文件路径")); FontFamily FML = FM.Families[0];
这样我们就可以直接读取字体了
我们可以通过
FontStyle fontStyle = FontStyle.Regular;<br />fontStyle |= FontStyle.Italic;<br />fontStyle |= FontStyle.Underline;<br /><br />...
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();<br /><br /><br /><br />MemoryStream msBG = new MemoryStream();
//保存图片
image.Save(msBG, ImageFormat.Png);
最后不要忘了释放资源
FML.Dispose(); font.Dispose(); g.Dispose(); image.Dispose();<br /><br />
return File(msBG.ToArray(), "image/png");
具体的demo演示大家可以去我网站上查看 http://www.shiwusui.com
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20