SQL Database

  • Create Database Sample
  • Create Table

You can create a table using the following query -

create table tblImageInfo(id int identity(1,1) primary key,Name nvarchar(30),Content nvarchar(200));
Now, let's go ahead and start the process of compressing the images.

Step 1 - Open Visual Studio -> select New Project -> select Empty ASP.NET Web application

Step 2 - Create a folder named Images.

Step 3 - Here is the code for Default.aspx page.

output
  1. <body>
  2. <form id="form1" runat="server">
  3. <div align="center">
  4. <br />
  5. Name:<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
  6. <br />
  7. <br />
  8. UploadImage :<asp:FileUpload ID="fileupload1" runat="server" />
  9. <br />
  10. <br />
  11. <asp:Button ID="btnupload" runat="server" Text="Upload" OnClick="btnupload_Click" />
  12. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false"
  13. Font-Names = "Arial" >
  14. <Columns>
  15. <asp:BoundField DataField = "id" HeaderText = "ID" Visible="false" />
  16. <asp:BoundField DataField = "Name" HeaderText = "Image Name" />
  17. <asp:TemplateField>
  18. <ItemTemplate>
  19. <img src="<%#Eval("Content")%>" alt="<%#Eval("Name")%>" width="125px" height="150px"/>
  20. </ItemTemplate>
  21. </asp:TemplateField>
  22. </Columns>
  23. </asp:GridView>
  24. </div>
  25. </form>
  26. </body>
Step 4 - This is the code for default.aspx.cs file.
  1. public partial class Default : System.Web.UI.Page
  2. {
  3. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString);
  4. protected void Page_Load(object sender, EventArgs e)
  5. {
  6. if (!IsPostBack)
  7. {
  8. BindGridView();
  9. }
  10. }
  11. protected void btnupload_Click(object sender, EventArgs e)
  12. {
  13. string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
  14. string storedb = "Images/" + filename + "";
  15. string targetPath = Server.MapPath("Images/" + filename);
  16. Stream strm = fileupload1.PostedFile.InputStream;
  17. var targetFile = targetPath;
  18. ReduceImageSize(0.5, strm, targetFile);
  19. //insert reduced size image in db
  20. con.Open();
  21. SqlCommand cmd = new SqlCommand("Insert into tblImageInfo(Name,Content)values('"+txtname.Text+"','"+storedb+"')", con);
  22. cmd.ExecuteNonQuery();
  23. }
  24. protected void BindGridView()
  25. {
  26. DataTable dt = new DataTable();
  27. string strQuery = "select * from tblImageInfo order by id desc";
  28. SqlCommand cmd = new SqlCommand(strQuery);
  29. SqlDataAdapter da = new SqlDataAdapter();
  30. cmd.CommandType = CommandType.Text;
  31. cmd.Connection = con;
  32. try
  33. {
  34. con.Open();
  35. da.SelectCommand = cmd;
  36. da.Fill(dt);
  37. GridView1.DataSource = dt;
  38. GridView1.DataBind();
  39. }
  40. catch (Exception ex)
  41. {
  42. Response.Write(ex.Message);
  43. }
  44. finally
  45. {
  46. con.Close();
  47. da.Dispose();
  48. con.Dispose();
  49. }
  50. }
  51. private void ReduceImageSize(double scaleFactor, Stream sourcePath, string targetPath)
  52. {
  53. using (var image = System.Drawing.Image.FromStream(sourcePath))
  54. {
  55. var newWidth = (int)(image.Width * scaleFactor);
  56. var newHeight = (int)(image.Height * scaleFactor);
  57. var thumbnailImg = new Bitmap(newWidth, newHeight);
  58. var thumbGraph = Graphics.FromImage(thumbnailImg);
  59. thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
  60. thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
  61. thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
  62. var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
  63. thumbGraph.DrawImage(image, imageRectangle);
  64. thumbnailImg.Save(targetPath, image.RawFormat);
  65. }
  66. }
  67. }
Step 5 - Build and run the application. Upload uncompressed images and get the compressed ones. You can check the image size.
application