Esempio n. 1
0
        /// <summary>
        /// 将生成验证码的代码拆分成若干个方法,并为其添加异常机制,要求能够:o显式的抛出一个自定义异常
        /// 捕获并包裹一个被抛出的异常,记入日志文件,然后再次抛出,o根据不同的异常,给用户相应的友好的异常提示
        /// 使用using释放文件资源
        /// </summary>
        public static void CaptchaInsertException(string custom = null)//需要自己传入一个验证码,要求不得大于4个字符
        {
            //主方法,生成画布添加背景颜色
            Bitmap   image   = new Bitmap(200, 100);
            Graphics drawing = Graphics.FromImage(image);

            drawing.Clear(Color.White);

            //添加随机种子
            Random random = new Random();

            //生成背景像素点
            Captcha.captchaBackGroundPixel(image, random);

            //生成背景干扰线条
            Captcha.captchaBackgroundDrawing(drawing, random);



            //为了捕获异常特意声明的一个Try catch
            try
            {
                if (custom.Length > 4)
                {//自定义的异常
                    throw new HomeworkException("传入的验证码请不要超过4个字符");
                }
                else
                {//输入正确就跑到正常的执行手段里面去。
                    Captcha.captchaMaker(random, drawing, image, custom);
                }
            }
            catch (HomeworkException Record)
            {
                string path = @"C:\Users\Administrator\source\repos\luckstack3\Captcha\Log.txt";
                using (StreamWriter writer = File.AppendText(path))
                {
                    DateTime date = DateTime.Now;//设置日志时间
                    string   time = date.ToString("yyyy-MM-dd HH:mm:ss");


                    //日志存放
                    //writer = File.AppendText(path);
                    writer.WriteLine("异常时间" + time);
                    writer.WriteLine("异常对象" + Record.Source);
                    writer.WriteLine("调用堆栈" + Record.StackTrace.Trim());
                    writer.WriteLine("调用堆栈" + Record.ToString());

                    writer.Flush();
                }

                //重新抛出
                throw new HomeworkException("传入的验证码请不要超过4个字符");
            }
            catch (Exception)
            {
                throw new Exception("正确传入4个字符的自定义验证码");
            }
            //生成随机验证码
            Captcha.captchaMaker(random, drawing, image);

            //存储图片
            image.Save(@"C:\Users\Administrator\source\repos\luckstack3\Captcha\Captcha.jpg", ImageFormat.Jpeg);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            EmailSettle emailSettle = new EmailSettle(@"C:\Users\Administrator\source\repos\luckstack3\Captcha\Email.txt");

            emailSettle.GetEmail();

            Captcha.RefreshCaptcha();
            #region 2020.4.16异步作业

            Captcha.CaptchaAsync();


            //重构之前的验证码作业:
            //1.创建一个新的前台线程(Thread),在这个线程上运行生成随机字符串的代码
            //Thread thread = new Thread(new ThreadStart(Captcha.RefreshCaptcha));
            //Bitmap image = new Bitmap(200, 100); ;
            //Graphics drawing;

            //Random random = new Random();
            //int random_0_To_200 = random.Next(0, 200);
            //int random_0_To_100 = random.Next(0, 100);
            ////2.在一个任务(Task)中生成画布
            //Action draingMap = () =>
            //{
            //    Console.WriteLine("当前线程ID是: " + Thread.CurrentThread.ManagedThreadId);

            //    drawing = Graphics.FromImage(image);
            //};
            //Task task = new Task(draingMap);

            /////3.使用生成的画布,用两个任务完成:
            /////1.在画布上添加干扰线条
            /////2.在画布上添加干扰点
            /////
            //Parallel.Invoke(() =>
            //{
            //    Console.WriteLine("当前线程ID是: " + Thread.CurrentThread.ManagedThreadId);

            //    for (int i = 0; i < 200; i++)
            //    {
            //        image.SetPixel(random_0_To_200, random_0_To_200, Color.BurlyWood);
            //        image.SetPixel(random_0_To_200, random_0_To_200, Color.IndianRed);
            //        image.SetPixel(random_0_To_200, random_0_To_200, Color.DarkCyan);
            //        image.SetPixel(random_0_To_200, random_0_To_200, Color.DarkViolet);
            //    }

            //},
            //() =>
            //{
            //    drawing = Graphics.FromImage(image);
            //    for (int i = 0; i < 25; i++)
            //    {
            //        Console.WriteLine("当前线程ID是: " + Thread.CurrentThread.ManagedThreadId);

            //        drawing.DrawCurve(new Pen(new SolidBrush(Color.Gray), 1),
            //            new Point[] {
            //                new Point(random_0_To_200,random_0_To_100),
            //                new Point(random_0_To_200,random_0_To_200),
            //                new Point(random_0_To_200, random_0_To_100) });

            //        drawing.DrawCurve(new Pen(new SolidBrush(Color.Chocolate), 1),
            //            new Point[] {
            //                new Point(random_0_To_200,random_0_To_100),
            //                new Point(random_0_To_200,random_0_To_200),
            //                new Point(random_0_To_200, random_0_To_100) });
            //    }
            //});
            ////随机验证码。
            //string subCaptcha = "1234567890QAZWSXEDCRFVTGBYHNUJMIKOLP";
            //string captcha = "";
            //for (int i = 0; i < 4; i++)
            //{
            //    captcha = captcha + subCaptcha.Substring(random.Next(subCaptcha.Length), 1);
            //}
            //drawing = Graphics.FromImage(image);
            //drawing.DrawString(
            //    captcha, new Font("微软雅黑", 35),
            //    new SolidBrush(Color.SaddleBrown), 45, 25);
            ////4.将生成的验证码图片异步的存入文件
            ////5.能捕获抛出的若干异常,并相应的处理
            ////6.以上作业,需要在控制台输出线程和Task的Id,以演示异步并发的运行。
            //Parallel.Invoke(() =>
            //{
            //    try
            //    {
            //        Console.WriteLine("当前线程ID是: "+Thread.CurrentThread.ManagedThreadId);
            //        image.Save(@"C:\Users\Administrator\source\repos\luckstack3\Captcha\CaptchaAsync.jpg",
            //                            ImageFormat.Jpeg);
            //        throw new Exception("人家大佬的作业要求");
            //    }
            //    catch (Exception)
            //    {
            //        Console.WriteLine("我是一只异常,只是输出给你看,");
            //    }

            //});
            #endregion

            #region Email作业--之没写出来
            /// 现有一个txt文件,里面存放了若干email地址,使用分号(;)或者换行进行了分隔。
            /// 请删除其中重复的email地址,并按每30个email一行(行内用;分隔)重新组织

            //EmailSettle.MakerEail();

            //EmailSettle.DeleteDuplicateEmail();
            #endregion



            #region 画验证码作业

            ///Captcha Maker Method
            //Captcha.RefreshCaptcha();


            //Captcha Exception Method
            //Captcha.CaptchaInsertException("52plk");
            #endregion
        }