Thumbnail

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<div style="font-family:Segoe UI,Lucida Grande,Verdana,Arial,Helvetica,sans-serif; outline-width:0px; outline-style:initial; outline-color:initial; color:#333333; font-size:13px; line-height:16px; text-align:left; padding:0px; margin:0px; border:0px initial initial
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
protected void Button1_Click(object sender, EventArgs e)
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
{
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
//Get Filename from fileupload control
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
//Save images into Images folder
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
FileUpload1.SaveAs(Request.MapPath("/Images/uploaded/" + filename));
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
//Getting dbconnection from web.config connectionstring
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
using (SqlDataAdapter adapter = new SqlDataAdapter("select ImageName from Images", connection))
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
//Open the database connection
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
{
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
DataTable dt = new DataTable();
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
adapter.Fill(dt);
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
DataList1.DataSource = dt;
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
DataList1.DataBind();
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
}
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
connection.Open();
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
//Query to insert images path and name into database
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
SqlCommand cmd = new SqlCommand("Insert into Images(ImageName) values(@ImageName)", connection);
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
//Passing parameters to query
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
cmd.Parameters.AddWithValue("@ImageName", filename);
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
cmd.ExecuteNonQuery();
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
//Close dbconnection
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
connection.Close();
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
string strImage = FileUpload1.FileName.ToString();
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
if(FileUpload1.HasFile)
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
{
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
strImage = FileUpload1.FileName.ToString();
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
//check whether the file exists or not
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
if (!File.Exists(Request.MapPath("/Images/uploaded/") + strImage)) //testing
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
{
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial

<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
//create a new file name with grid
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
strImage = Guid.NewGuid().ToString().Substring(0,8) + strImage.Substring(strImage.IndexOf(.));
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial

<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
}
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
//save the file
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
FileUpload1.SaveAs(Request.MapPath("/Images/uploaded/") + strImage); //testing
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial

<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
}
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
}
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
public bool ThumbnailCallback()
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
{
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
return false;
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
}
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
protected void btnGenerateThumbNail_Click(object sender, EventArgs e)
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
{
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
/* Get Image From FileUpload Control and Save it */
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
if (FileUpload1.HasFile)
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
{
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
string FileName = FileUpload1.FileName;
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
string ImageFolderPath = Request.MapPath("/Images/uploaded/");
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
string SavePath = ImageFolderPath + "/" + FileName;
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
FileUpload1.SaveAs(SavePath);
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
System.Drawing.Image image = System.Drawing.Image.FromFile(SavePath);
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
/* callback */
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
System.Drawing.Image.GetThumbnailImageAbort callback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
/* Generate Thumbnail */
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
System.Drawing.Image ThumbImage = image.GetThumbnailImage(64, 64, callback, IntPtr.Zero);
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
ThumbImage.Save(ImageFolderPath + "/Thumb_" + FileName);
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
}
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
}
<div style="outline-width:0px; outline-style:initial; outline-color:initial; padding:0px; margin:0px; border:0px initial initial
Is there anything wrong with my code? The picture wont appear after I click on upload button.


View the full article
 
Back
Top