调整文件上传时的图像大小 - C#

时间:2015-11-02 18:00:03

标签: c# image resize image-resizing

我想在上传文件时调整图片大小(创建缩略图)。但它似乎没有正确地读取路径而且它正在崩溃......

CODE:

if (FileUpload1.HasFile)
        {
            string imageFile = FileUpload1.FileName;
            string path = "~/images/galeria/" + imageFile;
            cmd.Parameters.Add("@IMAGE_URL", System.Data.SqlDbType.NVarChar).Value = path;
            FileUpload1.SaveAs(Server.MapPath(path));

            System.Drawing.Image image = System.Drawing.Image.FromFile(path);
            float aspectRatio = (float)image.Size.Width / (float)image.Size.Height;
            int newHeight = 200;
            int newWidth = Convert.ToInt32(aspectRatio * newHeight);
            System.Drawing.Bitmap thumbBitmap = new System.Drawing.Bitmap(newWidth, newHeight);
            System.Drawing.Graphics thumbGraph = System.Drawing.Graphics.FromImage(thumbBitmap);
            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, newWidth, newHeight);
            thumbGraph.DrawImage(image, imageRectangle);
            thumbBitmap.Save("~/images/galeria/thumb/" + FileUpload1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
            thumbGraph.Dispose();
            thumbBitmap.Dispose();
            image.Dispose();
        }

它将图像保存到目录中,但它不会读取路径,因此aspectRatio无法获取其大小。有什么想法吗?

EDIT1: 错误信息: System.Drawing.dll中出现“System.IO.FileNotFoundException”类型的异常,但未在用户代码中处理

其他信息:imagepath此处。

EDIT2: System.Drawing.dll中发生类型为“System.Runtime.InteropServices.ExternalException”的异常但未在用户代码中处理附加信息:GDI +中发生一般错误。 此行导致错误:

thumbBitmap.Save("~/images/galeria/thumb/" + FileUpload1.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);

1 个答案:

答案 0 :(得分:2)

使用Server.MapPath(path)读取路径。您正在使用它进行保存,但不能用于阅读。

 if (FileUpload1.HasFile)
        {
            string imageFile = FileUpload1.FileName;
            string path = Server.MapPath("~/images/galeria/" + imageFile);
            FileUpload1.SaveAs(path);

            System.Drawing.Image image = System.Drawing.Image.FromFile(path);
            float aspectRatio = (float)image.Size.Width / (float)image.Size.Height;
            int newHeight = 200;
            int newWidth = Convert.ToInt32(aspectRatio * newHeight);
            System.Drawing.Bitmap thumbBitmap = new System.Drawing.Bitmap(newWidth, newHeight);
            System.Drawing.Graphics thumbGraph = System.Drawing.Graphics.FromImage(thumbBitmap);
            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, newWidth, newHeight);
            thumbGraph.DrawImage(image, imageRectangle);
            thumbBitmap.Save(Server.MapPath("~/images/galeria/thumb/" + FileUpload1.FileName), System.Drawing.Imaging.ImageFormat.Jpeg);
            thumbGraph.Dispose();
            thumbBitmap.Dispose();
            image.Dispose();
        }