C#生成验证码程序

做网站都必须有生成验证吗的地方,下面简单贴一段生成验证吗的程序

View Code
static void Main(string[] args)
        {
            try
            {
                Bitmap image = CreateCheckCodeImage(GetCheckCodeStr(4));
                image.Save(@"img.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
            }
        }

        /// <summary>
        /// 生成一个随机的验证码
        /// </summary>
        /// <param name="codeLength">需要生成的验证码长度</param>
        /// <returns>生成的验证码字符串</returns>
        private static string GetCheckCodeStr(int codeLength)
        {
            StringBuilder sb = new StringBuilder();
            System.Random random = new Random();

            //循环生成验证码
            for (int i = 0; i < codeLength; i++)
            {
                if (random.Next() % 2 == 0)
                {
                    sb.Append((char)('0' + (char)(random.Next() % 10)));
                }
                else
                {
                    sb.Append((char)('A' + (char)(random.Next() % 26)));
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 创建一个验证码图片,并保存图片
        /// </summary>
        /// <param name="checkCode">验证码字符串</param>
        private static Bitmap CreateCheckCodeImage(string checkCode)
        {
            //如果生成checkCode出现错误,返回异常
            if (string.IsNullOrEmpty(checkCode))
                throw new Exception("checkCode IsNullOrEmpty!");

            System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 26.0)), 40);
            Graphics g = Graphics.FromImage(image);

            try
            {
                //生成随机生成器
                Random random = new Random();

                //清空图片背景色
                g.Clear(Color.White);

                //画图片的背景噪音线
                for (int i = 0; i < 10; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);

                    g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
                }

                for (int index = 0; index < checkCode.Length; ++index)
                {
                    Font font = new System.Drawing.Font(FontFamily.GenericSansSerif, 16 + random.Next(10), (System.Drawing.FontStyle.Regular | System.Drawing.FontStyle.Italic));
                    System.Drawing.Drawing2D.LinearGradientBrush brush =
        new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.ForestGreen, Color.DeepSkyBlue, 1.2f, true);
                    g.DrawString(checkCode[index].ToString(), font, brush, image.Width / checkCode.Length * index + random.Next(6) - 3, 0);
                }


                //画图片的前景噪音点
                for (int i = 0; i < 30; i++)
                {
                    int x = random.Next(image.Width);
                    int y = random.Next(image.Height);

                    image.SetPixel(x, y, Color.FromArgb(random.Next()));
                }

                //画图片的前景噪音线
                for (int i = 0; i < 2; i++)
                {
                    int x1 = random.Next(image.Width);
                    int x2 = random.Next(image.Width);
                    int y1 = random.Next(image.Height);
                    int y2 = random.Next(image.Height);

                    g.DrawLine(new Pen(Color.White), x1, y1, x2, y2);
                }

                //画图片的边框线
                g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
                return image;
            }
            catch (Exception ex)
            {
                throw new Exception("checkCode : " + checkCode + Environment.NewLine + ex.ToString());
            }
            finally
            {
                g.Dispose();
            }
        }
    }

插入一个片段,改变图片大小的程序段

View Code
try
            {
                while (true)
                {
                    string fileName = Console.ReadLine();
                    Image image = Image.FromFile(fileName);
                    double size = double.Parse(Console.ReadLine());
                    string result = Regex.Replace(fileName, @"(?=\.\w+$)", "1");
                    Image saveImg = new Bitmap(image, new Size((int)(image.Width * size), (int)(image.Height * size)));
                    saveImg.Save(result,System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
            }

可以看到Image saveImg = new Bitmap(image, new Size((int)(image.Width * size), (int)(image.Height * size)));

这里从新new了一个bitmap对象,并且传入了一个大小信息,在这里改变了图片的大小!

还有就是改变图片格式的方法,在保存的时候选择图片格式即可:

saveImg.Save(result,System.Drawing.Imaging.ImageFormat.Jpeg);

这里选择的就是jpg的格式,其他的格式还有很多,另行发挥吧到时候!

这个只是一个简单的改变图片大小的方法,高深的等研究过再说……

 

记在这里,以后学习!

posted @ 2012-06-29 13:03  pmars  阅读(368)  评论(0编辑  收藏  举报