0

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);
5
  • Have you checked to make sure the file actually exists after you save it? Commented Nov 2, 2015 at 18:43
  • Yes, it's saved correctly. Maybe it's processing to resize image before it's finished to save. How can I give it a time to save? Commented Nov 2, 2015 at 18:45
  • I would just debug through the code and after the save is executed, but before the load from file, check for the file and then run the load and see if it errors. If the file is there and it still errors, then that mustn't be the issue. Commented Nov 2, 2015 at 18:51
  • The file is there. It's saving correctly as I said, but it's giving the IO.FileNotFoundException and I don't know why... Commented Nov 2, 2015 at 18:54
  • Ok, made it through the error in EDIT one, by adding Server.MapPath in Image.FromFile before path. But now get second error in EDIT2. Anyone? Commented Nov 2, 2015 at 19:12

1 Answer 1

2

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();
        }
Sign up to request clarification or add additional context in comments.

2 Comments

Check the edit. Please create the folder structure as images/galeria/thumb .If the folder structure is not there it will throw an error. I ran the code and it's fine. You can also create the folders pragmatically. Thumbs up if I am able to help :)
did you try to use the code I pasted along with the folder structure?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.