从控制器发送图像到MVC3中查看

我使用的是asp.net mvc3。 我正在使用system.drawing的文本制作一个位图。 我想要
将该图像从我的控制器发送到VIEWDATA中的视图,但在我看来,我无法正确解析VIEWDATA。

这是控制器代码:

public ActionResult About( string agha) { agha = "asgdjhagsdjgajdga"; Color BackColor = Color.White; String FontName = "Times New Roman"; int FontSize = 25; int Height = 50; int Width = 700; Bitmap bitmap = new Bitmap(Width, Height); Graphics graphics = Graphics.FromImage(bitmap); Color color = Color.Gray; ; Font font = new Font(FontName, FontSize); SolidBrush BrushBackColor = new SolidBrush(BackColor); Pen BorderPen = new Pen(color); Rectangle displayRectangle = new Rectangle(new Point(0, 0), new Size(Width - 1, Height - 1)); graphics.FillRectangle(BrushBackColor, displayRectangle); graphics.DrawRectangle(BorderPen, displayRectangle); graphics.DrawString(agha,font,Brushes.Red, 0, 0); ViewData["picture"] = bitmap; return View( ); } 

调用viewdata的视图看起来像这样

   

我建议您编写一个自定义操作结果,以避免使用完全无用且无聊的GDI +基础结构代码来污染您的控制器操作:

 public class ImageResult : ActionResult { private readonly string _agha; public ImageResult(string agha) { _agha = agha; } public override void ExecuteResult(ControllerContext context) { Color BackColor = Color.White; String FontName = "Times New Roman"; int FontSize = 25; int Height = 50; int Width = 700; using (Bitmap bitmap = new Bitmap(Width, Height)) using (Graphics graphics = Graphics.FromImage(bitmap)) { Color color = Color.Gray; Font font = new Font(FontName, FontSize); SolidBrush BrushBackColor = new SolidBrush(BackColor); Pen BorderPen = new Pen(color); Rectangle displayRectangle = new Rectangle(new Point(0, 0), new Size(Width - 1, Height - 1)); graphics.FillRectangle(BrushBackColor, displayRectangle); graphics.DrawRectangle(BorderPen, displayRectangle); graphics.DrawString(_agha, font, Brushes.Red, 0, 0); context.HttpContext.Response.ContentType = "image/jpg"; bitmap.Save(context.HttpContext.Response.OutputStream, ImageFormat.Jpeg); } } } 

然后只需定义一个控制器操作,该操作将返回此自定义操作结果:

 public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult Image(string agha) { return new ImageResult(agha); } } 

从某些视图(在我的示例中为~/Views/Home/Index.cshtml )中,您可以引用动态生成图像的操作:

  

如果您不想创建额外的操作来构建映像,另一种可能性是使用数据URI方案 ,它基本上允许您将图像作为Base64数据直接嵌入到HTML中。 有一点需要注意的是,并非所有浏览器都支持它,除此之外,它使您的HTML页面更大。

如果它不需要在ViewData中(不确定您是否能够在ViewData中执行它,因为每个资源(图像,flash,页面本身)都具有在当前请求中不会更改的ContentType。

但是,您可以在控制器中尝试此操作:

 public ActionResult GenerateImage() { FileContentResult result; using(var memStream = new System.IO.MemoryStream()) { bitmap.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg); result = this.File(memStream.GetBuffer(), "image/jpeg"); } return result; } 

在您的视图中,您需要调用Controller / Action:

  

你尝试过ContentResult吗?

您可以尝试以下内容(未经validation)

 public ContentResult GetImage() { var content = new ContentResult(); content.Content = "Image as String"; content.ContentType = "image/jpg"; return content; } 
 public ContentResult GetImage() { var content = new ContentResult(); content.Content = "Image as String"; content.ContentType = "~image/jpg"; return content; } 
 public ActionResult GenerateImage() { FileContentResult result; using(var memStream = new System.IO.MemoryStream()) { bitmap.Save(memStream, System.Drawing.Imaging.ImageFormat.Jpeg); result = this.File(memStream.GetBuffer(), "image/jpeg"); } return result; }