0

我在尝试使用 FileUpload 对话框上传图像时没有运气,在保持纵横比的同时调整大小,然后使用 c# 和 ASP.NET 将其保存到我的文件系统中是否有人有有效的解决方案或指向某个地方的链接可以给我看一个工作的例子吗?我查看并尝试过的许多解决方案仅适用于 C#,但不适用于 ASP.NET

4

1 回答 1

0

ImageResizer 会给你你想要的一切:

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
         }
     }
  }
于 2012-10-09T03:48:13.713 回答