I have created my custom captcha image, which is not rendering due to missing font on server.
private const string FontFamily = "Arial"; //this one works
private const string FontFamily = "Rockwell"; //this one is not existing on server
How do i provide the font file to the application to use 开发者_开发技巧my custom font family from file? at the moment i have my function as:
using (Font font = new Font(FontFamily, 1f)){
//rest of the code
}
This depends on your custom captcha drawing method, but you generally have the function drawString(), where you can add your newly registered font via a PrivateFontCollection() to write text with. An example in VB would be:
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
Source: http://www.tutorialsasp.net/uncategorized/loading-and-previewing-fonts-from-files-in-aspnet/
精彩评论