惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
IT之家
IT之家
A
About on SuperTechFans
量子位
Engineering at Meta
Engineering at Meta
B
Blog
The Cloudflare Blog
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Y
Y Combinator Blog
J
Java Code Geeks
D
DataBreaches.Net
aimingoo的专栏
aimingoo的专栏
T
Tailwind CSS Blog
H
Help Net Security
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - tenero

【原创】CSSOO的思想及CSS框架的应用(未整理完) 验证码整理 (转)VSS使用手册 c#基本关键字学习 Windows Vista 抢鲜中关于 wcf,wpf的介绍 windows mediaPlayer 图片切换(也适用其他插件的状态变化): windows_js_css WindowsMediaPlayer的常用属性和方法 随笔-记录 TiddlyWiki中文化 (网络收藏)WIKI Prototype文档解说--$ 基于StringTemplate的视图 Prototype.js介绍及扩展(待续) SSO-资料搜集 nodeType 从网络上搜索整理的 IMPORT功能函数 WEBService学习 大量的使用用户自定义控件对web性能有什么影响?
(转)验证码
tenero · 2008-02-18 · via 博客园 - tenero

网络搜索的:
using System;

using System.Drawing;

using System.Drawing.Imaging;

using System.IO;

namespace Chapter55

{

    class DrawingHelper

    {

        // 生成验证码字符串

        private string CreateCheckCodeString()

        {

            // 定义用于验证码的字符数组

            char [] allCharArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'W', 'X', 'Y', 'Z' };

            // 定义验证码字符串

            string randomCode = "";

            Random rand = new Random();

            // 生成四位验证码字符串

            for (int i = 0; i < 4; i++)

                randomCode += allCharArray[rand.Next(allCharArray.Length)];

            return randomCode;

        }

        // 生成验证码图片

        public void CreateCheckCodeImage(string sFilePath)

        {

            // 定义图片的长度

            int iWidth = 55;

            // 定义图片的宽度

            int iHeight = 22;

            // 定义pt的Arial字体,用于绘制文字

            Font font = new Font("Arial", 12, FontStyle.Bold);

            // 定义黑色的单色画笔,用于绘制文字

            SolidBrush brush = new SolidBrush(Color.Black);

            // 定义第钢笔,用于绘制干扰线

            Pen pen1 = new Pen(Color.Gray, 0); // 注意这里直接获得了一个现有的Color对象

            Pen pen2 = new Pen(Color.FromArgb(255, 100, 100, 100), 0); // 注意这里根据ARGB值获得了一个Color对象

            // 创建一个px*20px的图像

            Bitmap image = new Bitmap(iWidth, iHeight);

            // 从图像获取一个绘图画面

            Graphics g = Graphics.FromImage(image);

            // 清除整个绘图画面并以指定颜色填充

            g.Clear(ColorTranslator.FromHtml("#F0F0F0")); // 注意这里从HTML颜色代码获得了Color对象

            // 定义文字的绘制矩形区域

            RectangleF rect = new RectangleF(5, 2, iWidth, iHeight);

            // 定义一个随机数对象,用于绘制干扰线

            Random rand = new Random();

            // 生成两条横向的干扰线

            for (int i = 0; i < 2; i++)

            {

                // 定义起点

                Point p1 = new Point(0, rand.Next(iHeight));

                // 定义终点

                Point p2 = new Point(iWidth, rand.Next(iHeight));

                // 绘制直线

                g.DrawLine(pen1, p1, p2);

            }

            // 生成四条纵向的干扰线

            for (int i = 0; i < 4; i++)

            {

                // 定义起点

                Point p1 = new Point(rand.Next(iWidth), 0);

                // 定义终点

                Point p2 = new Point(rand.Next(iWidth), iHeight);

                // 绘制直线

                g.DrawLine(pen2, p1, p2);

            }

            // 绘制验证码文字

            g.DrawString(CreateCheckCodeString(), font, brush, rect);

            // 保存图片为Jpeg格式

            image.Save(sFilePath, ImageFormat.Jpeg);

            // 释放对象

            g.Dispose();

            image.Dispose();

        }

        // 生成缩略图

        public void CreateThumbnailImage(string sFileSrcPath, string sFileDstPath, int iSizeLimit)

        {

            // 检测源图片是否存在

            if (File.Exists(sFileSrcPath))

            {

                // 获取源图片图像

                Image image = Image.FromFile(sFileSrcPath);

                // 定义一个大小结构

                SizeF size = new SizeF(image.Width, image.Height);

                // 计算出符合要求的大小

                while (size.Width > iSizeLimit || size.Height > iSizeLimit)

                {

                    size.Height /= 1.1F;

                    size.Width /= 1.1F;

                }

                // 创建缩略图图像

                Bitmap bitmap = new Bitmap(Convert.ToInt16(size.Width), Convert.ToInt16(size.Height));

                // 创建缩略图绘图面

                Graphics g = Graphics.FromImage(bitmap);

                // 清除整个绘图画面并以透明色填充

                g.Clear(Color.Transparent);

                // 定义源图像矩形区域

                Rectangle Srcrect = new Rectangle(0, 0, image.Width, image.Height);

                // 定义缩略图矩形区域

                Rectangle Dstrect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);

                // 绘制缩略图

                g.DrawImage(image, Dstrect, Srcrect, GraphicsUnit.Pixel);

                // 存储为Jpeg图片

                bitmap.Save(sFileDstPath, ImageFormat.Jpeg);

                // 释放对象

                g.Dispose();

                image.Dispose();

                bitmap.Dispose();               

            }

        }

        // 生成黑白图像,仅支持Jpeg

        // 另:仅供学习使用,没有考虑效率问题,这种做法效率非常差

        public void CreateBlackAndWhiteImage(string sFileSrcPath, string sFileDstPath)

        {

            // 获取源图片图像

            Bitmap image = (Bitmap)Image.FromFile(sFileSrcPath);

            // 得到源图片宽度和高度

            int iWidth = image.Width;

            int iHeight = image.Height;

            // // 创建黑白图图像

            Bitmap bitmap = new Bitmap(iWidth,iHeight);

            // 定义临时变量

            Color color;

            int value;

            // 遍历所有象素

            for (int i = 0; i < iWidth; i++)

            {

                for (int j = 0; j < iHeight; j++)

                {

                    // 从源图像中得到当前象素的Color对象

                    color = image.GetPixel(i, j);

                    // 计算新的RGB值

                    value = Convert.ToInt16(0.299 * color.R) + Convert.ToInt16(0.114 * color.G) + Convert.ToInt16(0.587 * color.B);

                    // 设置新的象素

                    bitmap.SetPixel(i, j, Color.FromArgb(value, value, value));                   

                }

            }

            // 存储为Jpeg图片

            bitmap.Save(sFileDstPath, ImageFormat.Jpeg);

            // 释放对象

            image.Dispose();

            bitmap.Dispose();

        }

    }

}