做了个艺术字在线制作网站,整理一下技术代码
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