C# 使用自定义 TTF 字体进行 DrawString 图像渲染
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/523246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using custom TTF font for DrawString image rendering
提问by Llyle
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 font and use this font for drawing my strings to the graphics object:
我在服务器端使用 GDI+ 来创建一个图像,该图像被流式传输到用户的浏览器。没有一个标准字体符合我的要求,因此我想加载 TrueType 字体并使用此字体将我的字符串绘制到图形对象:
using (var backgroundImage = new Bitmap(backgroundPath))
using (var avatarImage = new Bitmap(avatarPath))
using (var myFont = new Font("myCustom", 8f))
{
Graphics canvas = Graphics.FromImage(backgroundImage);
canvas.DrawImage(avatarImage, new Point(0, 0));
canvas.DrawString(username, myFont,
new SolidBrush(Color.Black), new PointF(5, 5));
return new Bitmap(backgroundImage);
}
myCustom
represents a font that is not installed on the server, but for which I have the TTF file for.
myCustom
代表一种未安装在服务器上的字体,但我有它的 TTF 文件。
How can I load the TTF file so that I can use it in GDI+ string rendering?
如何加载 TTF 文件以便我可以在 GDI+ 字符串渲染中使用它?
采纳答案by Llyle
I've found a solution to using custom fonts.
我找到了使用自定义字体的解决方案。
// 'PrivateFontCollection' is in the 'System.Drawing.Text' namespace
var foo = new PrivateFontCollection();
// Provide the path to the font on the filesystem
foo.AddFontFile("...");
var myCustomFont = new Font((FontFamily)foo.Families[0], 36f);
Now myCustomFont
can be used with the Graphics.DrawString method as intended.
现在myCustomFont
可以按预期与 Graphics.DrawString 方法一起使用。
回答by user890255
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!");
}
}