简体   繁体   中英

Resizing image on file upload - C#

I want to resize image(create thumbnails) on uploading file. But it seems to not reading the path correctly and it's crashing...

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();
        }

It's saving the image into directory, but it won't read the path, so the aspectRatio can't get its size. Any ideas?

EDIT1: Error message: An exception of type 'System.IO.FileNotFoundException' occurred in System.Drawing.dll but was not handled in user code

Additional information: imagepath here.

EDIT2: An exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll but was not handled in user code Additional information: A generic error occurred in GDI+. This line is causing error:

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

Use Server.MapPath(path) for reading the path. You're using it for saving but not for reading.

 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();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM