Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Image From Controller To View In MVC3

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.

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

 <img src="@ViewData["picture"]." />
like image 856
ZCoder Avatar asked Jan 19 '12 09:01

ZCoder


People also ask

How can we transfer data from controller to view?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

Can we pass TempData from controller to view?

TempData is used to transfer data from view to controller, controller to view, or from one action method to another action method of the same or a different controller. TempData stores the data temporarily and automatically removes it after retrieving a value. TempData is a property in the ControllerBase class.

Can we pass Viewbag from view to controller?

Yes you cannot pass a Viewbag from view to controller. But you can pass them using TempData.

What is @model in Cshtml?

The @model directive allows access to the list of movies that the controller passed to the view by using a Model object that's strongly typed. For example, in the Index.cshtml view, the code loops through the movies with a foreach statement over the strongly typed Model object: CSHTML Copy.


2 Answers

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

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

<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 scheme which 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.

like image 104
Darin Dimitrov Avatar answered Oct 04 '22 13:10

Darin Dimitrov


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:

<img src='@Url.Action("GenerateImage")' alt="" />
like image 41
Sean Avatar answered Oct 04 '22 11:10

Sean