我想在将个人资料图片存储到 asp.net 中的文件夹之前调整其大小。调整大小的技术是什么?这是我的图片上传代码。任何帮助将不胜感激..谢谢!
protected void btnUpload_Click(object sender, EventArgs e)
{
StartUpLoad();
}
private void StartUpLoad()
{
//get the file name of the posted image
string imgName = fileuploadImage.FileName.ToString();
//sets the image path
string imgPath = "ImageStorage/" + imgName;
fileuploadImage.SaveAs(Server.MapPath(imgPath));
//get the size in bytes that
int imgSize = fileuploadImage.PostedFile.ContentLength;
//validates the posted file before saving
if (fileuploadImage.PostedFile != null && fileuploadImage.PostedFile.FileName != "")
{
if (fileuploadImage.PostedFile.ContentLength > 102400)
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big')", true);
}
else
{
//save the file
//Call the method to execute Insertion of data to the Database
ExecuteInsert(imgName, imgSize, imgPath);
Response.Write("Save Successfully!");
}
}
}
private string GetConnectionString()
{
//sets the connection string from your web config file. "DBConnection" is the name of your Connection String
return System.Configuration.ConfigurationManager.ConnectionStrings["ParkingProjectConnectionString"].ConnectionString;
}
private void ExecuteInsert(string name, int size, string path)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
string sql = "INSERT INTO ImageInfo (ImageName, ImageSize, ImagePath) VALUES "
+ " (@imgName,@imgSize,@imgPath)";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] param = new SqlParameter[3];
param[0] = new SqlParameter("@imgName", SqlDbType.NVarChar, 50);
param[1] = new SqlParameter("@imgSize", SqlDbType.BigInt, 9999);
param[2] = new SqlParameter("@imgPath", SqlDbType.VarChar, 50);
param[0].Value = name;
param[1].Value = size;
param[2].Value = path;
for (int i = 0; i < param.Length; i++)
{
cmd.Parameters.Add(param[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}