C# 从控制器向MVC3中的视图发送图像
我正在使用asp.net mvc3。我正在通过system.drawing使用文本制作位图。我想要C# 从控制器向MVC3中的视图发送图像,c#,asp.net-mvc,asp.net-mvc-3,razor,C#,Asp.net Mvc,Asp.net Mvc 3,Razor,我正在使用asp.net mvc3。我正在通过system.drawing使用文本制作位图。我想要 将该图像从控制器发送到VIEWDATA中的视图,但在我的视图中,我无法正确解析VIEWDATA 这是控制器代码: public ActionResult About( string agha) { agha = "asgdjhagsdjgajdga"; Color BackColor = Color.White;
将该图像从控制器发送到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的视图如下所示
<img src="@ViewData["picture"]." />
您是否尝试过ContentResult 您可以尝试以下方法(未验证)
我建议您编写一个自定义操作结果,以避免完全无用和无聊的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
,在我的示例中),您可以引用将为您动态生成图像的操作:
<img src="@Url.Action("Image", new { agha = "foo bar" })" alt="" />
<img src='@Url.Action("GenerateImage")' alt="" />
如果您不想创建其他操作来构建图像,另一种可能性是使用,它基本上允许您将图像作为Base64数据直接嵌入HTML。需要注意的是,并非所有浏览器都支持它,而且它会使您的HTML页面更大。如果它不需要位于ViewData中(不确定您是否能够在ViewData中执行此操作,因为每个资源(图像、flash、页面本身)都有一个在当前请求中不会更改的内容类型 但是,您可以在控制器中尝试以下操作:
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;
}
在您看来,您需要调用控制器/操作:
在视图中是这样的Page@Darin你能告诉我这不是一个MVC项目吗?如果没有context
,那么如何将它发送到Razor查看页面。请看看你用两个不同的主题在两天内连续保存了我的时间
<img src='@Url.Action("GenerateImage")' alt="" />
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;
}