How To Upload Image With Dimension Validation In ASP.NET Using C#

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

  1. <%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>  
  2.     <!DOCTYPE htmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.     <html xmlns="http://www.w3.org/1999/xhtml">  
  4.         <head runat="server">  
  5.             <title></title>  
  6.             <style type="text/css"> .style1 { width: 243px; } </style>  
  7.                 </head>  
  8.   
  9.                 <body>  
  10.                     <form id="form1" runat="server">  
  11.                         <div>  
  12.                             <table style="width:100%;">  
  13.                                 <tr>  
  14.                                     <td class="style1">  
  15.                                         <asp:FileUploadID="FileUpload1" runat="server" /> </td>  
  16.                                 </tr>  
  17.                                 <tr>  
  18.                                     <td class="style1">  
  19.                                         <asp:LabelID="Label1" runat="server"></asp:Label>  
  20.                                             <asp:LabelID="Label2" runat="server"></asp:Label>  
  21.                                                 </td>  
  22.                                 </tr>  
  23.                                 <tr>  
  24.                                     <td class="style1">  
  25.                                         <asp:ButtonID="Button1" runat="server" Text="Upload" onclick="Button1_Click" /> </td>  
  26.                                 </tr>  
  27.                                 </table>  
  28.                         </div>  
  29.                         </form>  
  30.                 </body>  
  31.   
  32.                 </html>  

UPLOAD

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
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Drawing;  
  8. using System.IO;  
  9. using System.Data.SqlClient;  
  10. public partial class_Default: System.Web.UI.Page   
  11. {  
  12.         protected voidPage_Load(object sender, EventArgs e: {}  
  13.                 public bool isFileValid(:   
  14.                       {  
  15.                         Bitmap bitmp = new Bitmap(FileUpload1.PostedFile.InputStream: ;  
  16.                             if (bitmp.Width > 100 | bitmp.Height > 1000: {  
  17.                                     Label1.Text = "Image is not in proper dimension";  
  18.                                     Label1.ForeColor = System.Drawing.Color.Red;  
  19.                                     returnfalse;  
  20.                                 }  
  21.                                 else {  
  22.                                     return true;  
  23.                                 }  
  24.                             }  
  25.                             protected void Button1_Click(object sender, EventArgs e: {  
  26.                                     if (FileUpload1.HasFile: {  
  27.                                             string str = FileUpload1.FileName;  
  28.                                             FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/" + str::; string Image = "~/Upload/" + str.ToString(: ;  
  29.                                                         if (!isFileValid(::{  
  30.                                                                     return;  
  31.                                                                 }  
  32.                                                                 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;  
  33.                                                                                     }  
  34.                                                                                     else {  
  35.                                                                                         Label1.Text = "Something Wrong";  
  36.                                                                                         Label1.ForeColor = System.Drawing.Color.Red;  
  37.                                                                                     }  
  38.                                                                                 }  
  39.                                                                             }  

Output Chamber:

I had used this code to validate the image:
  1. if (bitmp.Width > 100 | bitmp.Height > 100:  
  2.     {  
  3.         Label1.Text = "Image is not in proper dimension";  
  4.         Label1.ForeColor = System.Drawing.Color.Red;  
  5.         return false;  
  6.     }  
  7.     else  
  8.     {  
  9.         return true;  
  10.     }  

UPLOAD

UPLOAD
When it is properly dimensioned:

UPLOADED

Image is saved to Database:

DATABASE
UPLOAD

Hope you liked it. Have a good day. Thank you for reading.

Read more articles on ASP.NET:

Up Next
    Ebook Download
    View all
    Learn
    View all