【问题标题】:save image to http server将图像保存到 http 服务器
【发布时间】:2015-11-10 09:12:20
【问题描述】:

从 http 服务器获取图像。将图像大小调整为缩略图,并以不同的名称“thumb”+ imgName 保存回同一文件夹。 除了再次保存到服务器之外,这一切都有效。我尝试保存到本地并且它可以工作所以服务器是问题

    Image fullsizeImage = Image.FromStream(new MemoryStream(new WebClient().DownloadData(ResolveUrl(ConfigurationManager.AppSettings["ImagesFolder"] + exportedFileName))));
    Image thumbImage = fullsizeImage.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);
 thumbImage.Save(Server.MapPath(ResolveUrl(ConfigurationManager.AppSettings["ImagesFolder"] + "thumb_" + exportedFileName)));

这不允许我将 thumbImage.Save() 返回到外部服务器 http 它标记为 'http:/localhost:61318/Uploads/thumb_6086Jellyfish.jpg' is not a valid virtual path.

所以我需要先将它传递给内存流??我试过了

string destinationFileName = ResolveUrl(ConfigurationManager.AppSettings["ImagesFolder"] + "thumb_" + exportedFileName);
 System.IO.FileStream fs = System.IO.File.Open(destinationFileName, FileMode.Create);
thumbImage.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
 fs.Close();
 //BUT THIS FLAGGED AS - uri formats are not supported

所以我需要先创建内存吗?然后将图像保存到该路径...有什么想法吗?谢谢

【问题讨论】:

    标签: c# asp.net-mvc-3


    【解决方案1】:

    您可以使用 WebImage 帮助器来完成此类工作。这比手动实现要容易。

    var photo = WebImage.GetImageFromRequest();
    if(photo != null){
      var imagePath = ConfigurationManager.AppSettings["ImagesFolder"] + exportedFileName;
      photo.Save(@"~\" + imagePath);
    
      //Let's save also a thumbnail
      var imageThumbPath = ConfigurationManager.AppSettings["ImagesFolder"] + @"thumbs\" + exportedFileName;
      photo.Resize(width: 60, height: 60, preserveAspectRatio: true, preventEnlarge: true);
      photo.Save(@"~\" + imageThumbPath);
    }
    

    您只需确保 ImagesFolder 应用程序设置是您的网络应用程序的虚拟路径。从而让 .NET 决定将其物理存储在服务器上的哪个位置。

    您可以在此处找到带有缩略图保存功能的完整教程: http://www.asp.net/web-pages/overview/ui,-layouts,-and-themes/9-working-with-images

    【讨论】:

      猜你喜欢
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      • 2015-01-24
      • 2019-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多