ZCo*_*der 17 c# asp.net-mvc razor asp.net-mvc-3
我使用的是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( );
}
Run Code Online (Sandbox Code Playgroud)
调用viewdata的视图看起来像这样
<img src="@ViewData["picture"]." />
Run Code Online (Sandbox Code Playgroud)
Dar*_*rov 15
我建议您编写一个自定义操作结果,以避免使用完全无用且无聊的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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后只需定义一个将返回此自定义操作结果的控制器操作:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Image(string agha)
{
return new ImageResult(agha);
}
}
Run Code Online (Sandbox Code Playgroud)
从某些视图(~/Views/Home/Index.cshtml在我的示例中)中,您可以引用动态生成图像的操作:
<img src="@Url.Action("Image", new { agha = "foo bar" })" alt="" />
Run Code Online (Sandbox Code Playgroud)
如果您不想创建额外的操作来构建映像,另一种可能性是使用数据URI方案,它基本上允许您将图像作为Base64数据直接嵌入到HTML中.有一点需要注意的是,并非所有浏览器都支持它,除此之外,它还会使您的HTML页面更大.
Sea*_*ean 12
如果它不需要在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;
}
Run Code Online (Sandbox Code Playgroud)
在您的视图中,您需要调用Controller/Action:
<img src='@Url.Action("GenerateImage")' alt="" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21426 次 |
| 最近记录: |