由于服务器上缺少字体族文件,生成验证码图像失败
本文关键字:验证 失败 图像 文件 服务器 字体 | 更新日期: 2023-09-27 18:06:54
我已经创建了我的自定义captcha图像,由于服务器上缺少字体而无法渲染。
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
}
这取决于你的自定义captcha绘制方法,但你通常有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/