发送图像控制器与查看MVC3 [英] Send Image From Controller To View In MVC3

查看:143
本文介绍了发送图像控制器与查看MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net MVC3。我正在使用的文本由System.Drawing中的位图。我想结果
这一形象从我的控制器发送到我的可视数据视图,但在我看来,我不能正确解析可视数据。

这是控制器code:

 公众的ActionResult关于(字符串阿加)
        {
            阿迦=asgdjhagsdjgajdga;
             颜色背景色= Color.White;
            字符串FONTNAME =宋体;
            INT字号= 25;
            INT高度= 50;
            INT宽度= 700;            位图位图=新位图(宽度,高度);
            显卡显卡= Graphics.FromImage(位图);
            色色= Color.Gray; ;
            字体的字体=新Font(字体名称,字号);            SolidBrush BrushBackColor =新SolidBrush(背景色);
            笔BorderPen =新笔(颜色);            矩形displayRectangle =新的Rectangle(新点(0,0),新尺寸(宽 - 1,高度 - 1));            graphics.FillRectangle(BrushBackColor,displayRectangle);
            graphics.DrawRectangle(BorderPen,displayRectangle);            graphics.DrawString(阿加,字体Brushes.Red,0,0);
            计算机[图片报] =位图;            返回查看();
        }

调用可视数据视图看起来像这样

 < IMG SRC =@计算机[图片。 />


解决方案

我建议你写一个自定义操作的结果,以避免完全无用的和枯燥的GDI +基础设施code污染你的控制器动作:

 公共类ImageResult:的ActionResult
{
    私人只读字符串_agha;
    公共ImageResult(字符串阿加)
    {
        _agha =阿迦;
    }    公共覆盖无效的ExecuteReuslt(ControllerContext上下文)
    {
        颜色背景色= Color.White;
        字符串FONTNAME =宋体;
        INT字号= 25;
        INT高度= 50;
        INT宽度= 700;        使用(位图位图=新位图(宽度,高度))
        使用(图形图像= Graphics.FromImage(位图))
        {
            色色= Color.Gray;
            字体的字体=新Font(字体名称,字号);            SolidBrush BrushBackColor =新SolidBrush(背景色);
            笔BorderPen =新笔(颜色);            矩形displayRectangle =新的Rectangle(新点(0,0),新尺寸(宽 - 1,高度 - 1));            graphics.FillRectangle(BrushBackColor,displayRectangle);
            graphics.DrawRectangle(BorderPen,displayRectangle);            graphics.DrawString(_agha,字体Brushes.Red,0,0);            context.HttpContext.Response.ContentType =图像/ JPG
            bitmap.Save(context.HttpContext.Response.OutputStream,ImageFormat.Jpeg);
        }
    }
}

,然后简单地定义一个控制器动作,将返回这个自定义操作的结果:

 公共类HomeController的:控制器
{
    公众的ActionResult指数()
    {
        返回查看();
    }    公众的ActionResult图像(字符串阿加)
    {
        返回新ImageResult(阿加);
    }
}

和一些视图中(〜/查看/主页/ Index.cshtml 在我的例子),你可以参考,这将动态地为您生成图像的操作:

 < IMG SRC =@ Url.Action(图像,新{阿迦=富巴})ALT =/>

如果你不希望创建一个额外的行动来构建映像另一种可能性是使用数据URI方案这基本上可以让你的形象为Base64数据直接嵌入到HTML。这一个需要注意的是,并不是所有的浏览器都支持它,并妮此外,它使你的HTML页面大得多。

I am using asp.net mvc3. I am making a bitmap using text by system.drawing. I want
to send that image from my controller to my view in VIEWDATA, but in my view I cannot parse VIEWDATA properly.

This is the controller code:

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

The view calling the viewdata looks like this

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

解决方案

I'd recommend you writing a custom action result to avoid polluting your controller action with completely useless and boring GDI+ infrastructure code:

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

and then simply define a controller action that will return this custom action result:

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

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

and from within some view (~/Views/Home/Index.cshtml in my example) you could reference the action that will dynamically generate the image for you:

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

Another possibility if you don't want to create an additional action to build the image is to use the Data URI scheme which basically allows you to embed the image as Base64 data directly into the HTML. One caveat with this is that not all browsers support it and ni addition to that it makes your HTML pages much larger.

这篇关于发送图像控制器与查看MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆