在将图像存储到asp.net c#中的文件夹之前调整图像大小

时间:2013-07-27 06:44:43

标签: c# asp.net

我希望在将其存储到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();

        }

    }

2 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分: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;
    }