ASP.NET 响应输出流中的 MVC 图像。不工作
ASP.NET MVC Image in response output stream. Not working
我想直接在视图中显示图像,所以我调用了一个操作方法并将图像写入输出流。
但是我收到错误提示 "The image cannot be displayed as it contains error"。我确信图像生成正确但不确定我的控制器代码有什么问题..有没有人遇到过这种情况?
提前致谢..
这是我的控制器操作
[ChildActionOnly]
public ActionResult GetCaptcha()
{
try
{
this.Session["CaptchaImageText"] = GenerateRandomCode();
this.Response.Clear();
this.Response.ContentType = "image/jpeg";
Bitmap img = new Bitmap(Server.MapPath("../Images/captcha.jpg"));
Graphics graphicImage = Graphics.FromImage(img);
graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
graphicImage.DrawString(this.Session["CaptchaImageText"].ToString(),
new Font("Arial", 12, FontStyle.Bold),
SystemBrushes.WindowText, new Point(100, 250));
graphicImage.DrawArc(new Pen(Color.Red, 3), 90, 235, 150, 50, 0, 360);
//Save the new image to the response output stream.
img.Save(this.Response.OutputStream, ImageFormat.Jpeg);
// Create a CAPTCHA image using the text stored in the Session object.
graphicImage.Dispose();
img.Dispose();
}
catch (Exception ex)
{
ex = null;
}
var v = new FileStreamResult(Response.OutputStream,"image/jpeg");
return v;
}
下面是我如何从视图中调用操作方法
<tr><td> <iframe width="200px" height="80px" frameborder="1" scrolling="no"> <img src="@Url.Action("GetCaptcha")" alt="SimpleChart" /> </iframe> </td></tr>
您需要 return 文件而不是 EmptyResult
。
return File(imageByteData, "image/png");
在视图侧:
<tr><td> <iframe> <img src='@Url.Action("GetCaptcha")'/> </iframe> </td></tr>
更新
不要写回复而是像下面那样做。
[ChildActionOnly]
public ActionResult GetCaptcha()
{
try
{
byte[] imageByteData = null
this.Session["CaptchaImageText"] = GenerateRandomCode();
this.Response.Clear();
this.Response.ContentType = "image/jpeg";
Bitmap img = new Bitmap(Server.MapPath("../Images/captcha.jpg"));
Graphics graphicImage = Graphics.FromImage(img);
graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
graphicImage.DrawString(this.Session["CaptchaImageText"].ToString(),
new Font("Arial", 12, FontStyle.Bold),
SystemBrushes.WindowText, new Point(100, 250));
graphicImage.DrawArc(new Pen(Color.Red, 3), 90, 235, 150, 50, 0, 360);
MemoryStream stream = new MemoryStream();
bitmap.Save(stream, ImageFormat.Jpeg);
imageByteData = stream.ToArray();
// Create a CAPTCHA image using the text stored in the Session object.
graphicImage.Dispose();
img.Dispose();
return File(imageByteData, "image/png");
}
catch (Exception ex)
{
ex = null;
}
}
我认为您使用的 FileStreamResult 有误。
将 img.Save(...) 更改为相同的内存流并在 FileStreamResult(...) return 该内存流中。
我想直接在视图中显示图像,所以我调用了一个操作方法并将图像写入输出流。
但是我收到错误提示 "The image cannot be displayed as it contains error"。我确信图像生成正确但不确定我的控制器代码有什么问题..有没有人遇到过这种情况?
提前致谢..
这是我的控制器操作
[ChildActionOnly]
public ActionResult GetCaptcha()
{
try
{
this.Session["CaptchaImageText"] = GenerateRandomCode();
this.Response.Clear();
this.Response.ContentType = "image/jpeg";
Bitmap img = new Bitmap(Server.MapPath("../Images/captcha.jpg"));
Graphics graphicImage = Graphics.FromImage(img);
graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
graphicImage.DrawString(this.Session["CaptchaImageText"].ToString(),
new Font("Arial", 12, FontStyle.Bold),
SystemBrushes.WindowText, new Point(100, 250));
graphicImage.DrawArc(new Pen(Color.Red, 3), 90, 235, 150, 50, 0, 360);
//Save the new image to the response output stream.
img.Save(this.Response.OutputStream, ImageFormat.Jpeg);
// Create a CAPTCHA image using the text stored in the Session object.
graphicImage.Dispose();
img.Dispose();
}
catch (Exception ex)
{
ex = null;
}
var v = new FileStreamResult(Response.OutputStream,"image/jpeg");
return v;
}
下面是我如何从视图中调用操作方法
<tr><td> <iframe width="200px" height="80px" frameborder="1" scrolling="no"> <img src="@Url.Action("GetCaptcha")" alt="SimpleChart" /> </iframe> </td></tr>
您需要 return 文件而不是 EmptyResult
。
return File(imageByteData, "image/png");
在视图侧:
<tr><td> <iframe> <img src='@Url.Action("GetCaptcha")'/> </iframe> </td></tr>
更新
不要写回复而是像下面那样做。
[ChildActionOnly]
public ActionResult GetCaptcha()
{
try
{
byte[] imageByteData = null
this.Session["CaptchaImageText"] = GenerateRandomCode();
this.Response.Clear();
this.Response.ContentType = "image/jpeg";
Bitmap img = new Bitmap(Server.MapPath("../Images/captcha.jpg"));
Graphics graphicImage = Graphics.FromImage(img);
graphicImage.SmoothingMode = SmoothingMode.AntiAlias;
graphicImage.DrawString(this.Session["CaptchaImageText"].ToString(),
new Font("Arial", 12, FontStyle.Bold),
SystemBrushes.WindowText, new Point(100, 250));
graphicImage.DrawArc(new Pen(Color.Red, 3), 90, 235, 150, 50, 0, 360);
MemoryStream stream = new MemoryStream();
bitmap.Save(stream, ImageFormat.Jpeg);
imageByteData = stream.ToArray();
// Create a CAPTCHA image using the text stored in the Session object.
graphicImage.Dispose();
img.Dispose();
return File(imageByteData, "image/png");
}
catch (Exception ex)
{
ex = null;
}
}
我认为您使用的 FileStreamResult 有误。
将 img.Save(...) 更改为相同的内存流并在 FileStreamResult(...) return 该内存流中。