0
嘿,我想在來調整圖像到我的文件上傳的功能添加,但我不知道如何與當前的代碼合併它:我的方法與圖像大小調整方法結合
我的代碼
protected void UploadButton_Click(object sender, EventArgs e)
{
if (FileUploadControl.HasFile)
{
try
{
string theUserId = Session["UserID"].ToString();
OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;");
cn.Open();
OdbcCommand sc = new OdbcCommand(string.Format("SELECT picturepath FROM Pictures WHERE UserID ='{0}'", theUserId), cn);
OdbcDataReader reader = sc.ExecuteReader();
while (reader.Read())
{
if (System.IO.File.Exists(Server.MapPath(Convert.ToString(reader[0]))))
{
System.IO.File.Delete(Server.MapPath(Convert.ToString(reader[0])));
}
}
string filenameDB = Path.GetFileName(FileUploadControl.FileName);
string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + Path.GetFileName(FileUploadControl.FileName);
FileUploadControl.SaveAs(fileuploadpath);
string fileuploadpaths = ("~/userdata/" + theUserId + "/uploadedimage/") + filenameDB;
StatusLabel.Text = "Upload status: File uploaded!";
OdbcCommand cmd = new OdbcCommand("UPDATE Pictures SET picturepath ='" + fileuploadpaths + "' WHERE UserId = '" + theUserId + "'", cn);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
}
}
圖像的大小調整代碼:
protected void Button1_Click(object sender, EventArgs e)
{
if (this.IsValid && this.FileUpload1.HasFile)
{
//Create an ImageElement to wrap up the uploaded image
Neodynamic.WebControls.ImageDraw.ImageElement uploadedImage;
uploadedImage = Neodynamic.WebControls.ImageDraw.ImageElement.FromBinary(this.FileUpload1.FileBytes);
//Create Resize imaging action to apply on the uploaded image
//NOTE: You may apply any of the ImageDraw built-in imaging actions
Neodynamic.WebControls.ImageDraw.Resize actResize = new Neodynamic.WebControls.ImageDraw.Resize();
actResize.Width = 150;
actResize.LockAspectRatio = Neodynamic.WebControls.ImageDraw.LockAspectRatio.WidthBased;
uploadedImage.Actions.Add(actResize);
//Composite the output image by using ImageDraw class
Neodynamic.WebControls.ImageDraw.ImageDraw imgDraw = new Neodynamic.WebControls.ImageDraw.ImageDraw();
//Add uploaded image
imgDraw.Elements.Add(uploadedImage);
//Output image settings...
//For example: save the image in JPEG format always
imgDraw.ImageFormat = Neodynamic.WebControls.ImageDraw.ImageDrawFormat.Jpeg;
imgDraw.JpegCompressionLevel = 90;
//Now, save the output image on disk
string fileName = @"C:\Temp\" + System.IO.Path.GetFileNameWithoutExtension(this.FileUpload1.FileName) + ".jpg";
imgDraw.Save(fileName);
}
}
現在我只希望這一切在ŧ他一鍵:
protected void UploadButton_Click(object sender, EventArgs e)
從我的代碼。
也有一種方法來使用更通用的東西,而不是Neodynamic.WebControls.ImageDraw.dll – 2011-03-26 03:04:09