Reputation: 994
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);
Upvotes: 0
Views: 6388
Reputation: 82
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();
}
Upvotes: 2