【问题标题】:ASP.NET upload image, resize, and save to file system.ASP.NET 上传图像、调整大小并保存到文件系统。
【发布时间】:2012-10-09 11:54:08
【问题描述】:

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

【问题讨论】:

  • 我可以给你一个在我的 ASP.NET 服务器上运行的 VB.NET 版本。你也许可以转换它?
  • 为什么不使用 c# 代码作为 ASP.NET 项目的外部 dll 呢?只需调用所需的函数...
  • 能否发给我你的vb版本。我试试看

标签: c# asp.net


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 2011-01-29
    • 1970-01-01
    • 2010-09-20
    • 2018-02-09
    • 2023-04-11
    相关资源
    最近更新 更多