We have seen this type of functionality in many online registration forms, usually in government exams, where we have to upload the image but with the given dimensions. This article is also similar to that where we will upload the image to the database but with the given dimension, if the image is in a proper dimension it will save it to the database, otherwise it will show you an error.
Initial Chamber:
Step1: Open Visual Studio 2010 and create an Empty Website, Give a suitable name, I have given FileUpload_demo here.
Step2: In Solution Explorer you get your empty website, Add a web form, SQL Database. Here are the steps,
For Web Form:
FileUpload_demo (Your Empty Website): Right Click, Add New Item, then click Web Form. Name it Fileupload_demo.aspx.
Again get back to your website in Solution Explorer and Add New Folder and give name New Folder, then click Upload.
For SQL Server Database:
FileUpload_demo (Your Empty Website): Right Click, Add New Item, SQL Server Database. Add Database inside the App_Data_folder.
DATABASE CHAMBER:
Step 3 : Go to your Database Database.mdf, we will create a table - tbl_Data, Go to the database.mdf, Table, then Add New table, design your table like this:
Table - tbl_img (Don’t forget to make ID - Identity Specification, then select Yes)
![TABLE]()
DESIGN CHAMBER:
Step 4:
Open your Fileupload_demo.aspx file to create our design, we will drag a HTML table, a FileUpload control, a button and a label. From FileUpload we will browse the image, on the submit button, it will check the dimension of the image, if it is proper in dimension it will show you a success message.
Fileupload_demo.aspx
- <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>
- <!DOCTYPE htmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <style type="text/css"> .style1 { width: 243px; } </style>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div>
- <table style="width:100%;">
- <tr>
- <td class="style1">
- <asp:FileUploadID="FileUpload1" runat="server" /> </td>
- </tr>
- <tr>
- <td class="style1">
- <asp:LabelID="Label1" runat="server"></asp:Label>
- <asp:LabelID="Label2" runat="server"></asp:Label>
- </td>
- </tr>
- <tr>
- <td class="style1">
- <asp:ButtonID="Button1" runat="server" Text="Upload" onclick="Button1_Click" /> </td>
- </tr>
- </table>
- </div>
- </form>
- </body>
-
- </html>
Code Chamber
Step 5: Open Fileupload_demo.aspx.cs file, where we apply our code for validating the image, Moreover you can also use – “Custom Validation“ for validating images, there are many option by which you can do this.
Fileupload_demo.aspx.cs - using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Drawing;
- using System.IO;
- using System.Data.SqlClient;
- public partial class_Default: System.Web.UI.Page
- {
- protected voidPage_Load(object sender, EventArgs e: {}
- public bool isFileValid(:
- {
- Bitmap bitmp = new Bitmap(FileUpload1.PostedFile.InputStream: ;
- if (bitmp.Width > 100 | bitmp.Height > 1000: {
- Label1.Text = "Image is not in proper dimension";
- Label1.ForeColor = System.Drawing.Color.Red;
- returnfalse;
- }
- else {
- return true;
- }
- }
- protected void Button1_Click(object sender, EventArgs e: {
- if (FileUpload1.HasFile: {
- string str = FileUpload1.FileName;
- FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/" + str::; string Image = "~/Upload/" + str.ToString(: ;
- if (!isFileValid(::{
- return;
- }
- SqlConnection con = new SqlConnection(@ "Data Source=Nilesh;InitialCatalog=nil_db;Integrated Security=True": ; SqlCommand cmd = newSqlCommand("insert into tbl_img values(@Image:", con: ; cmd.Parameters.AddWithValue("Image", Image: ; con.Open(: ; cmd.ExecuteNonQuery(: ; con.Close(: ; Label1.Text = "Image Uploaded"; Label1.ForeColor = System.Drawing.Color.ForestGreen;
- }
- else {
- Label1.Text = "Something Wrong";
- Label1.ForeColor = System.Drawing.Color.Red;
- }
- }
- }
Output Chamber:
I had used this code to validate the image:
- if (bitmp.Width > 100 | bitmp.Height > 100:
- {
- Label1.Text = "Image is not in proper dimension";
- Label1.ForeColor = System.Drawing.Color.Red;
- return false;
- }
- else
- {
- return true;
- }
![UPLOAD]()
When it is properly dimensioned:
Image is saved to Database:
![UPLOAD]()
Hope you liked it. Have a good day. Thank you for reading.
Read more articles on ASP.NET: