openkava

专注物流系统,企业信息化,进出口业务,会展,电子商务 目标:CIO 架构师
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

收集整理的几个生成验证码的函数

Posted on 2007-09-05 14:17  openkava  阅读(356)  评论(0)    收藏  举报
using System;
using System.Drawing;
namespace Sbm.Web.Common
{
    
/// <summary>
    
/// Summary description for ImgUtil.
    
/// </summary>
    public class ImgUtil
    {
        
public ImgUtil()
        {
            
//
            
// TODO: Add constructor logic here
            
//
        }
        
#region     正弦曲线Wave扭曲图片 
        
/// <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 static  System.Drawing.Bitmap TwistImage(Bitmap srcBmp, bool bXDir, double dMultValue, double dPhase)
        {
            
double PI2=3.14159;
            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), 00, destBmp.Width, destBmp.Height);
            graph.Dispose();
  
            
double dBaseAxisLen = bXDir ? (double)destBmp.Height : (double)destBmp.Width;
  
            
for (int i = 0; i < destBmp.Width; i++)
            {
                
for (int j = 0; j < destBmp.Height; 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;
        }
        
#endregion 正弦曲线Wave扭曲图片 
        
#region 生成验证码图片
        
public  static  System.Drawing.Bitmap CreateCheckCodeImage(string checkCode)   
        {   
            
if (checkCode == null || checkCode.Trim() == String.Empty)   
                
return null;   
   
            System.Drawing.Bitmap image 
= new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);   
            Graphics g 
= Graphics.FromImage(image);   
   
            
try  
            {   
                
//生成随机生成器   
                Random random = new Random();   
   
                
//清空图片背景色   
                g.Clear(Color.White);   
   
                
//画图片的背景噪音线   
                for (int i = 0; i < 25; 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);   
                }   
   
                Font font 
= new System.Drawing.Font("Arial"12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));   
                System.Drawing.Drawing2D.LinearGradientBrush brush 
= new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(00, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2ftrue);   
                g.DrawString(checkCode, font, brush, 
22);   
   
                
//画图片的前景噪音点   
                for (int i = 0; i < 100; i++)   
                {   
                    
int x = random.Next(image.Width);   
                    
int y = random.Next(image.Height);   
   
                    image.SetPixel(x, y, Color.FromArgb(random.Next()));   
                }   
   
                
//画图片的边框线   
                g.DrawRectangle(new Pen(Color.Silver), 00, image.Width - 1, image.Height - 1);   
   
                 
return image;
                 
                 
            }   
             
            
finally  
            {   
                g.Dispose();   
                
//image.Dispose(); 
  
            }   
        }  
        
public  static string GenerateCheckCode()   
        {   
            
int number;   
            
char code;   
            
string checkCode = String.Empty;   
    
            System.Random random 
= new Random();   
    
            
for (int i = 0; i < 5; i++)   
            {   
                number 
= random.Next();   
    
                
if (number % 2 == 0)   
                    code 
= (char)('0' + (char)(number % 10));   
                
else  
                    code 
= (char)('A' + (char)(number % 26));   
    
                checkCode 
+= code.ToString();   
            }   
    
            
//Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));   
    
            
return checkCode;   
        }   
 
        
#endregion 生成验证码图片

        
/// <summary>
        
/// 生成缩略图
        
/// </summary>
        
/// <param name="originalImagePath">源图路径(物理路径)</param>
        
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
        
/// <param name="width">缩略图宽度</param>
        
/// <param name="height">缩略图高度</param>
        
/// <param name="mode">生成缩略图的方式</param>
        public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode,string type)
        {
            System.Drawing.Image originalImage 
= System.Drawing.Image.FromFile(originalImagePath);

            
int towidth = width;
            
int toheight = height;

            
int x = 0;
            
int y = 0;
            
int ow = originalImage.Width;
            
int oh = originalImage.Height;

            
switch (mode)
            {
                
case "HW"://指定高宽缩放(可能变形)
                    break;
                
case "W"://指定宽,高按比例
                    toheight = originalImage.Height * width / originalImage.Width;
                    
break;
                
case "H"://指定高,宽按比例
                    towidth = originalImage.Width * height / originalImage.Height;
                    
break;
                
case "Cut"://指定高宽裁减(不变形)
                    if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                    {
                        oh 
= originalImage.Height;
                        ow 
= originalImage.Height * towidth / toheight;
                        y 
= 0;
                        x 
= (originalImage.Width - ow) / 2;
                    }
                    
else
                    {
                        ow 
= originalImage.Width;
                        oh 
= originalImage.Width * height / towidth;
                        x 
= 0;
                        y 
= (originalImage.Height - oh) / 2;
                    }
                    
break;
                
case "DB"://等比缩放(不变形,如果高大按高,宽大按宽缩放)
                    if ((double)originalImage.Width / (double)towidth < (double)originalImage.Height / (double)toheight)
                    {
                        toheight 
= height;
                        towidth 
= originalImage.Width * height / originalImage.Height;
                    }
                    
else
                    {
                        towidth 
= width;
                        toheight 
= originalImage.Height * width / originalImage.Width;
                    }
                    
break;
                
default:
                    
break;
            }

            
//新建一个bmp图片
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);

            
//新建一个画板
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);

            
//设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

            
//设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            
//清空画布并以透明背景色填充
            g.Clear(System.Drawing.Color.Transparent);

            
//在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(originalImage, new System.Drawing.Rectangle(00, towidth, toheight),
                
new System.Drawing.Rectangle(x, y, ow, oh),
                System.Drawing.GraphicsUnit.Pixel);

            
try
            {
                
//保存缩略图
                if (type=="JPG")
                {
                    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                
if (type=="BMP")
                {
                    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp);
                }
                
if (type=="GIF")
                {
                    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif);
                }
                
if (type=="PNG")
                {
                    bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png);
                }
            }
            
catch (System.Exception e)
            {
                
throw e;
            }
            
finally
            {
                originalImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
        }

        
/**/
        
/// <summary>
        
/// 在图片上增加文字水印
        
/// </summary>
        
/// <param name="Path">原服务器图片路径</param>
        
/// <param name="Path_sy">生成的带文字水印的图片路径</param>
        public static void AddShuiYinWord(string Path, string Path_sy)
        {
            
string addText = "测试水印";
            System.Drawing.Image image 
= System.Drawing.Image.FromFile(Path);
            System.Drawing.Graphics g 
= System.Drawing.Graphics.FromImage(image);
            g.DrawImage(image, 
00, image.Width, image.Height);
            System.Drawing.Font f 
= new System.Drawing.Font("Verdana"16);
            System.Drawing.Brush b 
= new System.Drawing.SolidBrush(System.Drawing.Color.Blue);

            g.DrawString(addText, f, b, 
1515);
            g.Dispose();

            image.Save(Path_sy);
            image.Dispose();
        }

        
/**/
        
/// <summary>
        
/// 在图片上生成图片水印
        
/// </summary>
        
/// <param name="Path">原服务器图片路径</param>
        
/// <param name="Path_syp">生成的带图片水印的图片路径</param>
        
/// <param name="Path_sypf">水印图片路径</param>
        public static  void AddShuiYinPic(string Path, string Path_syp, string Path_sypf)
        {
            System.Drawing.Image image 
= System.Drawing.Image.FromFile(Path);
            System.Drawing.Image copyImage 
= System.Drawing.Image.FromFile(Path_sypf);
            System.Drawing.Graphics g 
= System.Drawing.Graphics.FromImage(image);
            g.DrawImage(copyImage, 
new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 00, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
            g.Dispose();

            image.Save(Path_syp);
            image.Dispose();
        }

    }
}