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

时间:2012-01-19 09:39:35

标签: 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;
            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"]." />

5 个答案:

答案 0 :(得分: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);
        }
    }
}

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

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="" />

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

答案 1 :(得分: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;
}

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

<img src='@Url.Action("GenerateImage")' alt="" />

答案 2 :(得分:2)

您是否尝试过ContentResult。

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

public ContentResult GetImage()
        {
            var content = new ContentResult();
            content.Content = "Image as String";
            content.ContentType = "image/jpg";
            return content;
        }

答案 3 :(得分:1)

public ContentResult GetImage()
        {
            var content = new ContentResult();
            content.Content = "Image as String";
            content.ContentType = "~image/jpg";
            return content;
        }

答案 4 :(得分:1)

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