Using custom TTF font for DrawString image rendering

前端 未结 2 1572
一个人的身影
一个人的身影 2021-02-01 19:36

I am using GDI+ on the server-side to create an image which is streamed to the user\'s browser. None of the standard fonts fit my requirements and so I want to load a TrueType f

2条回答
  •  情书的邮戳
    2021-02-01 20:11

    Just to give a more complete solution

    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Drawing;
    using System.Drawing.Text;
    
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string fontName = "YourFont.ttf";
            PrivateFontCollection pfcoll = new PrivateFontCollection();
            //put a font file under a Fonts directory within your application root
            pfcoll.AddFontFile(Server.MapPath("~/Fonts/" + fontName));
            FontFamily ff = pfcoll.Families[0];
            string firstText = "Hello";
            string secondText = "Friend!";
    
            PointF firstLocation = new PointF(10f, 10f);
            PointF secondLocation = new PointF(10f, 50f);
            //put an image file under a Images directory within your application root
            string imageFilePath = Server.MapPath("~/Images/YourImage.jpg");
            Bitmap bitmap = (Bitmap)System.Drawing.Image.FromFile(imageFilePath);//load the image file
    
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                using (Font f = new Font(ff, 14, FontStyle.Bold))
                {
                    graphics.DrawString(firstText, f, Brushes.Blue, firstLocation);
                    graphics.DrawString(secondText, f, Brushes.Red, secondLocation);
                }
            }
            //save the new image file within Images directory
            bitmap.Save(Server.MapPath("~/Images/" + System.Guid.NewGuid() + ".jpg"));
            Response.Write("A new image has been created!");
        } 
    }
    

提交回复
热议问题