C# 从控制器发送图像以在 MVC3 中查看

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8923817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:12:02  来源:igfitidea点击:

Send Image From Controller To View In MVC3

c#asp.net-mvcasp.net-mvc-3razor

提问by ZCoder

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.

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

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

调用 viewdata 的视图如下所示

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

采纳答案by Darin Dimitrov

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

我建议您编写自定义操作结果,以避免使用完全无用且无聊的 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);
        }
    }
}

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.cshtmlin my example) you could reference the action that will dynamically generate the image for you:

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

<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 schemewhich 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.

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

回答by Manas

Have you tried ContentResult.

您是否尝试过 ContentResult.

You can try something like following ( not verified)

您可以尝试以下操作(未验证)

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

回答by Sean

If it doesn't need to be in the ViewData (not sure if you will be able to do it in the ViewData because each resource (image, flash, the page itself) has a ContentType that doesn't change in the current request.

如果不需要在 ViewData 中(不确定您是否能够在 ViewData 中执行此操作,因为每个资源(图像、Flash、页面本身)都有一个在当前请求中不会更改的 ContentType。

However, you can try this in your controller:

但是,您可以在控制器中尝试此操作:

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

In your view, you need to call the Controller/Action:

在您看来,您需要调用 Controller/Action:

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

回答by Sean

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

回答by Mohsin

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