Step9: Modify your web.config file.
Connection string:
Paste this line in your web.cong file inside <system.web> section. This will increase the default image file upload size and allows you to upload large sized image.
Step10: Add following Namespaces:
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
Step11: GenerateThumbNail() Method to generate image thumbnails. Paste in your Default.aspx.cs page.
public string GenerateThumbNail(string imageName, string thumbNailFolder, int width)
{
System.Drawing.Image image =
System.Drawing.Image.FromFile(Server.MapPath("UploadedImages/" + imageName.ToString()));
int srcWidth = image.Width;
int srcHeight = image.Height;
int thumbWidth = width;
int thumbHeight;
Bitmap bmp;
if (srcHeight > srcWidth)
{
thumbHeight = (srcHeight / srcWidth) * thumbWidth;
bmp = new Bitmap(thumbWidth, thumbHeight);
}
else
{
thumbHeight = thumbWidth;
thumbWidth = (srcWidth / srcHeight) * thumbHeight;
bmp = new Bitmap(thumbWidth, thumbHeight);
}
System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
System.Drawing.Rectangle rectDestination =
new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);
string aa = Server.MapPath("~/" + thumbNailFolder + "/" + imageName);
bmp.Save(HttpContext.Current.Server.MapPath("~/" + thumbNailFolder + "/" + imageName));
bmp.Dispose();
image.Dispose();
return aa;
}
Step12: Button Click event to upload images and thumbnails and store their information in database.
protected void btnUploadImage_Click(object sender, EventArgs e)
{
string fileNameWithPath = Server.MapPath("~/UploadedImages/") + myFielUpload.FileName.ToString();
myFielUpload.SaveAs(fileNameWithPath);
//===== Generate thumbnail
GenerateThumbNail(myFielUpload.FileName, "UploadedThumbs", 100);
//---- Store image and image thumbnail information to database.
//====== Getting connection string defined in the web.config file. Pointed to the database we want to use.
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString);
//======= Insert Query.
string cmdText = "INSERT INTO ImageUpload (ImageName,ImageThumbnail) VALUES (@ImageName,@ImageThumbnail)";
//====== Providning information to SQL command object about which query to
//====== execute and from where to get database connection information.
SqlCommand cmd = new SqlCommand(cmdText, con);
//===== Adding parameters/Values.
cmd.Parameters.AddWithValue("@ImageName", myFielUpload.FileName);
cmd.Parameters.AddWithValue("@ImageThumbnail", myFielUpload.FileName);
//===== To check current state of the connection object. If it is closed open the connection
//===== to execute the insert query.
if (con.State == ConnectionState.Closed)
{
con.Open();
}
//===== Execute Query.
cmd.ExecuteNonQuery();
//===== close the connection.
con.Close();
//==== Bind images to image gallery.
bindImageGallery();
}
public void bindImageGallery()
{
//====== Getting connection string defined in the web.config file. Pointed to the database we want to use.
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString);
//======= Select Query.
string cmdText = "SELECT * FROM ImageUpload";
//====== Providning information to SQL command object about which query to
//====== execute and from where to get database connection information.
SqlCommand cmd = new SqlCommand(cmdText, con);
//===== To check current state of the connection object. If it is closed open the connection
//===== to execute the insert query.
if (con.State == ConnectionState.Closed)
{
con.Open();
}
//===== Execute Query and bind data to Datalist.
dtlstImageGallery.DataSource = cmd.ExecuteReader();
dtlstImageGallery.DataBind();
}