Read the font. ttf file, generate the IMG code, and font. ttf
I made an online production website for wordart and sorted out the technical code.
System. Drawing. Text. PrivateFontCollection FM = new PrivateFontCollection (); FM. AddFontFile (Server. MapPath ("font file path"); FontFamily FML = FM. Families [0];
In this way, we can directly read the font.
We can use
FontStyle fontStyle = FontStyle.Regular;
fontStyle |= FontStyle.Italic;
fontStyle |= FontStyle.Underline;
...
FontStyle-= FontStyle. Regular;
FontStyle | = FontStyle. Bold;
Font font = new Font (FML, fontStyle, GraphicsUnit. Point );
You can set the font to bold, italic, and underline.
Color color = ColorTranslator. FromHtml ("# ff0000"); // you can specify the font Color.
Bitmap image = new Bitmap (width, height); Graphics g = Graphics. fromImage (image); // set the image quality 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 = new SolidBrush (color); // draw an image g. drawString ("the text to be generated here", font, brush, rect); brush. dispose ();
MemoryStream msBG = new MemoryStream ();
// Save the image
image.Save(msBG, ImageFormat.Png);
Finally, do not forget to release resources.
FML.Dispose();font.Dispose();g.Dispose();image.Dispose();
return File(msBG.ToArray(), "image/png");
Specific demo you can go to my website to view http://www.shiwusui.com