我想使用动作方法显示图像。这是我在帐户控制器中的操作方法:
public ActionResult CaptchaImage(string prefix, bool noisy = true)
{
var rand = new Random((int)DateTime.Now.Ticks);
//generate new question
int a = rand.Next(10, 99);
int b = rand.Next(0, 9);
var captcha = string.Format("{0} + {1} = ?", a, b);
//store answer
Session["Captcha" + prefix] = a + b;
//image stream
FileContentResult img = null;
using (var mem = new System.IO.MemoryStream())
using (var bmp = new System.Drawing.Bitmap(130, 30))
using (var gfx = System.Drawing.Graphics.FromImage((System.Drawing.Image)bmp))
{
gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
gfx.FillRectangle(System.Drawing.Brushes.White, new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height));
//add noise
if (noisy)
{
int i, r, x, y;
var pen = new System.Drawing.Pen(System.Drawing.Color.Yellow);
for (i = 1; i < 10; i++)
{
pen.Color = System.Drawing.Color.FromArgb(
(rand.Next(0, 255)),
(rand.Next(0, 255)),
(rand.Next(0, 255)));
r = rand.Next(0, (130 / 3));
x = rand.Next(0, 130);
y = rand.Next(0, 30);
gfx.DrawEllipse(pen, (float)x, (float)y, (float)r, (float)r);
}
}
//add question
gfx.DrawString(captcha, new System.Drawing.Font("Tahoma", 15), System.Drawing.Brushes.Gray, 2, 3);
//render as Jpeg
bmp.Save(mem, System.Drawing.Imaging.ImageFormat.Jpeg);
img = this.File(mem.GetBuffer(), "image/Jpeg");
}
return img;
}
这是我的HTML:
<img alt="Captcha" src="@Url.Action("CaptchaImage","Account")" />
问题是没有调用CaptchaImage并且没有显示图像。我该如何解决这个问题?
我使用以下代码重新加载验证码图像,但它不会更改图像。
$(document).ready(function () {
$("#imgCaptcha").click(function () {
$.ajax({
url: "/Account/CaptchaImage",
success: function (result) {
$(this).src = result;
},
});
});
});
答案 0 :(得分:0)
在你的图像中,src应该是图像路径。因此,您应该考虑先执行此逻辑并将路径传递给您的img源。