I am showing this functionality by creating an ASP.Net MVC 4 application (R- An Image Gallery Application In MVC 4).

The following is my SQL Server Data Table structure: (Album_Details).

design view
Image 1.

Now open Visual Studio and select "File" -> "New" -> "Project...":

New Project
Image 2.

internet application
Image 3.

Now right-click on the Model Folder Add New Item -> Add New Class (ImageInfo):

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. namespace AnImageGalleryApplicationInMVC4.Models
  6. {
  7. public class ImageInfo
  8. {
  9. public int Image_Id { get; set; }
  10. public string Image_Name { get; set; }
  11. public byte[] Image { get; set; }
  12. public string Image_UploadedDate { get; set; }
  13. }
  14. }
Now again right-click on the Model Folder-> Add New Item -> Add New Class (RAlbumService):
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Configuration;
  8. using System.IO;
  9. namespace AnImageGalleryApplicationInMVC4.Models
  10. {
  11. public class RAlbumService
  12. {
  13. public IList<ImageInfo> GetAllImages()
  14. {
  15. List<ImageInfo> objImgInfo = new List<ImageInfo>();
  16. SqlDataAdapter da;
  17. DataSet ds = new DataSet();
  18. SqlConnection con = new SqlConnection();
  19. con.ConnectionString = @"Data Source=MYPC\SqlServer2k8;Integrated Security=true;Initial Catalog=R-ImageGallery";
  20. SqlCommand cmd = new SqlCommand("SELECT * FROM Album_Details", con);
  21. da = new SqlDataAdapter(cmd);
  22. da.Fill(ds);
  23. con.Open();
  24. cmd.ExecuteNonQuery();
  25. con.Close();
  26. if (!object.Equals(ds.Tables[0], null))
  27. {
  28. if (ds.Tables[0].Rows.Count > 0)
  29. {
  30. for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  31. {
  32. ImageInfo objThisIMGInfo = new ImageInfo();
  33. objThisIMGInfo.Image_Id = Convert.ToInt32(ds.Tables[0].Rows[i]["IMG_ID"]);
  34. objThisIMGInfo.Image_Name = ds.Tables[0].Rows[i]["ImageName"].ToString();
  35. objThisIMGInfo.Image = (byte[])ds.Tables[0].Rows[i]["Photo"];
  36. objThisIMGInfo.Image_UploadedDate = ds.Tables[0].Rows[i]["UploadedDate"].ToString().Split(' ')[0];
  37. objImgInfo.Add(objThisIMGInfo);
  38. }
  39. }
  40. }
  41. return objImgInfo.ToList();
  42. }
  43. public byte[] GetImageDetails(int id)
  44. {
  45. SqlConnection con = new SqlConnection(@"Data Source=MYPC\SqlServer2k8;Integrated Security=true;Initial Catalog=R-ImageGallery");
  46. using (SqlCommand cmd = new SqlCommand())
  47. {
  48. cmd.Connection = con;
  49. cmd.CommandType = CommandType.Text;
  50. cmd.CommandText = "SELECT Photo FROM Album_Details where IMG_ID=@ImageId";
  51. cmd.Parameters.AddWithValue("@ImageId", id);
  52. con.Open();
  53. SqlDataReader dr = cmd.ExecuteReader();
  54. ImageInfo objImageInfo = new ImageInfo();
  55. while (dr.Read())
  56. {
  57. objImageInfo.Image = (byte[])dr["Photo"];
  58. }
  59. return objImageInfo.Image;
  60. }
  61. }
  62. public int UploadImage(ImageInfo objImageInfo)
  63. {
  64. SqlConnection con = new SqlConnection(@"Data Source=MYPC\SqlServer2k8;Integrated Security=true;Initial Catalog=R-ImageGallery");
  65. using (SqlCommand cmd = new SqlCommand())
  66. {
  67. cmd.Connection = con;
  68. cmd.CommandType = CommandType.Text;
  69. cmd.CommandText = "INSERT INTO Album_Details(ImageName,Photo) VALUES(@ImageName,@Photo)";
  70. cmd.Parameters.AddWithValue("@ImageName", objImageInfo.Image_Name);
  71. cmd.Parameters.AddWithValue("@Photo", objImageInfo.Image);
  72. con.Open();
  73. int result = cmd.ExecuteNonQuery();
  74. con.Close();
  75. return result;
  76. }
  77. }
  78. }
  79. }
Now right-click on Controller -> Add New Controller (Albums):
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using AnImageGalleryApplicationInMVC4.Models;
  7. using System.IO;
  8. namespace AnImageGalleryApplicationInMVC4.Controllers
  9. {
  10. public class AlbumsController : Controller
  11. {
  12. RAlbumService objAlbumModelService = new RAlbumService();
  13. [HttpGet]
  14. public ActionResult Index()
  15. {
  16. ViewBag.total = objAlbumModelService.GetAllImages().ToList().Count;
  17. return View(objAlbumModelService.GetAllImages().ToList());
  18. }
  19. public ActionResult GetImgInformation(int id)
  20. {
  21. byte[] cover = objAlbumModelService.GetImageDetails(id);
  22. if (cover != null)
  23. {
  24. return File(cover, "image/jpg");
  25. }
  26. else
  27. {
  28. return null;
  29. }
  30. }
  31. [HttpPost]
  32. public ActionResult Index(FormCollection collection)
  33. {
  34. HttpPostedFileBase file = Request.Files["ImageData"];
  35. ImageInfo objAlbumMaster = new ImageInfo();
  36. objAlbumMaster.Image_Name = collection["ImageName"].ToString();
  37. objAlbumMaster.Image = ConvertToBytes(file);
  38. objAlbumModelService.UploadImage(objAlbumMaster);
  39. return View(objAlbumModelService.GetAllImages().ToList());
  40. }
  41. public byte[] ConvertToBytes(HttpPostedFileBase image)
  42. {
  43. byte[] imageBytes = null;
  44. BinaryReader reader = new BinaryReader(image.InputStream);
  45. imageBytes = reader.ReadBytes((int)image.ContentLength);
  46. return imageBytes;
  47. }
  48. }
  49. }
Now right-click on the Index Method name add new view and do the following code:
  1. @model IList<AnImageGalleryApplicationInMVC4.Models.ImageInfo>
  2. @{
  3. ViewBag.Title = "An Image Gallery Application In MVC 4";
  4. }
  5. <table>
  6. <tr>
  7. <td style="vertical-align: top; width: 15%;">
  8. <table style="border: solid 2px Red; padding: 5px; width: 100%;">
  9. <tr>
  10. <td style="background-color: skyblue; color: red; text-align: center; font-weight: bold;">Upload Image
  11. </td>
  12. </tr>
  13. <tr>
  14. <td>
  15. @using (Html.BeginForm("Index", "Albums", FormMethod.Post, new { enctype = "multipart/form-data" }))
  16. {
  17. @Html.AntiForgeryToken()
  18. <table style="width: 100%;">
  19. <tr>
  20. <td style="padding-left: 10px;">Name :
  21. </td>
  22. <td>@Html.TextBox("ImageName", "", new { style = "width:200px" })
  23. </td>
  24. </tr>
  25. <tr>
  26. <td style="padding-left: 10px;">Image :
  27. </td>
  28. <td>
  29. <input type="file" name="ImageData" id="ImageData" />
  30. </td>
  31. </tr>
  32. <tr>
  33. <td></td>
  34. <td>
  35. <input type="submit" name="submit" value="Upload" />
  36. </td>
  37. </tr>
  38. </table>
  39. }
  40. </td>
  41. </tr>
  42. </table>
  43. </td>
  44. <td style="vertical-align:top;">
  45. <table style="border: solid 2px Green; padding: 5px; width: 100%;">
  46. <tr>
  47. <td colspan="4" style="background-color: skyblue; color: red; text-align: center; font-weight: bold;">All Uploaded Images
  48. </td>
  49. </tr>
  50. <tr>
  51. <td>
  52. <table>
  53. @{
  54. const int ShowColumn = 3;
  55. int OldRecords = 0;
  56. var items = Model.Skip(OldRecords).Take(ShowColumn);
  57. while (items.Count() > 0)
  58. {
  59. <tr>
  60. @foreach (var item in items)
  61. {
  62. <td style="padding-left: 5px;">
  63. <table>
  64. <tr>
  65. <td style="text-align: center;">
  66. <img src="/Albums/GetImgInformation/@item.Image_Id" width="140" />
  67. </td>
  68. </tr>
  69. <tr>
  70. <td style="text-align: center; font-weight: bold; color: Teal">@item.Image_Name | @item.Image_UploadedDate
  71. </td>
  72. </tr>
  73. </table>
  74. </td>
  75. }
  76. </tr>
  77. OldRecords += ShowColumn;
  78. items = Model.Skip(OldRecords).Take(ShowColumn);
  79. }
  80. }
  81. </table>
  82. </td>
  83. </tr>
  84. </table>
  85. </td>
  86. </tr>
  87. </table>
Now run your application:

Run your application
Image 4.

image
Image 5.

show image
Image 6.