首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将我的方法和图像调整方法结合起来

将我的方法和图像调整方法结合起来
EN

Stack Overflow用户
提问于 2011-03-26 03:02:17
回答 1查看 496关注 0票数 0

嘿,我想在我的文件上传中添加调整图像大小的功能,但是我不知道如何将它与当前代码结合起来:

我的代码

代码语言:javascript
复制
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;

                }

            }
    }

}

图像调整代码:

代码语言:javascript
复制
    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);

    }
}

现在我只想把它都放在一个按钮下面:

代码语言:javascript
复制
protected void UploadButton_Click(object sender, EventArgs e)

从我的密码里。

EN

回答 1

Stack Overflow用户

发布于 2011-07-06 00:19:48

调整大小的照片在asp.net可能是痛苦的调整大小与良好的质量。请参阅这里的代码,例如,如何以良好的质量从fileupload调整图像的大小:http://webdeveloperpost.com/Articles/How-To-Resize-Image-In-ASP-NET-With-Good-Quality.aspx

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5440104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档