ASP.Net MVC 4 Project Of a Photo Gallery

The project shown in this article shows user login, user registration and how to upload and display images in ASP.Net MVC in C#.

First we will do a user login and then maintain a photo gallery. That is, the user uploads multiple images at once and those images will be added to a photo gallery. We can divide the user photo gallery functionality into the following 3 processes:

  1. User Login (if one does not exist then register).

  2. User uploads images to create a new photo gallery. Store the gallery details into the database.

  3. Display uploaded images.

Now first create the user table in the database.

  1. CREATE TABLE tbl_User(UserId int IDENTITY(1,1) NOT NULL,UserName varchar(100) NOT NULL, Email varchar(100) NOT NULL,Password varchar(50) NOT NULL)  
  2.   
  3. CREATE TABLE tbl_Gallery(ImageId int IDENTITY(1,1) NOT NULL, ImageName varchar(50) NOT NULL, Photo image NOT NULL, Fk_UserId int NOT NULL)  
After creating table create the application. New -> website.

website

Provide the name “PhotoGalleryofUserInMVC”.

web application

basic

Now create a UserPhotoGalleryMaster Class by right-clicking on the Models folder then select Add a New Item.
  1. public class UserPhotoGalleryMaster  
  2. {  
  3.       
  4.     [Required]  
  5.     public string UserName { getset; }  
  6.   
  7.     [Required]  
  8.     [Display(Name = "Email")]  
  9.     public string Email { getset; }  
  10.   
  11.     [Required]  
  12.     [DataType(DataType.Password)]  
  13.     [Display(Name = "Password")]  
  14.     public string Password { getset; }  
  15.   
  16.   
  17.     public int ImageId { getset; }  
  18.     public string ImageName { getset; }  
  19.     public byte[] Image { getset; }  
  20.     public int Fk_UserId { getset; }  
  21. }  
Now create a UserPhotoGalleryService class by right-clicking on the Models folder as Add a New Item for doing the ADO.Net database operation of user login, registration and photo gallery.
  1. namespace PhotoGalleryofUserInMVC.Models  
  2. {  
  3.    public class UserPhotoGalleryService  
  4.    {  
  5.    }  
  6. }  
Now create a controller user by right-clicking on the controller folder and select add as controller.

controller

Now right-click on the ActionResult method in the controller, to create a View for User login. Create a view as a strongly typed view and choose the UserPhotoGalleryMaster model class from the list. It is shown as in the following figure. If the UserPhotoGalleryMaster model class is not in the list, then you should build the application. Then do the preceding process.

view name

Now this is the User Login view.
  1. @model PhotoGalleryofUserInMVC.Models.UserPhotoGalleryMaster  
  2.   
  3. @{  
  4.     ViewBag.Title = "UserLogin";  
  5. }  
  6.   
  7. <html>  
  8. <head id="Head1" runat="server">  
  9.     <title>Userlogin</title>  
  10.       
  11.      
  12.     <link href="~/Content/Site.css" rel="stylesheet" />  
  13.      
  14.     <script type="text/javascript">  
  15.          
  16.         function ValidateForm() {  
  17.             var txt1 = document.getElementById('Email');  
  18.             if (txt1.value == '') {  
  19.                 alert("Enter Email !")  
  20.                 document.getElementById("Email").focus();  
  21.                 return false;  
  22.             }  
  23.   
  24.             var txtPwd = document.getElementById('Password');  
  25.             if (txtPwd.value == '') {  
  26.                 alert("Enter Password !")  
  27.                 document.getElementById("Password").focus();  
  28.                 return false;  
  29.             }  
  30.         }  
  31.   
  32.   
  33.     </script>  
  34. </head>  
  35. <body>  
  36.    @using (Html.BeginForm("UserLogin""User", FormMethod.Post))  
  37.    {  
  38.        @Html.AntiForgeryToken()  
  39.        @Html.ValidationSummary(true)  
  40.         <div id="login">  
  41.             <header>  
  42.                 
  43.                 <!--/logo-->  
  44.                 <!--Div to display time.START-->  
  45.                 <div id="time2"></div>  
  46.                 <!--Div to display time.END-->  
  47.                 <div class="clearfix"></div>  
  48.                 <div style="text-align: center">  
  49.                 <label id="lblMsg" class="errMsg" ></label>  
  50.                   </div>  
  51.                 <div class="clearfix"></div>  
  52.             </header>  
  53.             <div id="login-container">  
  54.                 <div class="login-left">  
  55.                     <h1>Login</h1>  
  56.   
  57.                     @Html.LabelFor(l => l.Email) :   
  58.                     @Html.TextBoxFor(l => l.Email)  
  59.                     @Html.ValidationMessageFor(l => l.Email)  
  60.                                        
  61.                     <div class="clearfix"></div>  
  62.   
  63.                     @Html.LabelFor(l => l.Password)  
  64.                    @Html.TextBoxFor(l => l.Password)  
  65.                      @Html.ValidationMessageFor(l => l.Password)  
  66.                     <div class="clearfix"></div>  
  67.                     <div class="row1-button7">  
  68.                         <input type="submit" class="login-btn" value="Submit" onclick="return ValidateForm()"  />  
  69.                           
  70.                     </div>  
  71.                     <div class="row1-button9">  
  72.                     @Html.ActionLink("Register User""RegisterUser""User")  
  73.                     </div>  
  74.                 </div>                 
  75.                 <div class="clearfix"></div>  
  76.             </div>  
  77.         </div>  
  78.    }  
  79. </body>  
  80. </html>  
Use the same process for the registration page. First write the following action result.
  1. public ActionResult RegisterUser()  
  2. {  
  3.    return View();  
  4. }  
Now right-click on the ActionResult method in the controller, to create a View for User Registration.

Now this is User Registration view.
  1. @model PhotoGalleryofUserInMVC.Models.UserPhotoGalleryMaster  
  2.   
  3. @{  
  4.     ViewBag.Title = "RegisterUser";  
  5. }  
  6.   
  7. <!DOCTYPE html>  
  8.   
  9. <html>  
  10. <head id="Head1" runat="server">  
  11.     <title>Regitration</title>  
  12.       
  13.      <link href="~/Content/Site.css" rel="stylesheet" />  
  14.       
  15.     <script type="text/javascript">  
  16.   
  17.         function ValidateForm() {  
  18.             var txt1 = document.getElementById('Email');  
  19.             if (txt1.value == '') {  
  20.                 alert("Enter Email !")  
  21.                 document.getElementById("Email").focus();  
  22.                 return false;  
  23.             }  
  24.   
  25.             var txtPwd = document.getElementById('Password');  
  26.             if (txtPwd.value == '') {  
  27.                 alert("Enter Password !")  
  28.                 document.getElementById("Password").focus();  
  29.                 return false;  
  30.             }  
  31.   
  32.             var txtUser = document.getElementById('UserName');  
  33.             if (txtUser.value == '') {  
  34.                 alert("Enter User Name !")  
  35.                 document.getElementById("UserName").focus();  
  36.                 return false;  
  37.             }  
  38.   
  39.             alert("Register sucessfully");  
  40.         }  
  41.   
  42.   
  43.     </script>  
  44.     </head>  
  45. <body>  
  46.    @using (Html.BeginForm("RegisterUser""User", FormMethod.Post ))  
  47.    {  
  48.         <div id="login">  
  49.             <header>  
  50.                 
  51.                 <!--/logo-->  
  52.                 <!--Div to display time.START-->  
  53.                 <div id="time2"></div>  
  54.                 <!--Div to display time.END-->  
  55.                 <div class="clearfix"></div>  
  56.                 <div style="text-align: center">  
  57.                 <label id="lblMsg" class="errMsg" ></label>  
  58.                   </div>  
  59.                 <div class="clearfix"></div>  
  60.             </header>  
  61.             <div id="login-container">  
  62.                 <div class="login-left">  
  63.                     <h1>Register</h1>  
  64.                     @Html.LabelFor(l => l.UserName) :   
  65.                     @Html.TextBoxFor(l => l.UserName)  
  66.                     @Html.ValidationMessageFor(l => l.UserName)  
  67.                                        
  68.                      @Html.LabelFor(l => l.Email) :   
  69.                     @Html.TextBoxFor(l => l.Email)  
  70.                     @Html.ValidationMessageFor(l => l.Email)  
  71.                                        
  72.                     <div class="clearfix"></div>  
  73.   
  74.                     @Html.LabelFor(l => l.Password)  
  75.                    @Html.TextBoxFor(l => l.Password)  
  76.                      @Html.ValidationMessageFor(l => l.Password)  
  77.                     <div class="clearfix"></div>  
  78.                     <div class="row1-button7">  
  79.                         <input type="submit" class="login-btn" value="Register" onclick="return ValidateForm()"  />  
  80.                           
  81.                     </div>  
  82. <div class="row1-button9">  
  83.                     @Html.ActionLink("Register User""RegisterUser""User")  
  84.                     </div>  
  85.                      @ViewBag.Msg               
  86.    </div>                 
  87.                 <div class="clearfix"></div>  
  88.             </div>  
  89.         </div>  
  90.    }  
  91. </body>  
  92. </html>  
Our Login and Registration should be such as shown below.

Login

Registration

Now create a controller ManagePhotoGallery by right-clicking on the controller folder and select Add as controller.

Than follow the same process for creating view as above.

The ManagePhotoGallery view is as in the following:
  1. @model IList<PhotoGalleryofUserInMVC.Models.UserPhotoGalleryMaster>  
  2. @{  
  3.     ViewBag.Title = "PhotoGallery";  
  4.       
  5. }  
  6.   
  7.  <link href="~/Content/Site.css" rel="stylesheet" />  
  8.   
  9.    
  10.   
  11. <script type="text/javascript">  
  12.     function ValidateForm() {  
  13.         var txt1 = document.getElementById('ImageName');  
  14.         if (txt1.value == '') {  
  15.             alert("Enter Image Name !")  
  16.             document.getElementById("ImageName").focus();  
  17.             return false;  
  18.         }  
  19.   
  20.         var ImageFile = document.getElementById('ImageFile');  
  21.   
  22.         if (ImageFile.value == '') {  
  23.             alert('Please upload the attachment');  
  24.             ImageFile.focus();  
  25.             return false;  
  26.         }  
  27.   
  28.         var fname = uploadfile.value;  
  29.         var ext = fname.split(".");  
  30.         var x = ext.length;  
  31.         var extstr = ext[x - 1].toLowerCase();  
  32.         if (extstr == 'jpg' || extstr == 'jpeg' || extstr == 'png' || extstr == 'gif') { }  
  33.         else {  
  34.             alert("Please upload valid image");  
  35.             uploadfile.focus();  
  36.             return false;  
  37.         }  
  38.   
  39.         alert("Image uploaded sucessfully");  
  40.     }  
  41. </script>  
  42. <style>  
  43.     .imageclass  
  44.     {  
  45.         max-width: 100%;  
  46.         -webkit-border-radius: 5px;  
  47.         -moz-border-radius: 5px;  
  48.         border-radius: 5px;  
  49.     }  
  50.       
  51.     .thumbmain  
  52.     {  
  53.         margin: 3px;  
  54.         border: 1px solid #A0ACC0;  
  55.         height: auto;  
  56.         float: left;  
  57.         text-align: center;  
  58.     }  
  59.       
  60.     .thumbimg  
  61.     {  
  62.         display: inline;  
  63.         margin: 5px;  
  64.         border: 1px solid #A0ACC0;  
  65.     }  
  66. </style>  
  67.   
  68. <div class="container">  
  69.   
  70.   
  71.   
  72. <div class="main">  
  73. <div style="float:right;">  
  74.     @if (Session["Userid"] != null)  
  75.             {  
  76.                 <text>  
  77.                     Welcome @Session["UserName"].ToString()   
  78.                 </text>  
  79.             }  
  80.   
  81.     @Html.ActionLink("Log Out""UserLogin""User")  
  82. </div>  
  83.   
  84. <h2>  
  85.     Manage Photo Gallery</h2>  
  86. <div style="width: 600px;">  
  87.     @using (Html.BeginForm("Index""ManagePhotoGallery", FormMethod.Post, new { enctype = "multipart/form-data" }))  
  88.     {  
  89.         @Html.AntiForgeryToken()  
  90.         @Html.ValidationSummary(true)  
  91.         <fieldset >  
  92.             <legend>Create Albums</legend>  
  93.             <table>  
  94.                 <tr>  
  95.                     <td>  
  96.                         Album Name :  
  97.                     </td>  
  98.                     <td>@Html.TextBox("ImageName"""new { style = "width:200px" })  
  99.                     </td>  
  100.                 </tr>  
  101.                 <tr>  
  102.                     <td>  
  103.                         Image :  
  104.                     </td>  
  105.                     <td>  
  106.                         <input type="file" name="ImageFile" id="ImageFile" />  
  107.                     </td>  
  108.                 </tr>  
  109.                 <tr>  
  110.                     <td colspan="2">  
  111.                         <input type="submit" name="submit" value="Upload" onclick="return ValidateForm()" />  
  112.                     </td>  
  113.                 </tr>  
  114.             </table>  
  115.         </fieldset>  
  116.     }  
  117. </div>  
  118.     </div>  
  119. <br />  
  120.   
  121. <div class="main2">  
  122.   
  123.   
  124. <h2>  
  125.     Photo Gallery</h2>  
  126. <table cellpadding="5" cellspacing="5" width="440">  
  127.     @{  
  128.         //repeatdirection = Horizontal, RepeatColumns = 4  
  129.         const int NumberOfColumns = 4;  
  130.         int skip = 0;  
  131.         var items = Model.Skip(skip).Take(NumberOfColumns);  
  132.         while (items.Count() > 0)  
  133.         {  
  134.         <tr>  
  135.             @foreach (var item in items)  
  136.             {  
  137.                 <td width="450">  
  138.                     <table class="thumbmain">  
  139.                         <tr>  
  140.                             <td>  
  141.                                 <img src="/ManagePhotoGallery/RetrieveImage/@item.ImageId" width="120" height="120" class="imageclass" />  
  142.                             </td>  
  143.                         </tr>  
  144.                         <tr>  
  145.                             <td style="text-align: center; font-weight: bold; color: Teal">@item.ImageName  
  146.                             </td>  
  147.                         </tr>  
  148.                     </table>  
  149.                 </td>  
  150.             }  
  151.         </tr>  
  152.             skip += NumberOfColumns;  
  153.             items = Model.Skip(skip).Take(NumberOfColumns);  
  154.         }  
  155.     }  
  156. </table>  
  157. </div>  
  158.     </div>  
Now add some lines of code in UserPhotoGalleryService for performing user login, Registration and manage photo gallery.
  1. namespace PhotoGalleryofUserInMVC.Models  
  2. {  
  3.     public class UserPhotoGalleryService  
  4.     {  
  5.         //For connect to database  
  6.         SqlConnection scon = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnection"].ToString());  
  7.   
  8.         //method For upload the image in the database.  
  9.         public int UploadAlbums(UserPhotoGalleryMaster objUserPhotoGalleryMaster)  
  10.         {  
  11.             using (SqlCommand scmd = new SqlCommand())  
  12.             {  
  13.                 scmd.Connection = scon;  
  14.                 scmd.CommandType = CommandType.Text;  
  15.                 scmd.CommandText = "INSERT INTO tbl_Gallery(ImageName,Photo, Fk_Userid) VALUES(@ImageName,@Photo, @Fk_Userid)";  
  16.                 scmd.Parameters.AddWithValue("@ImageName", objUserPhotoGalleryMaster.ImageName);  
  17.                 scmd.Parameters.AddWithValue("@Photo", objUserPhotoGalleryMaster.Image);  
  18.                 scmd.Parameters.AddWithValue("@Fk_Userid", objUserPhotoGalleryMaster.Fk_UserId);  
  19.                 scon.Open();  
  20.                 int status = scmd.ExecuteNonQuery();  
  21.                 scon.Close();  
  22.                 return status;  
  23.             }  
  24.         }  
  25.   
  26.         //method for the user registration  
  27.         public int RegisterUser(UserPhotoGalleryMaster objUserPhotoGalleryMaster)  
  28.         {  
  29.             using (SqlCommand scmd = new SqlCommand())  
  30.             {  
  31.                 scmd.Connection = scon;  
  32.                 scmd.CommandType = CommandType.Text;  
  33.                 scmd.CommandText = "INSERT INTO tbl_User(UserName,Email, Password) VALUES(@UserName,@Email, @Password)";  
  34.                 scmd.Parameters.AddWithValue("@UserName", objUserPhotoGalleryMaster.UserName);  
  35.                 scmd.Parameters.AddWithValue("@Email", objUserPhotoGalleryMaster.Email);  
  36.                 scmd.Parameters.AddWithValue("@Password", objUserPhotoGalleryMaster.Password);  
  37.                 scon.Open();  
  38.                 int status = scmd.ExecuteNonQuery();  
  39.                 scon.Close();  
  40.                 return status;  
  41.             }  
  42.         }  
  43.   
  44.         //method for user login  
  45.         public int LoginUser(UserPhotoGalleryMaster objUserPhotoGalleryMaster,out int userid, out string username)  
  46.         {  
  47.             using (SqlCommand scmd = new SqlCommand())  
  48.             {  
  49.                 int ret = 0;  
  50.                 userid = 0;  
  51.                 username = "";  
  52.                 scmd.Connection = scon;  
  53.                 scmd.CommandType = CommandType.Text;  
  54.                 scmd.CommandText = "Select Userid, UserName from tbl_User where Email=@Email and Password=@Password";  
  55.                 scmd.Parameters.AddWithValue("@Email", objUserPhotoGalleryMaster.Email);  
  56.                 scmd.Parameters.AddWithValue("@Password", objUserPhotoGalleryMaster.Password);  
  57.                 scon.Open();  
  58.                 SqlDataReader sdr = scmd.ExecuteReader();  
  59.                 while (sdr.Read())  
  60.                 {  
  61.                     userid =Convert.ToInt32(sdr.GetValue(0));  
  62.                     username = sdr.GetString(1);  
  63.                     ret = 1;  
  64.                 }  
  65.                 if (sdr != null)  
  66.                 {  
  67.                     sdr.Dispose();  
  68.                     sdr.Close();  
  69.                 }  
  70.   
  71.                 return ret;  
  72.             }  
  73.         }  
  74.   
  75.         //method for the get photo album from the database using userid  
  76.         public IList<UserPhotoGalleryMaster> GetAlbums(int userid)  
  77.         {  
  78.             List<UserPhotoGalleryMaster> objUserPhotoGalleryMaster = new List<UserPhotoGalleryMaster>();  
  79.             using (SqlCommand scmd = new SqlCommand())  
  80.             {  
  81.                 scmd.Connection = scon;  
  82.                 scmd.CommandType = CommandType.Text;  
  83.                 scmd.CommandText = "SELECT * FROM tbl_Gallery where Fk_Userid=@userid";  
  84.                 scmd.Parameters.AddWithValue("@userid", userid);  
  85.                 scon.Open();  
  86.                 SqlDataReader sdr = scmd.ExecuteReader();  
  87.                 while (sdr.Read())  
  88.                 {  
  89.                     UserPhotoGalleryMaster objAlbumMaster = new UserPhotoGalleryMaster();  
  90.                     objAlbumMaster.ImageId = Convert.ToInt32(sdr["ImageId"]);  
  91.                     objAlbumMaster.ImageName = sdr["ImageName"].ToString();  
  92.                     objAlbumMaster.Image = (byte[])sdr["Photo"];  
  93.                     objUserPhotoGalleryMaster.Add(objAlbumMaster);  
  94.                 }  
  95.   
  96.                 if (sdr != null)  
  97.                 {  
  98.                     sdr.Dispose();  
  99.                     sdr.Close();  
  100.                 }  
  101.                 scon.Close();  
  102.                 return objUserPhotoGalleryMaster.ToList(); ;  
  103.             }  
  104.         }  
  105.   
  106.         // method for the photo using image id  
  107.         public byte[] GetImageFromDataBase(int id)  
  108.         {  
  109.             using (SqlCommand scmd = new SqlCommand())  
  110.             {  
  111.                 scmd.Connection = scon;  
  112.                 scmd.CommandType = CommandType.Text;  
  113.                 scmd.CommandText = "SELECT Photo FROM tbl_Gallery where ImageId=@ImageId";  
  114.                 scmd.Parameters.AddWithValue("@ImageId", id);  
  115.                 scon.Open();  
  116.                 SqlDataReader sdr = scmd.ExecuteReader();  
  117.                 UserPhotoGalleryMaster objUserPhotoGalleryMaster = new UserPhotoGalleryMaster();  
  118.                 while (sdr.Read())  
  119.                 {  
  120.                     objUserPhotoGalleryMaster.Image = (byte[])sdr["Photo"];  
  121.                 }  
  122.                 return objUserPhotoGalleryMaster.Image;  
  123.             }  
  124.         }  
  125.   
  126.     }  
  127. }  
User Controller
  1. public class UserController : Controller  
  2. {  
  3.   
  4.     UserPhotoGalleryService objUserPhotoGalleryService = new UserPhotoGalleryService();  
  5.     //  
  6.     // GET: /User/  
  7.   
  8.     public ActionResult Index()  
  9.     {  
  10.         return View();  
  11.     }  
  12.   
  13.     public ActionResult UserLogin()  
  14.     {  
  15.           
  16.         return View();  
  17.     }  
  18.   
  19.     public ActionResult RegisterUser()  
  20.     {  
  21.         return View();  
  22.     }  
  23.   
  24.     //To post form data, we need to add HttpPost action method for Index controller.   
  25.     [HttpPost]  
  26.     public ActionResult RegisterUser(FormCollection collection)  
  27.     {  
  28.         UserPhotoGalleryMaster objUserPhotoGalleryMaster = new UserPhotoGalleryMaster();  
  29.         objUserPhotoGalleryMaster.UserName = collection["Username"].ToString();  
  30.         objUserPhotoGalleryMaster.Email = collection["Email"].ToString();  
  31.         objUserPhotoGalleryMaster.Password = collection["Password"].ToString();  
  32.           
  33.         objUserPhotoGalleryService.RegisterUser(objUserPhotoGalleryMaster);  
  34.         return View();  
  35.     }  
  36.   
  37.     //To post form data, we need to add HttpPost action method for Index controller.   
  38.     [HttpPost]  
  39.     public ActionResult UserLogin(FormCollection collection)  
  40.     {  
  41.         if (ModelState.IsValid)  
  42.         {  
  43.             int ret = 0;  
  44.             int userid = 0;  
  45.             string username = "";  
  46.             UserPhotoGalleryMaster objUserPhotoGalleryMaster = new UserPhotoGalleryMaster();                  
  47.             objUserPhotoGalleryMaster.Email = collection["Email"].ToString();  
  48.             objUserPhotoGalleryMaster.Password = collection["Password"].ToString();  
  49.             ret = objUserPhotoGalleryService.LoginUser(objUserPhotoGalleryMaster, out userid, out username);  
  50.   
  51.   
  52.             if (ret > 0)  
  53.             {  
  54.                 Session["Userid"]=userid;  
  55.                 Session["UserName"] = username;  
  56.                 return RedirectToAction("Index""ManagePhotoGallery");  
  57.             }  
  58.             else  
  59.             {  
  60.                  ViewBag.Msg = "Invalid login Details";  
  61.             }  
  62.         }  
  63.         return View();  
  64.     }  
  65.   
  66.   
  67. }  
ManagePhotoGallery Controller
  1. public class ManagePhotoGalleryController : Controller  
  2. {  
  3.     UserPhotoGalleryService objUserPhotoGalleryService = new UserPhotoGalleryService();  
  4.   
  5.     //To get data, we need to add HttpGet action method for Index controller.   
  6.     [HttpGet]  
  7.     [OutputCache(Duration = 60, VaryByParam = "None")]  
  8.     public ActionResult Index()  
  9.     {  
  10.         if (Session["Userid"] != null)  
  11.         {  
  12.             string userid = Session["Userid"].ToString();  
  13.   
  14.             ViewBag.total = objUserPhotoGalleryService.GetAlbums(Convert.ToInt32(userid)).ToList().Count;  
  15.             return View(objUserPhotoGalleryService.GetAlbums(Convert.ToInt32(userid)).ToList());  
  16.         }  
  17.         else  
  18.         {  
  19.             return RedirectToAction("UserLogin""User");  
  20.         }  
  21.     }  
  22.   
  23.     //To post form data, we need to add HttpPost action method for Index controller.   
  24.     [HttpPost]  
  25.     public ActionResult Index(FormCollection collection)  
  26.     {  
  27.         string userid = Session["Userid"].ToString();  
  28.         HttpPostedFileBase file = Request.Files["ImageData"];  
  29.         UserPhotoGalleryMaster objUserPhotoGalleryMaster = new UserPhotoGalleryMaster();  
  30.         objUserPhotoGalleryMaster.ImageName = collection["ImageName"].ToString();  
  31.         objUserPhotoGalleryMaster.Fk_UserId = Convert.ToInt32(userid);  
  32.         objUserPhotoGalleryMaster.Image = ConvertToBytes(file);  
  33.         objUserPhotoGalleryService.UploadAlbums(objUserPhotoGalleryMaster);  
  34.         return View(objUserPhotoGalleryService.GetAlbums(Convert.ToInt32(userid)).ToList());  
  35.     }  
  36.   
  37.     //methods to convert byte array into image format  
  38.     public byte[] ConvertToBytes(HttpPostedFileBase image)  
  39.     {  
  40.         byte[] imageBytes = null;  
  41.         BinaryReader reader = new BinaryReader(image.InputStream);  
  42.         imageBytes = reader.ReadBytes((int)image.ContentLength);  
  43.         return imageBytes;  
  44.     }  
  45.   
  46.     public ActionResult RetrieveImage(int id)  
  47.     {  
  48.         byte[] cover = objUserPhotoGalleryService.GetImageFromDataBase(id);  
  49.         if (cover != null)  
  50.         {  
  51.             return File(cover, "image/jpg");  
  52.         }  
  53.         else  
  54.         {  
  55.             return null;  
  56.         }  
  57.     }  
  58. }  
Test the project

Test the project

manage photo gallery

I created a project in ASP.NET MVC of User Photo Gallery. I hope this article is useful for everybody. Any suggestion for updating and improving the project is welcome. I hope to see some good comments.

Up Next
    Ebook Download
    View all
    Learn
    View all