c# - 从 Controller 发送图像以在 MVC3 中查看

标签 c# asp.net-mvc asp.net-mvc-3 razor

我正在使用 asp.net mvc3。我正在通过 system.drawing 使用文本制作位图。我要
将该图像从我的 Controller 发送到我在 VIEWDATA 中的 View ,但在我看来我无法正确解析 VIEWDATA。

这是 Controller 代码:

 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( );
        }

调用 View 数据的 View 看起来像这样

 <img src="@ViewData["picture"]." />

最佳答案

我建议您编写一个自定义操作结果,以避免用完全无用且乏味的 GDI+ 基础结构代码污染您的 Controller 操作:

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);
        }
    }
}

然后简单地定义一个将返回此自定义操作结果的 Controller 操作:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Image(string agha)
    {
        return new ImageResult(agha);
    }
}

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

<img src="@Url.Action("Image", new { agha = "foo bar" })" alt="" />

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

关于c# - 从 Controller 发送图像以在 MVC3 中查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8923817/

相关文章:

c# - Epplus 不读取 excel 文件

c# - 在 WinRT 上复制文件夹

c# - 如何使用NEST elasticsearch批量插入Json?

javascript - 使用删除按钮而不是 Html.ActionLink 来调用 Controller 的操作?

c# - 如何在 ASP.NET MVC 中使用 View 模型来显示计算字段?

asp.net-mvc-3 - 将整个页面从MVC3 Razor iFrame页面重定向到另一个URL

asp.net-mvc-3 - ASP.NET MVC 强类型 HTML 帮助程序

c# - preCondition ="managedHandler"如何用于模块?

c# - 如何为 ASP.NET MVC 5 创建依赖注入(inject)?

asp.net-mvc - 从子页面选择 MVC 母版页上的右侧菜单