Render image to the screen from MVC controller

前端 未结 3 1485
生来不讨喜
生来不讨喜 2020-12-16 07:32

I have images in the database and I want to return the image for viewing from an action. Here\'s my action.

public FileContentResult Index(ItemImageRequest          


        
3条回答
  •  别那么骄傲
    2020-12-16 08:04

    As requested, here is my solution.

    Here is the ImageResult class, copied and modified from John

    public class ImageResult : ActionResult
    {
        public ImageResult()
        {
        }
    
        public byte[] ImageData
        {
            get;
            set;
        }
    
        public MemoryStream ImageStream
        {
            get;
            set;
        }
    
        public string MimeType
        {
            get;
            set;
        }
    
        public HttpCacheability Cacheability
        {
            get;
            set;
        }
    
        public string ETag
        {
            get;
            set;
        }
    
        public DateTime? Expires
        {
            get;
            set;
        }
    
        public override void ExecuteResult(ControllerContext context)
        {
            if (this.ImageData == null && ImageStream == null)
            {
                throw new ArgumentNullException("ImageData or ImageStream");
            }
    
            if (string.IsNullOrEmpty(this.MimeType))
            {
                throw new ArgumentNullException("MimeType");
            }
    
            context.HttpContext.Response.ContentType = this.MimeType;
    
            if (!string.IsNullOrEmpty(this.ETag))
            {
                context.HttpContext.Response.Cache.SetETag(this.ETag);
            }
    
            if (this.Expires.HasValue)
            {
                context.HttpContext.Response.Cache.SetCacheability(this.Cacheability);
                context.HttpContext.Response.Cache.SetExpires(this.Expires.Value);
            }
    
            if (ImageStream != null)
            {
                ImageData = ImageStream.ToArray();
            } 
    
            context.HttpContext.Response.OutputStream.Write(this.ImageData, 0, this.ImageData.Length);
    
        }
    }
    

    Here is my action, modified for clarity

    public ActionResult Index(ItemImageRequest request)
    {
        var result = queueService.GetItemImage(request);
    
        if (result.TotalResults == 0)
            return new EmptyResult();
    
        ItemImage image = result.FirstResult;
    
        //image.Image is type byte[]
        MemoryStream tiffStream = new MemoryStream(image.Image);
    
        MemoryStream pngStream = new MemoryStream();
    
        System.Drawing.Bitmap.FromStream(tiffStream).Save(pngStream, System.Drawing.Imaging.ImageFormat.Png);
    
        return new ImageResult()
        {
            ImageStream = pngStream,
            MimeType = "image/png",
            Cacheability = HttpCacheability.NoCache
        };
    }
    

    Thanks to John and Will for helping me out with this.

提交回复
热议问题