0

I am having no luck when trying to use a FileUpload dialog to upload an image, resize while keepin the aspect ratio and then saving it to my file system using c# and ASP.NET Does anyone have a solution that works or a link to somewhere that can show me a working example? A lot of the solutions I've looked and tried only work with C# but not with ASP.NET

Pacobart
  • 241
  • 2
  • 7
  • 20

1 Answers1

0

ImageResizer will give you everything you want:

http://imageresizing.net/

string fileExt = System.IO.Path.GetExtension(imageUpload.FileName);
if (string.Equals(fileExt, ".jpg", StringComparison.OrdinalIgnoreCase))
{
    foreach (string fileKey in HttpContext.Current.Request.Files.Keys)
    {
        HttpPostedFile file = HttpContext.Current.Request.Files[fileKey];
        if (file.ContentLength <= 0) continue; //Skip unused file controls.

        try
        {
             ImageJob i = new ImageJob(file, "~/eventimages/<guid>_<filename:A-Za-z0-9>.<ext>",
                 new ResizeSettings("width=60&height=60&format=jpg&crop=auto"));
             i.Build();

             ImageJob j = new ImageJob(file, "~/eventimages/<guid>_<filename:A-Za-z0-9>.<ext>",
                 new ResizeSettings("width=250&height=250&format=jpg&crop=auto"));
             j.Build();

             string sitePath = MapPath(@"~");
             thumbImagePath = i.FinalPath.Replace(sitePath, "~\\");
             mainImagePath = j.FinalPath.Replace(sitePath, "~\\");
         }
         catch
         {
  // Had to swallow the exception here: 
  // http://stackoverflow.com/questions/7533845/system-argumentexception-parameter-is-not-valid
         }
     }
  }
IrishChieftain
  • 15,108
  • 7
  • 50
  • 91