Send Image From Controller To View In MVC3

前端 未结 5 1377
说谎
说谎 2021-02-12 19:19

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 pars

5条回答
  •  萌比男神i
    2021-02-12 19:35

    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.

    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:

    
    

提交回复
热议问题