附加码生产程序(c#)http://dev.csdn.net/article/article/36/36945.shtm

yyy431706 2005-03-14 09:04:25
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;//'返回流



}
...全文
203 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yyy431706 2005-06-09
  • 打赏
  • 举报
回复
把用户输入的数据与Session["imagenumber"]比较就可以了,这个Session["imagenumber"]就是图片上的数据.
wenxinwenyi 2005-04-15
  • 打赏
  • 举报
回复
楼主想问一下,怎么判断输入的数据和附加码是否一致呢?
冷月孤峰 2005-03-14
  • 打赏
  • 举报
回复
楼主好啊,但是我运行的时候怎么出现保存对话框啊
yyy431706 2005-03-14
  • 打赏
  • 举报
回复
我的项目中刚好要用到附加码,所以将 dudu8686的翻译成了
C#,这是原VB.net的地址:http://dev.csdn.net/article/article/36/36945.shtm ,非常感谢dudu8686所作的工作!
qiuzhong1983 2005-03-14
  • 打赏
  • 举报
回复
支持,学习
  • 打赏
  • 举报
回复
关注!
siugwan 2005-03-14
  • 打赏
  • 举报
回复
up
yyy431706 2005-03-14
  • 打赏
  • 举报
回复
protected void Show_image(MemoryStream Show_Stream)
{
Response.ClearContent();
Response.ContentType = "Image/Jpeg";
Response.BinaryWrite(Show_Stream.ToArray());
Response.End();

}
protected string getChar()//获得随机字符 0-9 a-z A-Z
{
string[] Char_array=new string[5];

Char_array[0] = Convert.ToString((int)(10 * rnd.Next(10)*0.1+48),16);
Char_array[1] = Convert.ToString((int)(26 * rnd.Next(10)*0.1+65),16);
Char_array[2] = Convert.ToString((int)(26 * rnd.Next(10)*0.1+65),16);

Char_array[3] = Convert.ToString((int)(26 * rnd.Next(10)*0.1+97),16);
Char_array[4] = Convert.ToString((int)(26 * rnd.Next(10)*0.1+97),16);
return Char_array[(int)(5 * rnd.Next(10)*0.1)];

}
protected string getColor()//获得随机字体
{
int int_a,int_b,int_c;

int_a=(int)(180 * rnd.Next(10)*0.1+20);
int_b=(int)(180 * rnd.Next(10)*0.1+20);
int_c=(int)(180 * rnd.Next(10)*0.1+20);

if ((int_a>150) && (int_b>150) && (int_c>150))
int_a=(int)(150 * rnd.Next(10)*0.1+20);
return "#" + Convert.ToString(int_a,16) +Convert.ToString(int_b,16) + Convert.ToString(int_c,16);

}
protected string getFont()//'获得随机字体
{

string[] font_array=new string[7];

font_array[0] = "Book Antiqua";
font_array[1] = "Microsoft Sans Serif";

font_array[2] = "Comic Sans MS";
font_array[3] = "Comic Sans MS";
font_array[4] = "Comic Sans MS";

font_array[5] = "Comic Sans MS";
font_array[6] = "Comic Sans MS";
return font_array[(int)(7 * rnd.Next(10)*0.1)];
}
protected string getFont1()//获得随机字体
{
string[] font_array=new string[10];

font_array[0] = "华文行楷";
font_array[1] = "隶书";
font_array[2] = "华文彩云";
font_array[3] = "方正舒体";
font_array[4] = "华文彩云";
font_array[5] = "方正舒体";
font_array[6] = "华文彩云";
font_array[7] = "方正舒体";
font_array[8] = "楷体_GB2312";
font_array[9] = "华文行楷";
return font_array[(int)(10 * rnd.Next(10)*0.1)];

}

}
}
yyy431706 2005-03-14
  • 打赏
  • 举报
回复
因为这个页面生成的是图片,还需要另一个页面调用,如下:
<img width="110" height="50" src="ChrToImages.aspx" align="absMiddle"
alt="附加码">

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧