반응형

using System.Drawing;
using System.Drawing.Imaging;

private void Make_CAPTCHA()
    {
        Random r = new Random();

        int width = 110;
        int height = 30;
        int fontSize = 15;
        int pf = 10;
        int sf = 6;

        string[] strRandom = new string[r.Next(4,6)];
        string strRtot = "";
        for(int i = 0; i < strRandom.Length; i++)
        {
            strRandom[i] = r.Next(0,9).ToString();
            strRtot += strRandom[i];
        }

        ViewState["cap"] = strRtot;

        Bitmap bmp = new Bitmap(width, height);
        Graphics grp = Graphics.FromImage(bmp);

        //회색사각형
        SolidBrush backBrush = new SolidBrush(Color.DarkGray);
        Rectangle rect = new Rectangle(0, 0, width, height);
        grp.FillRectangle(backBrush, rect);

        //사각형안쪽 글자
        Font font = new Font("굴림", fontSize);
        SolidBrush strBrush = new SolidBrush(Color.Red);
        grp.DrawString(strRtot, font, strBrush, pf, sf);

        string strPath = MapPath(Request.ApplicationPath) + @"\images\tmp\";
        string strName = ctcoms.getTodate();
        strName = "CHA" + strName.Substring(strName.Length - 1, 1) + ".gif"; //주석하단(※01)

        bmp.Save(strPath + strName, ImageFormat.Gif);

        imgCHA.ImageUrl = "~/images/tmp/" + strFile;
    }
/*
 * ※01: strName = "CHA" + strName.Substring(strName.Length -2, 2) + ".gif";
 * 웹캐쉬문제로 ie계열에서 제대로 인식되지 않는다.
 * 방지문자이미지를 생성할때 10개중 하나로 인식하여
 * 동일한 파일이 인식돼는 일이 없도록 하였다.
 */

http://hoonsbara.com/ 참조하였음.

보통 문자열을 생성하여 입력하도록 하는 방식인데
귀찮아서 숫자열을 생성하여 숫자열의 길이도 랜덤, 생성되는 숫자도 랜덤으로 처리했다.

+ Recent posts