附加码生产程序(c#)http://dev.csdn.net/article/article/36/36945.shtm
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing.Imaging;
namespace CEPN.UserControl
{
/// <summary>
/// ChrToImages 的摘要说明。
/// </summary>
public class ChrToImages : System.Web.UI.Page
{
protected Random rnd;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
rnd=new Random();
//Font_Size 字体大小integer
//Char_Number 验证码的位数
//BackgroundColor 背景颜色 十六进制的字符串
MemoryStream My_Stream = Get_Images(20,2,"#ffffff");
Show_image(My_Stream);
My_Stream.Close();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
//Font_Size 字体大小integer
//Char_Number 验证码的位数
//BackgroundColor 背景颜色 十六进制的字符串
/*把字符转换为图像,并且保存到内存流*/
protected MemoryStream Get_Images(int Font_Size,int Char_Number,string BackgroundColor)
{
int image_w=(int)(Font_Size*1.5)+Font_Size*Char_Number;
//这个数字在调用页面需要,你要自己算出明确的数值 注意注意注意注意!!!!!
int image_h=(int)(2.5*Font_Size);
//这个数字在调用页面需要,你要自己算出明确的数值 注意注意注意注意!!!!!
Bitmap Temp_Bitmap;//封装GDI+位图
Graphics Temp_Graphics;//封装GDI+绘图面
Color Color_Back=ColorTranslator.FromHtml(BackgroundColor);//背景颜色
//Temp_Bitmap=new Bitmap(image_w,5*image_h,PixelFormat.Format24bppRgb);//注意 确定背景大小
Temp_Bitmap=new Bitmap(image_w,image_h,PixelFormat.Format24bppRgb);
Temp_Graphics = Graphics.FromImage(Temp_Bitmap);
Temp_Graphics.FillRectangle(new SolidBrush(Color_Back),new Rectangle(0, 0,image_w, 5*image_h));//注意 绘制背景
string Sesson_Company="";//为了进行验证比较
int n;
for (n=0;n<=(Char_Number-1);n++)
{
string Show_Str=getChar();//要显示为图像的字符
Sesson_Company=Sesson_Company+Show_Str;
int Show_Str_Font_Size=(int)(3*rnd.Next(10)*0.1+(Font_Size-2));//字体随机大小 下波动2
Color Color_Font=ColorTranslator.FromHtml(getColor());//字体随机颜色
string Show_Font_Name=getFont();//字体
Font Show_Str_Font =new Font(Show_Font_Name,Show_Str_Font_Size,FontStyle.Bold);//定义文本格式(字体,字号,粗体)
Temp_Graphics.DrawString(Show_Str, Show_Str_Font, new SolidBrush(Color_Font), (int)(Font_Size/2)+n*Font_Size, (int)(image_h*0.125 * rnd.Next(10)*0.1+image_h*0.08));//绘出字符 '绘字符的Y方向下波动+4
}
Session["imagenumber"]=Sesson_Company.Trim().ToLower();;
Font_Size=Font_Size*4;
image_h=(int)(2.5*Font_Size);
/*Temp_Graphics.DrawString("欢", new Font(getFont1(),(int)(3 * rnd.Next(10)*0.1+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), (int)(Font_Size/5+0*1.3*Font_Size), (int)(image_h/4)+(int)(image_h*0.125 * rnd.Next(10)*0.1+image_h*0.08));//'绘出字符 '绘字符的Y方向下波动+4
Temp_Graphics.DrawString("迎", new Font(getFont1(),(int)(3 * rnd.Next(10)*0.1+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), (int)(Font_Size/5+1*1.3*Font_Size), (int)(image_h/4)+(int)(image_h*0.125 * rnd.Next(10)*0.1+image_h*0.08));//'绘出字符 '绘字符的Y方向下波动+4
Temp_Graphics.DrawString("使", new Font(getFont1(),(int)(3 * rnd.Next(10)*0.1+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), (int)(Font_Size/5+2*1.3*Font_Size), (int)(image_h/4)+(int)(image_h*0.125 * rnd.Next(10)*0.1+image_h*0.08));//'绘出字符 '绘字符的Y方向下波动+4
Temp_Graphics.DrawString("用", new Font(getFont1(),(int)(3 * rnd.Next(10)*0.1+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), (int)(Font_Size/5+3*1.3*Font_Size), (int)(image_h/4)+(int)(image_h*0.125 * rnd.Next(10)*0.1+image_h*0.08));//'绘出字符 '绘字符的Y方向下波动+4
Temp_Graphics.DrawString("! ", new Font(getFont1(),(int)(3 * rnd.Next(10)*0.1+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), (int)(Font_Size/5+4*1.3*Font_Size), (int)(image_h/4)+(int)(image_h*0.125 * rnd.Next(10)*0.1+image_h*0.08));//'绘出字符 '绘字符的Y方向下波动+4
Temp_Graphics.DrawString("请", new Font(getFont1(),(int)(3 * rnd.Next(10)*0.1+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), (int)(Font_Size/5+5*1.3*Font_Size), (int)(image_h/4)+(int)(image_h*0.125 * rnd.Next(10)*0.1+image_h*0.08));//'绘出字符 '绘字符的Y方向下波动+4
Temp_Graphics.DrawString("按", new Font(getFont1(),(int)(3 * rnd.Next(10)*0.1+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), (int)(Font_Size/5+6*1.3*Font_Size), (int)(image_h/4)+(int)(image_h*0.125 * rnd.Next(10)*0.1+image_h*0.08));//'绘出字符 '绘字符的Y方向下波动+4
Temp_Graphics.DrawString("F5", new Font(getFont1(),(int)(3 * rnd.Next(10)*0.1+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), (int)(Font_Size/5+7*1.3*Font_Size), (int)(image_h/4)+(int)(image_h*0.125 * rnd.Next(10)*0.1+image_h*0.08));//'绘出字符 '绘字符的Y方向下波动+4
Temp_Graphics.DrawString("键", new Font(getFont1(),(int)(3 * rnd.Next(10)*0.1+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), (int)(Font_Size/5+8*1.3*Font_Size), (int)(image_h/4)+(int)(image_h*0.125 * rnd.Next(10)*0.1+image_h*0.08));//'绘出字符 '绘字符的Y方向下波动+4
Temp_Graphics.DrawString("!", new Font(getFont1(),(int)(3 * rnd.Next(10)*0.1+(Font_Size-2)),FontStyle.Bold), new SolidBrush(ColorTranslator.FromHtml(getColor())), (int)(Font_Size/5+9*1.3*Font_Size), (int)(image_h/4)+(int)(image_h*0.125 * rnd.Next(10)*0.1+image_h*0.08));//'绘出字符 '绘字符的Y方向下波动+4
*/
MemoryStream Temp_Stream=new MemoryStream();
Temp_Bitmap.Save(Temp_Stream,System.Drawing.Imaging.ImageFormat.Bmp);
Temp_Graphics.Dispose();//'释放资源
Temp_Bitmap.Dispose();//'释放资源
Temp_Stream.Close();//'关闭打开的流文件
return Temp_Stream;//'返回流
}