HI
I used below code for uploading image and show in image control with thumpnail metod for reducing image size:
protected void BtnUpload_Click(object sender, EventArgs e)
{
string path = Server.MapPath(".") + "\\../image/House/product\\";
string filename = System.IO.Path.GetFileName(fup1.PostedFile.FileName);
string[] validext = { ".jpg", ".png" };
string ext = System.IO.Path.GetExtension(fup1.PostedFile.FileName);
if (Array.IndexOf(validext, ext.ToLower()) < 0)
{
Label21.Text = "*please insert .jpg file";
return;
}
while (System.IO.File.Exists(path + "\\" + filename))
{
filename = "1" + filename;
}
SqlCommand _cmd = new SqlCommand("Fileupload11", _cn);
_cmd.CommandType = CommandType.StoredProcedure;
string data = Session["behcode"].ToString();
_cn.Open();
_cmd.Parameters.AddWithValue("@image", filename);
_cmd.Parameters.AddWithValue("@Behcode", data);
_cmd.Parameters.AddWithValue("@id", Convert.ToInt32(ViewState["Id"]));
object o = _cmd.ExecuteScalar();
int ID = (o != null && o != DBNull.Value) ? Convert.ToInt32(o) : 0;
if (ID > 0)
{
ViewState["Id"] = ID.ToString();
if (fup1.HasFile)
{
try
{
if (fup1.PostedFile.ContentLength < 202400)
{
fup1.PostedFile.SaveAs(Server.MapPath("~/image/house/product") + System.IO.Path.DirectorySeparatorChar + fup1.PostedFile.FileName);
Label21.Text = "*your file uploaded sucessfully";
}
else
{
Label21.Text = "file size is" + Convert.ToString(fup1.PostedFile.ContentLength / 1024) + "please insert file with 100KB size";
return;
}
}
catch (Exception ex)
{
Label21.Text = "Some problem occurred while uploading the file. Please try after some time.";
}
}
else
{
Label21.Text = "Please choose a file to upload.";
}
fup1.PostedFile.SaveAs(path + filename);
}
_cn.Close();
SqlCommand _cmd1 = new SqlCommand("selectFUP1", _cn);
_cmd1.CommandType = CommandType.StoredProcedure;
_cn.Open();
_cmd1.Parameters.AddWithValue("@Behcode", data);
_cmd1.Parameters.AddWithValue("@id", Convert.ToInt32(ViewState["Id"]));
SqlDataReader _dr = _cmd1.ExecuteReader();
while (_dr.Read())
{
this.imagepic.ImageUrl = GetThumbNail("~/image/house/product/" + _dr["Image"].ToString());
}
_cn.Close();
}
and:
public string GetThumbNail(string url)
{
string path = Server.MapPath(url);
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
using (System.Drawing.Image thumbnail = image.GetThumbnailImage(100, 100, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero))
{
using (MemoryStream memoryStream = new MemoryStream())
{
thumbnail.Save(memoryStream, ImageFormat.Jpeg);
Byte[] bytes = new Byte[memoryStream.Length];
memoryStream.Position = 0;
memoryStream.Read(bytes, 0, (int)bytes.Length);
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
return "data:image/jpeg;base64," + base64String;
}
}
}
it save original image in host
now I want it save image that reduce size with GetThumpnail metod in other place How I can do it?
Best Regards
Neda