zoukankan      html  css  js  c++  java
  • 生成验证码

    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class vcode : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    Page.Response.AppendHeader("Cache-Control", "no-cache");
    string verifyCode = System.Guid.NewGuid().ToString().Substring(0,4).ToUpper();

    verifyCode = verifyCode.Replace("O", "X").Replace("I", "Z");

    Session["vc"] = verifyCode.ToLower();
    DrawPic(verifyCode);
    GC.Collect();
    }

    private void CheckReferer()
    {
    if (Request.ServerVariables["HTTP_REFERER"] != null)
    {
    if (Request.ServerVariables["HTTP_REFERER"].ToString().Split('/')[2] != Request.ServerVariables["HTTP_HOST"].ToString())
    {
    Response.Redirect("error/403.html");
    }
    }
    else
    {
    Response.Redirect("error/403.html");
    }
    }

    private void DrawPic(string vc)
    {
    int width = 78;
    int height = 24;
    Bitmap bm = new Bitmap(width, height);
    Graphics g;
    g = Graphics.FromImage(bm);
    g.Clear(Color.White);
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

    // pic
    Random x = new Random();
    int[] a = new int[4];
    int[] b = new int[4];
    int[] c = new int[4];
    // 杂线
    for (int i = 0; i < 5; i++)
    {
    g.DrawLine(Pens.DimGray, x.Next(0, width - 1), x.Next(0, height - 1), x.Next(0, width - 1), x.Next(0, height - 1));
    }
    for (int i = 0; i < 5; i++)
    {
    g.DrawLine(Pens.LightGray, x.Next(0, width - 1), x.Next(0, height - 1), x.Next(0, width - 1), x.Next(0, height - 1));
    }
    a[0] = x.Next(0, 7);
    a[1] = x.Next(0, 7);
    a[2] = x.Next(0, 7);
    a[3] = x.Next(0, 7);
    b[0] = x.Next(0, 4);
    b[1] = x.Next(0, 4);
    b[2] = x.Next(0, 4);
    b[3] = x.Next(0, 4);
    c[0] = x.Next(0, 8);
    c[1] = x.Next(0, 8);
    c[2] = x.Next(0, 8);
    c[3] = x.Next(0, 8);
    //g.DrawRectangle(Pens.DimGray, 0, 0, width - 1, height - 1);
    string fonts = "Verdana,Verdana,Arial,Tahoma";
    FontStyle[] fs = new FontStyle[4];
    fs[0] = FontStyle.Bold;
    fs[1] = FontStyle.Bold;
    fs[2] = FontStyle.Bold;
    fs[3] = FontStyle.Bold;

    // 写验证码
    int fontSize = 16;
    float[] angle = new float[4];
    angle[0] = x.Next(0, 20);
    angle[1] = x.Next(0, 20);
    angle[2] = x.Next(0, 20);
    angle[3] = x.Next(0, 20);

    string reg = "";
    if (Request.QueryString["reg"] != null)
    {
    reg = Request.QueryString["reg"];
    }

    for (int i = 0; i < 4; i++)
    {
    g.TranslateTransform(i * 16 + 1 + c[i] + 0, 0f);
    g.RotateTransform(angle[i] - 10f);
    if (reg == "p")
    {
    g.DrawString(vc.Substring(i, 1), new Font(fonts.Split(',')[b[i]], fontSize, fs[b[i]]), Brushes.DarkBlue, new Point(0, 0));
    }
    else if (reg == "s")
    {
    g.DrawString(vc.Substring(i, 1), new Font(fonts.Split(',')[b[i]], fontSize, fs[b[i]]), Brushes.Maroon, new Point(0, 0));
    }
    else
    {
    g.DrawString(vc.Substring(i, 1), new Font(fonts.Split(',')[b[i]], fontSize, fs[b[i]]), Brushes.DarkGreen, new Point(0, 0));
    }
    g.ResetTransform();
    }

    // 变形
    //Bitmap bmp = new Bitmap(width, height);
    //Disort(bm, ref bmp);
    //bm = bmp;

    //g.DrawRectangle(Pens.Black, 0, 0, width - 1/2, height - 1/2);
    // 杂点
    for (int i = 0; i < 10; i++)
    {
    int startX = x.Next(1, width - 2);
    int startY = x.Next(1, height - 2);
    g.DrawRectangle(Pens.LightGray, startX, startY, 1, 1);
    //g.DrawLine(Pens.Gray, startX + 1, startY + 1, startX + 2, startY + 2);
    }
    a = null;
    b = null;
    c = null;
    fs = null;
    GC.Collect();

    bm.Save(Response.OutputStream, ImageFormat.Jpeg);
    }

    private int ColorRGB(System.Drawing.Color posClr, string RGB)
    {
    //return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;
    if (RGB == "R")
    {
    return posClr.R;
    }
    if (RGB == "G")
    {
    return posClr.G;
    }
    else
    {
    return posClr.B;
    }
    }

    /// 扭曲,逐点方式
    public void Disort(Bitmap bmpobj, ref Bitmap bmpOut)
    {
    int[,] rs = new int[bmpobj.Height, bmpobj.Width];
    int[,] gs = new int[bmpobj.Height, bmpobj.Width];
    int[,] bs = new int[bmpobj.Height, bmpobj.Width];
    for (int i = 0; i < bmpobj.Height; i++)
    {
    for (int j = 0; j < bmpobj.Width; j++)
    {
    rs[i, j] = ColorRGB(bmpobj.GetPixel(j, i), "R");
    gs[i, j] = ColorRGB(bmpobj.GetPixel(j, i), "G");
    bs[i, j] = ColorRGB(bmpobj.GetPixel(j, i), "B");
    //bmpOut.SetPixel(j, i, Color.FromArgb(r, g, b));
    }
    }

    Random x = new Random();
    int mover = 0;
    for (int i = 0; i < bmpobj.Height; i++)
    {
    if (x.Next(0, 10) <= 5)
    {
    mover--;
    }
    else
    {
    mover++;
    }
    for (int j = 0; j < bmpobj.Width; j++)
    {
    if (j + mover >= bmpobj.Width)
    {
    bmpOut.SetPixel(j, i, Color.FromArgb(255, 255, 255));
    }
    else if (j + mover < 0)
    {
    bmpOut.SetPixel(j, i, Color.FromArgb(255, 255, 255));
    }
    else
    {
    bmpOut.SetPixel(j, i, Color.FromArgb(rs[i, j + mover], gs[i, j + mover], bs[i, j + mover]));
    }
    }
    }
    }
    }

  • 相关阅读:
    有点成熟的短句,最新个性签名
    ACM2039_三角形三边关系
    Android 绘制中国地图
    Opengl-法线贴图(用来细化表面的表现表现的凹凸)
    Go的sync
    Laravel Study(使用 Laravel )
    对于宅男来说,硬盘里的数据就是命
    设计模式之模板方法模式
    游戏掉落道具掉落
    NEWMING
  • 原文地址:https://www.cnblogs.com/taomylife/p/3216938.html
Copyright © 2011-2022 走看看