生成随机数图片

来源:互联网 发布:mac 火鹤色 编辑:程序博客网 时间:2024/06/05 21:02
  生成验证码图片#region (2) 生成随机数图片  /// <summary>  /// 生成随机数图片  /// </summary>  /// <param name="codeCount">生成随机数的位数</param>  /// <returns>返回随机数</returns>  public static string CreateRandomCode(int codeCount)  {   string allChar = "0,1,2,3,4,5,6,7,8,9" ;   string[] allCharArray = allChar.Split(',');   string randomCode = "";   int temp = -1;    Random rand = new Random();   for(int i = 0; i < codeCount; i++)   {    if(temp != -1)    {     rand = new Random(i*temp*((int)DateTime.Now.Ticks));    }    int t = rand.Next(10);    if(temp == t)    {     return CreateRandomCode(codeCount);    }    temp = t;    randomCode += allCharArray[t];   }   return randomCode;  }  private const double PI = 3.1415926535897932384626433832795;  private const double PI2 = 6.283185307179586476925286766559;   /// <summary>  /// 正弦曲线Wave扭曲图片  /// </summary>  /// <param name="srcBmp"></param>  /// <param name="bXDir"></param>  /// <param name="nMultValue">波形的幅度倍数</param>  /// <param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param>  /// <returns></returns>  public System.Drawing.Bitmap TwistImage(Bitmap srcBmp,bool bXDir,double dMultValue,double dPhase)  {   System.Drawing.Bitmap destBmp = new Bitmap(srcBmp.Width,srcBmp.Height);    // 将位图背景填充为白色   System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(destBmp);   graph.FillRectangle(new SolidBrush(System.Drawing.Color.White),0,0,destBmp.Width,destBmp.Height);   graph.Dispose();     double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;    for(int i=0;i<destBmp.Width;i++)   {    for(int j=0;j<destBmp.Height;j++)    {     double dx = 0;     dx = bXDir ? (PI2*(double)j)/dBaseAxisLen : (PI2*(double)i)/dBaseAxisLen;     dx += dPhase;     double dy = Math.Sin(dx);      // 取得当前点的颜色     int nOldX = 0,nOldY = 0;     nOldX = bXDir ? i + (int)(dy*dMultValue) : i;     nOldY = bXDir ? j : j + (int)(dy*dMultValue);      System.Drawing.Color color = srcBmp.GetPixel(i,j);     if(nOldX >= 0 && nOldX < destBmp.Width      && nOldY >=0 && nOldY < destBmp.Height)     {      destBmp.SetPixel(nOldX,nOldY,color);     }    }   }    return destBmp;  }  /// <summary>  /// 创建随机数图片  /// </summary>  /// <param name="checkCode">随机数</param>  public static void CreateImage(string checkCode)  {   int iwidth = (int)(checkCode.Length * 12);   System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 25);   Graphics g = Graphics.FromImage(image);   Font f = new System.Drawing.Font("Arial", 12, System.Drawing.FontStyle.Bold);      Brush b = new System.Drawing.SolidBrush(Color.FromArgb(51,51,51));   //g.FillRectangle(new System.Drawing.SolidBrush(Color.Blue),0,0,image.Width, image.Height);   g.Clear(Color.FromArgb(187,241,0));   g.DrawString(checkCode, f, b, 3, 3);    Pen blackPen = new Pen(Color.White, 0);   Random rand = new Random();//   for (int i=0;i<1;i++)//   {//    int y = rand.Next(image.Height);//    g.DrawLine(blackPen,0,y,image.Width,image.Height);//   }//   //画图片的前景噪音点   for(int i=0; i<30; i++)   {    int x = rand.Next(image.Width);    int y = rand.Next(image.Height);     image.SetPixel(x, y, Color.FromArgb(rand.Next()));   }   System.IO.MemoryStream ms = new System.IO.MemoryStream();      image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);   System.Web.HttpContext.Current.Response.ClearContent();   System.Web.HttpContext.Current.Response.ContentType = "image/Jpeg";   System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());   g.Dispose();   image.Dispose();  }  #endregion