我创建了自定义验证码图像,由于服务器上缺少字体而无法渲染。
private const string FontFamily = "Arial"; //this one works
private const string FontFamily = "Rockwell"; //this one is not existing on server
如何向应用程序提供字体文件以从文件中使用我的自定义字体系列? 目前我的功能是:
using (Font font = new Font(FontFamily, 1f)){
//rest of the code
}
答案 0 :(得分:0)
这取决于您的自定义验证码绘制方法,但您通常具有函数drawString(),您可以在其中通过PrivateFontCollection()添加新注册的字体以写入文本。 VB中的一个例子是:
Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Text
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Public Class clsUtility
Public Shared Function GetFontPic(ByVal Fontpath As String, ByVal Text As String) As System.Drawing.Bitmap
Dim width As Integer = 620
Dim height As Integer = 30
Dim FontImage As New System.Drawing.Bitmap(width, height, PixelFormat.Format24bppRgb)
Dim g As Graphics = Graphics.FromImage(FontImage)
g.Clear(Color.White)
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias
g.SmoothingMode = SmoothingMode.AntiAlias
Dim pointF As New PointF(10,0)
Dim FontSize As Integer = 24
Dim solidBrush As New SolidBrush(Color.Black)
Dim privateFontCollection As New PrivateFontCollection()
privateFontCollection.AddFontFile(Fontpath) ' load font from file
Dim thisFont As FontFamily = privateFontCollection.Families(0)
Dim regFont As New Font(thisFont, FontSize, FontStyle.Regular, GraphicsUnit.Pixel) ' Create a new font
g.DrawString(Text, regFont, solidBrush, pointF) ' Using the font write the text using the font style
Return FontImage
End Function
来源:http://www.tutorialsasp.net/uncategorized/loading-and-previewing-fonts-from-files-in-aspnet/