首页 新闻 会员 周边

动态生成的验证码图片如何绑定到web控件上

0
悬赏园豆:20 [已关闭问题]

  System.Random rand = new Random();
        
int len = rand.Next(46);
        
char[] chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();//当然 也可以是中文  后面字符长度设置长一点
        System.Text.StringBuilder myStr 
= new System.Text.StringBuilder();
        
for (int iCount = 0; iCount < len; iCount++)
        
{
            myStr.Append(chars[rand.Next(chars.Length)]);
        }

        
string text = myStr.ToString();
        
// 保存验证码到 session 中以便其他模块使用
        this.Session["checkcode"= text;
        Size ImageSize 
= Size.Empty;
        Font myFont 
= new Font("MS Sans Serif"20);
        
// 计算验证码图片大小
        using (Bitmap bmp = new Bitmap(1010))
        
{
            
using (Graphics g = Graphics.FromImage(bmp))
            
{
                SizeF size 
= g.MeasureString(text, myFont, 10000);
                ImageSize.Width 
= (int)size.Width + 8;
                ImageSize.Height 
= (int)size.Height + 8;
            }

        }

        
// 创建验证码图片
        using (Bitmap bmp = new Bitmap(ImageSize.Width, ImageSize.Height))
        
{
            
// 绘制验证码文本
            using (Graphics g = Graphics.FromImage(bmp))
            
{
                g.Clear(Color.White);
                
using (StringFormat f = new StringFormat())
                
{
                    f.Alignment 
= StringAlignment.Near;
                    f.LineAlignment 
= StringAlignment.Center;
                    f.FormatFlags 
= StringFormatFlags.NoWrap;
                    g.DrawString(
                    text,
                    myFont,
                    Brushes.Black,
                    
new RectangleF(
                    
0,
                    
0,
                    ImageSize.Width,
                    ImageSize.Height),
                    f);
                }
//using
            }
//using
            
// 制造噪声 杂点面积占图片面积的 30%
            int num = ImageSize.Width * ImageSize.Height * 30 / 100;
            
for (int iCount = 0; iCount < num; iCount++)
            
{
                
// 在随机的位置使用随机的颜色设置图片的像素
                int x = rand.Next(ImageSize.Width);
                
int y = rand.Next(ImageSize.Height);
                
int r = rand.Next(255);
                
int g = rand.Next(255);
                
int b = rand.Next(255);
                Color c 
= Color.FromArgb(r, g, b);
                bmp.SetPixel(x, y, c);
            }
//for
            
// 输出图片
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            
this.Response.ContentType = "image/png";
            ms.WriteTo(
this.Response.OutputStream);
            ms.Close();
        }
//using
        myFont.Dispose();

 

 

 

 

根据这上面的实例能生成图片

但是web的Image控件不支持内存流

难道只有要新建一个页面来为它做这一点点小事吗?

如果是保存在磁盘上

速度又很慢 如果页面刷新过快的话

经常会生成烂图片和发生Io异常

番茄爱炒蛋的主页 番茄爱炒蛋 | 初学一级 | 园豆:0
提问于:2008-09-11 01:40
< >
分享
其他回答(4)
0

理论上图片是个文件,。。。。 你可以用.ashx 做输出,而后在做个用户控件封装起来

用户控件里面放置<img src=".../vimg.ashx" alt="点击刷新" onclick="....">

Hunts.C | 园豆:266 (菜鸟二级) | 2008-09-11 03:20
0

个人感觉最常用的还是单独放在一个页面中,像是别的比较大型的网站(QQ,baidu,taobao等),都有专门的验证码服务器的,而且也不是普通的客户端验证方式,而是采取客户短服务器的验证方式,

为一个网站单独设一个页面当作验证码,不为过....没关系....当然....一楼的说做成用户控件也可以...只是稍微麻烦一点,其实最终实现的效果都是一样的.....

Jared.Nie | 园豆:1940 (小虾三级) | 2008-09-11 07:55
0

一般实现的方式都是通过采用一个单独页面来实现。

http://www.cnblogs.com/perfectdesign/articles/1208750.html

http://www.cnblogs.com/index/articles/54692.html

eaglet | 园豆:17139 (专家六级) | 2008-09-11 08:06
0

需要透过现象看本质,在客户端显示的图片除背景图之外都是通过img标签实现的,img标签必须指定src属性,src属性必然是一个链接地址,需要在一个单独的aspx页面输出图片的流。

玉开 | 园豆:8822 (大侠五级) | 2008-09-11 08:40
0

是的,一般都是单独的aspx页面输出的,你就这样搞吧:)

zjy | 园豆:3194 (老鸟四级) | 2008-09-11 08:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册