Can an ASP.NET MVC controller return an Image?

前端 未结 19 1636
旧时难觅i
旧时难觅i 2020-11-22 02:10

Can I create a Controller that simply returns an image asset?

I would like to route this logic through a controller, whenever a URL such as the following is requeste

相关标签:
19条回答
  • 2020-11-22 02:35

    Look at ContentResult. This returns a string, but can be used to make your own BinaryResult-like class.

    0 讨论(0)
  • I see two options:

    1) Implement your own IViewEngine and set the ViewEngine property of the Controller you are using to your ImageViewEngine in your desired "image" method.

    2) Use a view :-). Just change the content type etc.

    0 讨论(0)
  • 2020-11-22 02:40

    You can write directly to the response but then it isn't testable. It is preferred to return an ActionResult that has deferred execution. Here is my resusable StreamResult:

    public class StreamResult : ViewResult
    {
        public Stream Stream { get; set; }
        public string ContentType { get; set; }
        public string ETag { get; set; }
    
        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.ContentType = ContentType;
            if (ETag != null) context.HttpContext.Response.AddHeader("ETag", ETag);
            const int size = 4096;
            byte[] bytes = new byte[size];
            int numBytes;
            while ((numBytes = Stream.Read(bytes, 0, size)) > 0)
                context.HttpContext.Response.OutputStream.Write(bytes, 0, numBytes);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 02:40

    Below code utilizes System.Drawing.Bitmap to load the image.

    using System.Drawing;
    using System.Drawing.Imaging;
    
    public IActionResult Get()
    {
        string filename = "Image/test.jpg";
        var bitmap = new Bitmap(filename);
    
        var ms = new System.IO.MemoryStream();
        bitmap.Save(ms, ImageFormat.Jpeg);
        ms.Position = 0;
        return new FileStreamResult(ms, "image/jpeg");
    }
    
    0 讨论(0)
  • 2020-11-22 02:41

    Yes you can return Image

    public ActionResult GetImage(string imageFileName)
    {
        var path = Path.Combine(Server.MapPath("/Images"), imageFileName + ".jpg"); 
        return base.File(path, "image/jpeg");
    }
    

    (Please don't forget to mark this as answer)

    0 讨论(0)
  • 2020-11-22 02:44

    This might be helpful if you'd like to modify the image before returning it:

    public ActionResult GetModifiedImage()
    {
        Image image = Image.FromFile(Path.Combine(Server.MapPath("/Content/images"), "image.png"));
    
        using (Graphics g = Graphics.FromImage(image))
        {
            // do something with the Graphics (eg. write "Hello World!")
            string text = "Hello World!";
    
            // Create font and brush.
            Font drawFont = new Font("Arial", 10);
            SolidBrush drawBrush = new SolidBrush(Color.Black);
    
            // Create point for upper-left corner of drawing.
            PointF stringPoint = new PointF(0, 0);
    
            g.DrawString(text, drawFont, drawBrush, stringPoint);
        }
    
        MemoryStream ms = new MemoryStream();
    
        image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
    
        return File(ms.ToArray(), "image/png");
    }
    
    0 讨论(0)
提交回复
热议问题