使用自定义 TTF 字体进行 DrawString 图像渲染
我在服务器端使用 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
表示服务器上未安装的字体,但我有它的 TTF 文件。
如何加载 TTF 文件以便在 GDI+ 字符串渲染中使用它?
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:
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.
How can I load the TTF file so that I can use it in GDI+ string rendering?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了使用自定义字体的解决方案。
现在,
myCustomFont
可以按预期与 Graphics.DrawString 方法一起使用。I've found a solution to using custom fonts.
Now
myCustomFont
can be used with the Graphics.DrawString method as intended.只是为了给出更完整的解决方案
Just to give a more complete solution