EDN Admin
Well-known member
I had managed to let user upload the photo and store into the folder. Now, I need to generate thumbnail from the uploaded image but there are some errors. Can someone tell me where I had done wrongly? Thank you
<span style="color:#636363; font-family:Segoe UI; font-size:13px protected void Button1_Click(object sender, EventArgs e)<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px {<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //Get Filename from fileupload control<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //Save images into Images folder<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px FileUpload1.SaveAs(Request.MapPath("/Images/uploaded/" + filename));<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //Getting dbconnection from web.config connectionstring<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px using (SqlDataAdapter adapter = new SqlDataAdapter("select ImageName from Images", connection))<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //Open the database connection<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px {<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px DataTable dt = new DataTable();<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px adapter.Fill(dt);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px DataList1.DataSource = dt;<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px DataList1.DataBind();<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px }<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px connection.Open();<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //Query to insert images path and name into database<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px SqlCommand cmd = new SqlCommand("Insert into Images(ImageName) values(@ImageName)", connection);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //Passing parameters to query<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px cmd.Parameters.AddWithValue("@ImageName", filename);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px cmd.ExecuteNonQuery();<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //Close dbconnection<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px connection.Close();<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px string strImage = FileUpload1.FileName.ToString();<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px if(FileUpload1.HasFile)<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px {<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px strImage = FileUpload1.FileName.ToString();<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //check whether the file exists or not<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px if (!File.Exists(Request.MapPath("/Images/uploaded/") + strImage)) //testing<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px {<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px <br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //create a new file name with grid<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px strImage = Guid.NewGuid().ToString().Substring(0,8) + strImage.Substring(strImage.IndexOf(.));<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px <br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px }<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //save the file<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px FileUpload1.SaveAs(Request.MapPath("/Images/uploaded/") + strImage); //testing<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px } <br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px }<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px private void GenerateThumbnails(double scaleFactor, Stream sourcePath, string targetPath)<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px {<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px using (var image = <strong style="color:#636363; font-family:Segoe UI; font-size:13px Image.FromStream <span style="color:#636363; font-family:Segoe UI; font-size:13px (sourcePath))<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px {<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px // can given width of image as we want <br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px var newWidth = (int)(image.Width * scaleFactor);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px // can given height of image as we want <br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px var newHeight = (int)(image.Height * scaleFactor);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px var thumbnailImg = new Bitmap(newWidth, newHeight);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px var thumbGraph = Graphics.FromImage(thumbnailImg);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px thumbGraph.CompositingQuality = CompositingQuality.HighQuality;<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px thumbGraph.SmoothingMode = SmoothingMode.HighQuality;<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px thumbGraph.DrawImage(image, imageRectangle);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px thumbnailImg.Save(targetPath, image.RawFormat);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px }<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px } <br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px }<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px There is error with Image.FromStream
<div style="font-size:13px; color:#222222; font-family:arial,sans-serif Error 6 Image is an ambiguous reference between System.Web.UI.WebControls.Image and System.Drawing.Image
<div style="font-size:13px; color:#222222; font-family:arial,sans-serif
Error 7 System.Web.UI.WebControls.Image does not contain a definition for FromStream<br/>
<br/>
Can someone tell me how to solve this problem and how will the thumbnail be save into the database. (I need to save both the image and the thumbnail) Thank you =)
View the full article
<span style="color:#636363; font-family:Segoe UI; font-size:13px protected void Button1_Click(object sender, EventArgs e)<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px {<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //Get Filename from fileupload control<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //Save images into Images folder<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px FileUpload1.SaveAs(Request.MapPath("/Images/uploaded/" + filename));<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //Getting dbconnection from web.config connectionstring<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px using (SqlDataAdapter adapter = new SqlDataAdapter("select ImageName from Images", connection))<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //Open the database connection<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px {<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px DataTable dt = new DataTable();<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px adapter.Fill(dt);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px DataList1.DataSource = dt;<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px DataList1.DataBind();<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px }<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px connection.Open();<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //Query to insert images path and name into database<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px SqlCommand cmd = new SqlCommand("Insert into Images(ImageName) values(@ImageName)", connection);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //Passing parameters to query<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px cmd.Parameters.AddWithValue("@ImageName", filename);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px cmd.ExecuteNonQuery();<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //Close dbconnection<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px connection.Close();<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px string strImage = FileUpload1.FileName.ToString();<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px if(FileUpload1.HasFile)<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px {<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px strImage = FileUpload1.FileName.ToString();<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //check whether the file exists or not<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px if (!File.Exists(Request.MapPath("/Images/uploaded/") + strImage)) //testing<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px {<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px <br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //create a new file name with grid<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px strImage = Guid.NewGuid().ToString().Substring(0,8) + strImage.Substring(strImage.IndexOf(.));<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px <br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px }<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px //save the file<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px FileUpload1.SaveAs(Request.MapPath("/Images/uploaded/") + strImage); //testing<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px } <br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px }<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px private void GenerateThumbnails(double scaleFactor, Stream sourcePath, string targetPath)<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px {<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px using (var image = <strong style="color:#636363; font-family:Segoe UI; font-size:13px Image.FromStream <span style="color:#636363; font-family:Segoe UI; font-size:13px (sourcePath))<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px {<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px // can given width of image as we want <br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px var newWidth = (int)(image.Width * scaleFactor);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px // can given height of image as we want <br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px var newHeight = (int)(image.Height * scaleFactor);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px var thumbnailImg = new Bitmap(newWidth, newHeight);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px var thumbGraph = Graphics.FromImage(thumbnailImg);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px thumbGraph.CompositingQuality = CompositingQuality.HighQuality;<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px thumbGraph.SmoothingMode = SmoothingMode.HighQuality;<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px thumbGraph.DrawImage(image, imageRectangle);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px thumbnailImg.Save(targetPath, image.RawFormat);<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px }<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px } <br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px }<br style="color:#636363; font-family:Segoe UI; font-size:13px
<br style="color:#636363; font-family:Segoe UI; font-size:13px
<span style="color:#636363; font-family:Segoe UI; font-size:13px There is error with Image.FromStream
<div style="font-size:13px; color:#222222; font-family:arial,sans-serif Error 6 Image is an ambiguous reference between System.Web.UI.WebControls.Image and System.Drawing.Image
<div style="font-size:13px; color:#222222; font-family:arial,sans-serif
Error 7 System.Web.UI.WebControls.Image does not contain a definition for FromStream<br/>
<br/>
Can someone tell me how to solve this problem and how will the thumbnail be save into the database. (I need to save both the image and the thumbnail) Thank you =)
View the full article