今天遇到个难题,公司的服务器居然无法访问到系统的字体,郁闷,画验证码直接就找不到字体
搞了老半天,google了一把也没什么收获,终于发现有System.Drawing.Text.PrivateFontCollection这个字体集合,可以自己做字体的集合,于是把代码修改一下,哈哈,居然就OK了。
希望以后对有遇到同样困难的朋友能有帮助
搞了老半天,google了一把也没什么收获,终于发现有System.Drawing.Text.PrivateFontCollection这个字体集合,可以自己做字体的集合,于是把代码修改一下,哈哈,居然就OK了。
希望以后对有遇到同样困难的朋友能有帮助
public void CreateImage(string Code)
{
System.Drawing.Text.PrivateFontCollection pfc = new System.Drawing.Text.PrivateFontCollection();
pfc.AddFontFile( HttpContext.Current.Server.MapPath( "~/File/Font/arial.ttf")); //添加字体的集合
int iwidth = (int)(Code.Length * 15);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 24);
Graphics g = Graphics.FromImage(image);
Font f = new System.Drawing.Font(pfc.Families[0], 15,FontStyle.Bold, GraphicsUnit.Pixel);//创建font
Brush b = new System.Drawing.SolidBrush(Color.White);
g.Clear(Color.Blue);
//画字
g.DrawString(Code, f, b, 3, 3);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}
{
System.Drawing.Text.PrivateFontCollection pfc = new System.Drawing.Text.PrivateFontCollection();
pfc.AddFontFile( HttpContext.Current.Server.MapPath( "~/File/Font/arial.ttf")); //添加字体的集合
int iwidth = (int)(Code.Length * 15);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 24);
Graphics g = Graphics.FromImage(image);
Font f = new System.Drawing.Font(pfc.Families[0], 15,FontStyle.Bold, GraphicsUnit.Pixel);//创建font
Brush b = new System.Drawing.SolidBrush(Color.White);
g.Clear(Color.Blue);
//画字
g.DrawString(Code, f, b, 3, 3);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
g.Dispose();
image.Dispose();
}