asp.net中ashx生成验证码代码放在Linux(centos)主机*问时无法显示问题

最近有个项目加入了验证码功能,就从自己博客以前的代码中找到直接使用,直接访问验证码页面报错如下:

源代码:asp.net中使用一般处理程序生成验证码

Application Exception
System.ArgumentException
The requested FontFamily could not be found [GDI+ status: FontFamilyNotFound] Description: HTTP .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>:
at System.Drawing.FontFamily..ctor (GenericFontFamilies genericFamily) [0x00000] in <filename unknown>:
at (wrapper remoting-invoke-with-check) System.Drawing.FontFamily:.ctor (System.Drawing.Text.GenericFontFamilies)
at System.Drawing.FontFamily.get_GenericMonospace () [0x00000] in <filename unknown>:
at EnterpriseShow.UI.Manager.AjaxHandle.ValidCode.ProcessRequest (System.Web.HttpContext context) [0x00000] in <filename unknown>:
at System.Web.HttpApplication+<Pipeline>c__Iterator1.MoveNext () [0x00000] in <filename unknown>:
at System.Web.HttpApplication.Tick () [0x00000] in <filename unknown>:

1、首先从window上copy字体安装到centos中

安装字体方法如下:本段文字来自唧唧的博客

在CentOS中安装中文字体
作者:唧唧
1、先从你本机 C:\Windows\Fonts 拷贝或者网络上下载你想要安装的字体文件(*.ttf文件)到 /usr/share/fonts/chinese/TrueType 目录下(如果系统中没有此目录,则自行mkdir创建,亦可重命名为自己喜欢的文件夹名)
2、修改字体文件的权限,使root用户以外的用户也可以使用
# cd /usr/share/fonts/chinese/TrueType
# chmod 755 *.ttf
3、建立字体缓存
# mkfontscale (如果提示 mkfontscale: command not found,需自行安装 # yum install mkfontscale )
# mkfontdir
# fc-cache -fv (如果提示 fc-cache: command not found,则需要安装# yum install fontconfig )
4、重启计算机
# reboot

2、安装完毕后,我们在代码中指定要使用的字体

context.Response.ContentType = "image/gif";
Bitmap basemap = new Bitmap(, );
Graphics garph1 = Graphics.FromImage(basemap); garph1.FillRectangle(new SolidBrush(Color.White), , , , );
Font font = new Font("consola.ttf", , FontStyle.Bold, GraphicsUnit.Pixel); Random r = new Random();
string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
string letter;
StringBuilder s = new StringBuilder();
for (int i = ; i < ; i++)
{
letter = letters.Substring(r.Next(, letters.Length - ), );
s.Append(letter);
garph1.DrawString(letter, font, new SolidBrush(Color.Black), i * , r.Next(, )); } Pen linePen = new Pen(new SolidBrush(Color.Black), );
for (int x = ; x < ; x++)
{
garph1.DrawLine(linePen, new Point(r.Next(, ), r.Next(, )), new Point(r.Next(, ), r.Next(, )));
} basemap.Save(context.Response.OutputStream, ImageFormat.Gif);
context.Session["CheckCode"] = s.ToString();    //存入Session,用于对比验证
context.Response.End();

再次访问清晰可见啊

asp.net中ashx生成验证码代码放在Linux(centos)主机*问时无法显示问题

记录下来备忘。

上一篇:短视频图像处理 OpenGL ES 实践


下一篇:javaweb学习总结十二(JAXP对XML文档进行SAX解析)