首页 新闻 搜索 专区 学院

.net MVC验证码不能显示是怎么回事

0
悬赏园豆:10 [已解决问题] 解决于 2014-08-19 15:06

验证码不能正常显示出来,不知道是怎么回事,在线等大神帮忙,急啊!

问题补充:

[OutputCache(Duration = 0)]
public ActionResult VCode()
{
string code = ValidateCode.CreateRandomCode(4);
Session["vcode"] = code;
ValidateCode.CreateImage(code);
return View();
}后台代码

孤独狂少的主页 孤独狂少 | 初学一级 | 园豆:35
提问于:2014-08-19 11:13
< >
分享
最佳答案
0

还是调试一下吧!走一个大概的流程

收获园豆:10
秋壶冰月 | 大侠五级 |园豆:5694 | 2014-08-19 11:21

调试的时候也能获取到验证码的值,就是显示不出来

孤独狂少 | 园豆:35 (初学一级) | 2014-08-19 11:24

@孤独狂少: 你没给出后台action的生成验证码的代码,最后有没有指定images文件输出呀

  public ActionResult GetValidateCode()
        {
                     byte[] bytes = validateCode.CreateValidateGraphic(checkCode);
                     return File(bytes, @"image/jpeg"); //这里
        }
秋壶冰月 | 园豆:5694 (大侠五级) | 2014-08-19 11:30

@秋壶冰月: 

//graph.DrawString(randomcode,fontstyle,new SolidBrush(Color.Blue),2,2); //标准随机码
//生成图片
System.IO.MemoryStream ms = new System.IO.MemoryStream();
map.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/gif";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
graph.Dispose();
map.Dispose();类里面我写了哦!这里有问题吗

孤独狂少 | 园豆:35 (初学一级) | 2014-08-19 11:31

@秋壶冰月: 我用数组来接收他报错了无法将void转换为byte

孤独狂少 | 园豆:35 (初学一级) | 2014-08-19 11:46

@孤独狂少: mvc中 用将生成的图片保存到MemoryStream中,return ms.ToArray(),在用File() 就可以

秋壶冰月 | 园豆:5694 (大侠五级) | 2014-08-19 12:30

@秋壶冰月: 已经可以显示出来了,但是验证码重复了,密密麻麻的全部都是哦!最后这一个问题啦!大哥再帮帮忙呀!

孤独狂少 | 园豆:35 (初学一级) | 2014-08-19 13:03

@孤独狂少: 验证码重复,你调试在那里执行多次,你到现在都没有整个代码放出来,不知道在出错了!

秋壶冰月 | 园豆:5694 (大侠五级) | 2014-08-19 14:21

@秋壶冰月: 

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;
namespace Utility
{
/// <summary>
/// 完美随机验证码 0.10
/// Verion:0.10
/// Description:随机生成设定验证码,并随机旋转一定角度,字体颜色不同
/// </summary>
public class ValidateCode
{

/// <summary>
/// 生成随机码
/// </summary>
/// <param name="length">随机码个数www.52mvc.com</param>
/// <returns></returns>
public static string CreateRandomCode(int length)
{
int rand;
char code;
string randomcode = String.Empty;
//生成一定长度的验证码
System.Random random = new Random();
for (int i = 0; i < length; i++)
{
rand = random.Next();
if (rand % 3 == 0)
{
code = (char)('A' + (char)(rand % 26));
}
else
{
code = (char)('0' + (char)(rand % 10));
}
randomcode += code.ToString();
}
return randomcode;
}
/// <summary>
/// 创建随机码图片
/// </summary>
/// <param name="randomcode">随机码</param>
public static byte[] CreateImage(string randomcode)
{
int randAngle = 45; //随机转动角度
int mapwidth = (int)(randomcode.Length * 23);
Bitmap map = new Bitmap(mapwidth, 28);//创建图片背景
Graphics graph = Graphics.FromImage(map);
graph.Clear(Color.AliceBlue);//清除画面,填充背景
graph.DrawRectangle(new Pen(Color.Gray, 0), 0, 0, map.Width - 1, map.Height - 1);//画一个边框
//graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//模式
Random rand = new Random();
//背景噪点生成 www.2cto.com
Pen blackPen = new Pen(Color.LightGray, 0);
for (int i = 0; i < 50; i++)
{
int x = rand.Next(0, map.Width);
int y = rand.Next(0, map.Height);
graph.DrawRectangle(blackPen, x, y, 1, 1);
}
//验证码旋转,防止机器识别
char[] chars = randomcode.ToCharArray();//拆散字符串成单字符数组
//文字距中
StringFormat format = new StringFormat(StringFormatFlags.NoClip);
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
//定义颜色
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
//定义字体
string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
for (int i = 0; i < chars.Length; i++)
{
int cindex = rand.Next(7);
int findex = rand.Next(5);
Font f = new System.Drawing.Font(font[findex], 13, System.Drawing.FontStyle.Bold);//字体样式(参数2为字体大小)
Brush b = new System.Drawing.SolidBrush(c[cindex]);
Point dot = new Point(16, 16);
//graph.DrawString(dot.X.ToString(),fontstyle,new SolidBrush(Color.Black),10,150);//测试X坐标显示间距的
float angle = rand.Next(-randAngle, randAngle);//转动的度数
graph.TranslateTransform(dot.X, dot.Y);//移动光标到指定位置
graph.RotateTransform(angle);
graph.DrawString(chars.ToString(), f, b, 1, 1, format);
//graph.DrawString(chars.ToString(),fontstyle,new SolidBrush(Color.Blue),1,1,format);
graph.RotateTransform(-angle);//转回去
graph.TranslateTransform(2, -dot.Y);//移动光标到指定位置
}
//graph.DrawString(randomcode,fontstyle,new SolidBrush(Color.Blue),2,2); //标准随机码
//生成图片
System.IO.MemoryStream ms = new System.IO.MemoryStream();
map.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "image/jpeg";
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
graph.Dispose();
map.Dispose();
return ms.ToArray();
}

}
}

孤独狂少 | 园豆:35 (初学一级) | 2014-08-19 14:23

@孤独狂少: 生成的验证码,内容重复,我刚才调了一下,没解决,对要求不高的话,先用我以前收藏的吧

 public static byte[] CreateValidateGraphic(string validateCode)
        {
            Bitmap image = new Bitmap((int)Math.Ceiling(validateCode.Length * 12.0), 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 Font("Arial", 12, (FontStyle.Bold | FontStyle.Italic));
                LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
                                                                    Color.Blue, Color.DarkRed, 1.2f, true);
                g.DrawString(validateCode, font, brush, 3, 2);
                //画图片的前景干扰点
                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), 0, 0, image.Width - 1, image.Height - 1);
                //保存图片数据
                MemoryStream stream = new MemoryStream();
                image.Save(stream, ImageFormat.Jpeg);
                //输出图片流
                return stream.ToArray();
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }
秋壶冰月 | 园豆:5694 (大侠五级) | 2014-08-19 14:53

@秋壶冰月: 好的实在太谢谢了

孤独狂少 | 园豆:35 (初学一级) | 2014-08-19 14:56
其他回答(3)
0

呵呵,麻烦这样的提问者自己看看自己的问题,先。

你把事情说明白了?谁能帮你解决?

Alex_QY1987 | 园豆:1888 (小虾三级) | 2014-08-19 11:17
0

你用的是什么验证码组件?

dudu | 园豆:35899 (高人七级) | 2014-08-19 11:19
0
老余的博客 | 园豆:210 (菜鸟二级) | 2020-07-20 16:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册