随机性报错,报错信息如下:
- Server Error in '/' Application.
- Object reference not set to an instance of an object.
- Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
- Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
- Source Error:
- An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
- Stack Trace:
- [NullReferenceException: Object reference not set to an instance of an object.]
- FineUI.CommonResourceHelper.IsHeaderContains(Page page, String controlId) +40
- FineUI.CommonResourceHelper.AddContentToHead(Page page, String controlId, String msg) +19
- FineUI.CommonResourceHelper.RegisterCommonResource(Page page) +87
- FineUI.ResourceManager.SetupFirstLoadResource() +14
- FineUI.ResourceManager.Page_PreRenderComplete(Object sender, EventArgs e) +190
- System.Web.UI.Page.OnPreRenderComplete(EventArgs e) +8695102
- System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1029
- Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053
复制代码 代码前端为:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="validate.aspx.cs" Inherits="validate" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
-
- </div>
- </form>
- </body>
- </html>
复制代码 后端为:
- using System;
- using System.Drawing;
- public partial class validate : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- Random rad = new Random(Guid.NewGuid().GetHashCode());
- string aa = "";
- for (int i = 0; i < 4; i++)
- {
- aa = aa + rad.Next(10).ToString();
- }
- Session["yanzheng"] = aa;
- CreateImages(Convert.ToString(aa));
- }
- /// <summary>
- /// /// 生成验证图片
- /// /// </summary>
- /// /// <param name="checkCode">验证字符</param>
- protected void CreateImages(string checkCode)
- {
- int iwidth = (int)(checkCode.Length * 15);
- System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 30);
- Graphics g = Graphics.FromImage(image);
- g.Clear(Color.LightCyan);
- //定义颜色
- Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple, Color.SkyBlue };
- //定义字体
- string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体", "Comic Sans MS" };
- Random rand = new Random(Guid.NewGuid().GetHashCode());
- //随机输出噪点
- for (int i = 0; i < 150; i++)
- {
- int x = rand.Next(image.Width);
- int y = rand.Next(image.Height);
- // g.DrawPie(new Pen(GetRandomColor(), 0), x, y, 6, 6, 1, 1);
- }
- for (int i = 0; i < 12; i++)
- {
- int x1 = rand.Next(image.Width);
- int x2 = rand.Next(image.Width);
- int y1 = rand.Next(image.Height);
- int y2 = rand.Next(image.Height);
- g.DrawLine(new Pen(GetRandomColor()), x1, y1, x2, y2);
- }
- //输出不同字体和颜色的验证码字符
- for (int i = 0; i < checkCode.Length; i++)
- {
- int cindex = rand.Next(7);
- int findex = rand.Next(6);
- Font _font = new System.Drawing.Font(font[findex], 14, System.Drawing.FontStyle.Bold);
- Brush b = new System.Drawing.SolidBrush(c[cindex]);
- int ii = 4;
- if ((i + 1) % 2 == 0)
- {
- ii = 2;
- }
- g.DrawString(checkCode.Substring(i, 1), _font, b, 3 + (i * 12), ii);
- }
- Session["yanzheng"] = checkCode; //保存验证码
- //画一个边框
- g.DrawRectangle(new Pen(Color.Red, 0), 100, 0, image.Width - 1, image.Height - 1);
- //输出到浏览器
- System.IO.MemoryStream ms = new System.IO.MemoryStream();
- image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
- Response.ClearContent();//Response.ClearContent();
- Response.ContentType = "image/Jpeg";
- Response.BinaryWrite(ms.ToArray());
- g.Dispose();
- image.Dispose();
- }
- //////////////////////////////////////////////////////////
- /// /// 生成随机颜色
- /// //////////////////////////////////////////////////////////
- public static System.Drawing.Color GetRandomColor()
- {
- Random randomNum_1 = new Random(Guid.NewGuid().GetHashCode());
- System.Threading.Thread.Sleep(randomNum_1.Next(1));
- int int_Red = randomNum_1.Next(255);
- Random randomNum_2 = new Random((int)DateTime.Now.Ticks);
- int int_Green = randomNum_2.Next(255);
- Random randomNum_3 = new Random(Guid.NewGuid().GetHashCode());
- int int_Blue = randomNum_3.Next(255);
- int_Blue = (int_Red + int_Green > 380) ? int_Red + int_Green - 380 : int_Blue;
- int_Blue = (int_Blue > 255) ? 255 : int_Blue;
- return GetDarkerColor(System.Drawing.Color.FromArgb(int_Red, int_Green, int_Blue));
- }
- //获取加深颜色
- public static Color GetDarkerColor(Color color)
- {
- const int max = 255;
- int increase = new Random(Guid.NewGuid().GetHashCode()).Next(255); //还可以根据需要调整此处的值
- int r = Math.Abs(Math.Min(color.R - increase, max));
- int g = Math.Abs(Math.Min(color.G - increase, max));
- int b = Math.Abs(Math.Min(color.B - increase, max));
- return Color.FromArgb(r, g, b);
- }
- }
复制代码
web。config为- <?xml version="1.0"?>
- <!--
- 注意: 除了手动编辑此文件以外,您还可以使用
- Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
- “网站”->“Asp.Net 配置”选项。
- 设置和注释的完整列表在
- machine.config.comments 中,该文件通常位于
- \Windows\Microsoft.Net\Framework\v2.x\Config 中
- -->
- <configuration>
- <configSections>
- <section name="FineUI" type="FineUI.ConfigSection, FineUI"/>
- </configSections>
- <!-- 可用的配置项(这里列的都是默认值):Language="zh_CN" AjaxTimeout="60" EnableAjax="true" Theme="blue" FormMessageTarget="qtip" FormOffsetRight="20" FormLabelWidth="100" FormLabelSeparator=":" IconBasePath="~/icon" EnableAjaxLoading="true" AjaxLoadingType="default" CustomTheme="" CustomThemeBasePath="~/theme" -->
- <FineUI EnableBigFont="true" DebugMode="false" AjaxTimeout="6000"/>
- <appSettings>
- </appSettings>
- <system.web>
- <sessionState mode="InProc" timeout="1000"/>
- <!-- Net3.5以上的项目,一定要为pages节点加上这两个属性:controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" -->
- <pages>
- <controls>
- <add assembly="FineUI" namespace="FineUI" tagPrefix="ext"/>
- </controls>
- </pages>
- <httpModules>
- <add name="FineUIScriptModule" type="FineUI.ScriptModule, FineUI"/>
- </httpModules>
- <httpHandlers>
- <add verb="GET" path="res.axd" type="FineUI.ResourceHandler, FineUI"/>
- </httpHandlers>
- <customErrors mode="Off"/>
- <compilation debug="true">
- <assemblies>
- <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
- </assemblies></compilation>
- </system.web>
- <system.webServer>
- <modules>
- <add name="FineUIScriptModule" type="FineUI.ScriptModule, FineUI"/>
- </modules>
- <httpHandlers>
- <add verb="GET" path="res.axd" type="FineUI.ResourceHandler, FineUI"/>
- <add verb="Get" path="WebResource.axd" type="System.Web.Handlers.AssemblyResourceLoader"/>
- </httpHandlers>
- <httpErrors errorMode="Detailed"/>
- <asp scriptErrorSentToBrowser="true"/>
- </system.webServer>
- <system.codedom>
- </system.codedom>
- </configuration>
复制代码
有时候工作很正常,有时候爆上述错误
|