Code代码片断(5do8)

GDI+控件线程IO流ADO.NET接口类,函数语法

新主题
c#获取GIF的各帧
c#验证码
c#判断文件是否是图片
说话自由

首页 » .NET/C# » GDI+ »

c#验证码

标签: Drawing
using System;
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;
using System.Drawing;

public partial class GetImg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.CreateImage(GetRandString(4));
    }
    private string GetRandString(int MaxNum)
    {
        int number;
        char code;
        string checkCode = String.Empty;
        System.Random random = new Random();
        for(int i=1; i<=MaxNum;i++) {
            number = random.Next();
            if(number % 2 == 0)
                code = (char)('0' + (char)(number % 10));
            else if(number % 3 == 0)
                code = (char)('a' + (char)(number % 26));
            else
                code = (char)('A' + (char)(number % 26));
            if ((code == '0') || (code == 'i') || (code == 'I') || (code == 'O') || (code == 'o') || (code == '1') || (code == 'l'))
            {
                --i;
                continue;
            }
            checkCode += code.ToString();
        }

        return checkCode;
    }
    private void CreateImage(string checkCode)
    {
        int iwidth = (int)(checkCode.Length * 15+3);
        System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 28);
        Graphics g = Graphics.FromImage(image);
        g.Clear(Color.White);
        //定义颜色
        Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
        //定义字体           
        string[] font = {"Verdana","Microsoft Sans Serif","Comic Sans MS","Arial","宋体"};
        Random rand = new Random();



        //输出不同字体和颜色的验证码字符
        for (int i = 0; i < checkCode.Length; i++)
        {
            int cindex = rand.Next(7);
            int findex = rand.Next(5);

            Font f = new System.Drawing.Font(font[findex], 16, 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), f, b,   (i * 14), ii);
        }

        //g.DrawRectangle(new Pen(Color.Gray, 1), 0, 0, image.Width - 1, image.Height - 1);
        g.Dispose();
        //image = TwistImage(image,true,3,0.4);  
        //画一个边框

        //输出到浏览器
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        Response.ClearContent();
        Response.ContentType = "image/Jpeg";
        Response.BinaryWrite(ms.ToArray());
        
        image.Dispose();
    }
    /// <summary> 
    /// 正弦曲线Wave扭曲图片(Edit By 51aspx.com)
    /// </summary>
    /// <param name="srcBmp">图片路径</param> 
    /// <param name="bXDir">如果扭曲则选择为True</param>
    /// <param name="nMultValue">波形的幅度倍数,越大扭曲的程度越高,一般为3</param>
    /// <param name="dPhase">波形的起始相位,取值区间[0-2*PI)</param> 
    /// <returns></returns>
    public System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
        { 
            double PI2 = 6.28;
            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 = 1; i < destBmp.Width-1; i++)
            { 
                for (int j = 1; j < destBmp.Height-1; 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;
        }

}

ccdot写于2008-9-22 14:14:04

如果愿意,请留下你观点或者感受...
称呼*
内容*
验证码*