最近有个项目加入了验证码功能,就从自己博客以前的代码中找到直接使用,直接访问验证码页面报错如下:
Application Exception
System.ArgumentException
The requested FontFamily could not be found [GDI+ status: FontFamilyNotFound]Description: HTTP 500.Error processing request.
Details: Non-web exception. Exception origin (name of application or object): System.Drawing.
Exception stack trace:
at System.Drawing.GDIPlus.CheckStatus (Status status) [0x00000] in <filename unknown>:0
at System.Drawing.FontFamily..ctor (GenericFontFamilies genericFamily) [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.Drawing.FontFamily:.ctor (System.Drawing.Text.GenericFontFamilies)
at System.Drawing.FontFamily.get_GenericMonospace () [0x00000] in <filename unknown>:0
at EnterpriseShow.UI.Manager.AjaxHandle.ValidCode.ProcessRequest (System.Web.HttpContext context) [0x00000] in <filename unknown>:0
at System.Web.HttpApplication+<Pipeline>c__Iterator1.MoveNext () [0x00000] in <filename unknown>:0
at System.Web.HttpApplication.Tick () [0x00000] in <filename unknown>:0
1、首先从window上copy字体安装到centos中
安装字体方法如下:本段文字来自唧唧的博客
?
|
2、安装完毕后,我们在代码中指定要使用的字体
context.Response.ContentType = "image/gif";
Bitmap basemap = new Bitmap(200, 60);
Graphics garph1 = Graphics.FromImage(basemap);garph1.FillRectangle(new SolidBrush(Color.White), 0, 0, 200, 60);
Font font = new Font("consola.ttf", 48, FontStyle.Bold, GraphicsUnit.Pixel);Random r = new Random();
string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
string letter;
StringBuilder s = new StringBuilder();
for (int i = 0; i < 5; i++)
{
letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
s.Append(letter);
garph1.DrawString(letter, font, new SolidBrush(Color.Black), i * 38, r.Next(0, 15));}
Pen linePen = new Pen(new SolidBrush(Color.Black), 2);
for (int x = 0; x < 6; x++)
{
garph1.DrawLine(linePen, new Point(r.Next(0, 199), r.Next(0, 59)), new Point(r.Next(0, 199), r.Next(0, 59)));
}basemap.Save(context.Response.OutputStream, ImageFormat.Gif);
context.Session["CheckCode"] = s.ToString(); //存入Session,用于对比验证
context.Response.End();
再次访问清晰可见啊
记录下来备忘。
asp.net中ashx生成验证码代码放在Linux(centos)主机上访问时无法显示问题,布布扣,bubuko.com