.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Thumbnail image upload in asp.net

Posted By:sandeepms       Posted Date: January 07, 2009    Points: 10    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  
 

Hi guys this is my second contribute to this website
as I have done lots of achievements in .net so far.
This code will upload thumbnail images to folders.

The miracle in this coding is it reduces image sizes to 20 % of its original size

using System;

using System.Data;

using System.Drawing;

using System.Drawing.Imaging;

using System.Data.SqlClient;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Configuration;

using System.IO;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class uphoto : System.Web.UI.Page

{

int imgHeight;

int imgWidth;

int height;

int width;

System.Drawing.Bitmap img;

const double imgSize = 200;

const double smallSize = 80;

double ratio;

string conn = ConfigurationManager.AppSettings["dsn"];

protected void Page_Load(object sender, EventArgs e)

{

try

{

lblEmail.Text = Session["uname"].ToString();

}

catch

{

Response.Redirect("index.aspx");

}

SqlConnection cnn = new SqlConnection(conn);

SqlDataAdapter da1 = new SqlDataAdapter("select * from users where email='" + lblEmail.Text + "'", cnn);

DataSet ds1 = new DataSet();

da1.Fill(ds1);

DataList3.DataSource = ds1;

DataList3.DataBind();

hdnMid.Value = ds1.Tables[0].Rows[0]["memberID"].ToString();

}

protected void lnkUpload2_Click(object sender, EventArgs e)

{

//try

//{

string path = Server.MapPath(@"photos/");

string small = Server.MapPath(@"photos/small/");

SqlConnection cnn = new SqlConnection(conn);

SqlDataAdapter da = new SqlDataAdapter("select * from users where email='" + lblEmail.Text + "'", cnn);

DataSet ds = new DataSet();

da.Fill(ds);

string photo = ds.Tables[0].Rows[0]["pname"].ToString();

string photoname = path + photo;

string photopath = small + photo;

if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLength < 1024 * 10500)

{

if (Path.GetExtension(FileUpload1.FileName).Equals(".jpg", StringComparison.OrdinalIgnoreCase) || Path.GetExtension(FileUpload1.FileName).Equals(".bmp", StringComparison.OrdinalIgnoreCase) || Path.GetExtension(FileUpload1.FileName).Equals(".png", StringComparison.OrdinalIgnoreCase) || Path.GetExtension(FileUpload1.FileName).Equals(".gif", StringComparison.OrdinalIgnoreCase))

{

img = new Bitmap(FileUpload1.PostedFile.InputStream);

double height = Convert.ToDouble(img.Height);

double width = Convert.ToDouble(img.Width);

int smallwidth;

int smallheight;

System.Drawing.Image imgThumbnail;

string type = FileUpload1.PostedFile.ContentType;

if (Path.GetExtension(FileUpload1.FileName).Equals(".jpg", StringComparison.OrdinalIgnoreCase) || Path.GetExtension(FileUpload1.FileName).Equals(".bmp", StringComparison.OrdinalIgnoreCase) || Path.GetExtension(FileUpload1.FileName).Equals(".png", StringComparison.OrdinalIgnoreCase))

{

string photoid = ds.Tables[0].Rows[0]["memberID"].ToString();

string pname = photoid + ".jpg";

string ppath = path + pname;

string smallpath = small + pname;

if (photo != "blank.jpg")

{

File.Delete(photoname);

File.Delete(photopath);

if (width > height)

{

ratio = width / height;

imgWidth = Convert.ToInt32(imgSize);

imgHeight = Convert.ToInt32(imgSize / ratio);

smallwidth = Convert.ToInt32(smallSize);

smallheight = Convert.ToInt32(smallSize / ratio);

}

else

{

ratio = height / width;

imgWidth = Convert.ToInt32(imgSize / ratio);

imgHeight = Convert.ToInt32(imgSize);

smallwidth = Convert.ToInt32(smallSize / ratio);

smallheight = Convert.ToInt32(smallSize);

}

}

else

{

if (width > height)

{

ratio = width / height;

imgWidth = Convert.ToInt32(imgSize);

imgHeight = Convert.ToInt32(imgSize / ratio);

smallwidth = Convert.ToInt32(smallSize);

smallheight = Convert.ToInt32(smallSize / ratio);

}

else

{

ratio = height / width;

imgWidth = Convert.ToInt32(imgSize / ratio);

imgHeight = Convert.ToInt32(imgSize);

smallwidth = Convert.ToInt32(smallSize / ratio);

smallheight = Convert.ToInt32(smallSize);

}

}

imgThumbnail = img.GetThumbnailImage(imgWidth, imgHeight, null, IntPtr.Zero);

EncoderParameters enc = new EncoderParameters(600);

img.SaveAdd(enc);

imgThumbnail.Save(ppath, ImageFormat.Jpeg);

imgThumbnail = img.GetThumbnailImage(smallwidth, smallheight, null, IntPtr.Zero);

imgThumbnail.Save(smallpath, ImageFormat.Jpeg);

SqlCommand cmm1 = new SqlCommand("updatephotoothers", cnn);

cmm1.Parameters.AddWithValue("@pname", pname);

cmm1.Parameters.AddWithValue("@mid", hdnMid.Value);

cmm1.CommandType = CommandType.StoredProcedure;

SqlCommand cmm = new SqlCommand("updatephoto", cnn);

cmm.Parameters.AddWithValue("@pname", pname);

cmm.Parameters.AddWithValue("@email", lblEmail.Text);

cmm.CommandType = CommandType.StoredProcedure;

cnn.Open();

cmm.ExecuteNonQuery();

cmm1.ExecuteNonQuery();

cnn.Close();

Response.Redirect("uphoto.aspx");

}

else if (Path.GetExtension(FileUpload1.FileName).Equals(".gif", StringComparison.OrdinalIgnoreCase))

{

string pname = ds.Tables[0].Rows[0]["memberID"].ToString() + ".gif";

string ppath = path + pname;

string smallpath = small + pname;

if (photo != "blank.jpg")

{

File.Delete(photoname);

File.Delete(photopath);

if (width > height)

{

ratio = width / height;

imgWidth = Convert.ToInt32(imgSize);

imgHeight = Convert.ToInt32(imgSize / ratio);

smallwidth = Convert.ToInt32(smallSize);

smallheight = Convert.ToInt32(smallSize / ratio);

}

else

{

ratio = height / width;

imgWidth = Convert.ToInt32(imgSize / ratio);

imgHeight = Convert.ToInt32(imgSize);

smallwidth = Convert.ToInt32(smallSize / ratio);

smallheight = Convert.ToInt32(smallSize);

}

}

else

{

if (width > height)

{

ratio = width / height;

imgWidth = Convert.ToInt32(imgSize);

imgHeight = Convert.ToInt32(imgSize / ratio);

smallwidth = Convert.ToInt32(smallSize);

smallheight = Convert.ToInt32(smallSize / ratio);

}

else

{

ratio = height / width;

imgWidth = Convert.ToInt32(imgSize / ratio);

imgHeight = Convert.ToInt32(imgSize);

smallwidth = Convert.ToInt32(smallSize / ratio);

smallheight = Convert.ToInt32(smallSize);

}

}

imgThumbnail = img.GetThumbnailImage(imgWidth, imgHeight, null, IntPtr.Zero);

imgThumbnail.Save(ppath, ImageFormat.Gif);

imgThumbnail = img.GetThumbnailImage(smallwidth, smallheight, null, IntPtr.Zero);

imgThumbnail.Save(smallpath, ImageFormat.Gif);

SqlCommand cmm1 = new SqlCommand("updatephotoothers", cnn);

cmm1.Parameters.AddWithValue("@pname", pname);

cmm1.Parameters.AddWithValue("@mid", hdnMid.Value);

cmm1.CommandType = CommandType.StoredProcedure;

SqlCommand cmm = new SqlCommand("updatephoto", cnn);

cmm.Parameters.AddWithValue("@pname", pname);

cmm.Parameters.AddWithValue("@email", lblEmail.Text);

cmm.CommandType = CommandType.StoredProcedure;

cnn.Open();

cmm.ExecuteNonQuery();

cmm1.ExecuteNonQuery();

cnn.Close();

Response.Redirect("uphoto.aspx");

}

else

{

string str = "alert('Please upload a valid jpeg, bitmap or gif photo');";

ClientScript.RegisterStartupScript(this.GetType(), "onClick", str, true);

}

}

else

{

string str = "alert('Please upload a valid jpeg, bitmap or gif photo');";

ClientScript.RegisterStartupScript(this.GetType(), "onClick", str, true);

}

}

else

{

string str = "alert('Please attach a image to upload not more than 10 MB');";

ClientScript.RegisterStartupScript(this.GetType(), "onClick", str, true);

}

//}

//catch

//{

// //Response.Redirect("welcome.aspx");

//}

}

}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend