webapi返回图片,mvc返回图片

   public HttpResponseMessage Get(string imageName, int width, int height)
    {
        Image img = GetImage(imageName, width, height);
        MemoryStream ms = new MemoryStream();
        img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        result.Content = new ByteArrayContent(ms.ToArray());
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png");
        return result;
    }

 

 public FileResult UserPhoto()
        {
            byte[] image = (byte[])oc["photo" + EmployeeID];
            //get image from database   
            if (image == null)
            {
                CommonDAL cd = new CommonDAL();
                image = cd.GetUserPhoto();
                oc["photo" + EmployeeID] = image;
            }
            //return the image to View   
            if (image == null)
            {
                return new FilePathResult("~/Content/avatars/avatar2.png", "image/png");

            }
            else
            {
                return new FileContentResult(image, "image/png");
            }
            //or like below   

            //MemoryStream mem = new MemoryStream(image, 0, image.Length);   

            //return new FileStreamResult(mem, "image/jpg");

        }

 

你可能感兴趣的:(webapi返回图片,mvc返回图片)