0

我想在将个人资料图片存储到 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();

        }

    }
4

2 回答 2

1

本准则适用于我。

 protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Test/") + "test.jpg");
            string pth = Server.MapPath("~/Test/test.jpg");
            resizeImageAndSave(pth);
        }
    }
    private string resizeImageAndSave(string imagePath)
    {

        System.Drawing.Image fullSizeImg
             = System.Drawing.Image.FromFile(imagePath);
        var thumbnailImg = new Bitmap(150, 130);
        var thumbGraph = Graphics.FromImage(thumbnailImg);
        thumbGraph.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        thumbGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        thumbGraph.InterpolationMode =System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        var imageRectangle = new Rectangle(0, 0, 150, 130);
        thumbGraph.DrawImage(fullSizeImg, imageRectangle);
        string targetPath = imagePath.Replace(Path.GetFileNameWithoutExtension(imagePath), Path.GetFileNameWithoutExtension(imagePath) + "-resize");
        thumbnailImg.Save(targetPath, System.Drawing.Imaging.ImageFormat.Jpeg); //(A generic error occurred in GDI+) Error occur here !
        thumbnailImg.Dispose();
        return targetPath;
    }
于 2013-07-27T06:46:32.733 回答
1

试试这个代码:

    private Image resizeImageAndSave(string imagePath)
    {
        Size wantedSize = new Size(250, 180);
        Image fullImg = Image.FromFile(imagePath);
        Bitmap resizedImg = new Bitmap(fullImg, wantedSize);
        return (Image)resizedImg;
    }
于 2014-09-14T09:09:59.957 回答