articles

Home / DeveloperSection / Articles / WebImage Helper in ASP.NET MVC

WebImage Helper in ASP.NET MVC

Chris Anderson 28494 08-Oct-2011

ASP.NET MVC 3 provides various helper methods that are used for different purposes. WebImage helper method is one of them, which makes it very easy to display images in ASP.NET MVC. In this article, I will show you how you can use WebImage helper in ASP.NET MVC.

Open studio 2010 and create a new ASP.NET MVC 3 Web Application (Razor) project as seen below:

WebImage Helper in ASP.NET MVC

Choose Razor as the view engine and click OK.

WebImage Helper in ASP.NET MVC

Add a Controller and after adding a Controller adds the code as shown in the figure below. Also add a images folder in your which contained two images i.e. Desert.jpg and Coala.jpg.

using System.Web.Mvc;

using System.Web.Helpers;

namespace WebImageDemo.Controllers
{
     public class HomeController : Controller
    {
        public ActionResult Index()
         {
            return View();
         }
         public void GetImage1()
         {
            WebImage wbImage = new WebImage("~/images/Desert.jpg");
            wbImage.Resize(300, 300);
            wbImage.FileName = "Desert.jpg";
            wbImage.Write();
         }
         public void GetImage2()
         {
            WebImage wbImage = new WebImage("~/images/Koala.jpg");
            wbImage.Resize(300, 300);
            wbImage.FileName = "Koala.jpg";
            wbImage.Write();
         }
    }
}


 To get the image in a View use the following code:

<html>

<head>
     <title>Index</title>
</head>
<body>
     <div>
            <img src="@Url.Action("GetImage1")" alt="" />
            <img src="@Url.Action("GetImage2")" alt="" />
     </div>
</body>
</html>


The image is displayed on the page as follows:

WebImage Helper in ASP.NET MVC

Thank you for reading this article.


Updated 11-Jul-2020
hi I am software developer at mindstick software pvt. ltd.

Leave Comment

Comments

Liked By