简体   繁体   中英

ASP.NET upload image, resize, and save to file system.

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

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
         }
     }
  }

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