嘿,我想在我的文件上传中添加调整图像大小的功能,但是我不知道如何将它与当前代码结合起来:
我的代码
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)从我的密码里。
发布于 2011-07-06 00:19:48
调整大小的照片在asp.net可能是痛苦的调整大小与良好的质量。请参阅这里的代码,例如,如何以良好的质量从fileupload调整图像的大小:http://webdeveloperpost.com/Articles/How-To-Resize-Image-In-ASP-NET-With-Good-Quality.aspx
https://stackoverflow.com/questions/5440104
复制相似问题