【问题标题】:Image resize asp.net mvc application图像大小调整 asp.net mvc 应用程序
【发布时间】:2012-03-09 19:59:47
【问题描述】:

编辑后添加了链接

在 asp.net mvc [这里][1] 阅读关于图像大小调整的非常好的帖子。

http://dotnetslackers.com/articles/aspnet/Testing-Inbound-Routes.aspx

我也需要这个逻辑来处理在 cdn 中上传的图像。例如,我已经在 cdn 中上传了一个图像,现在我想从我的控制器中获取它并调整它的大小。此外,图像不应该被保存在我的服务器中,因为这不是一个好主意,因为它会消耗宝贵的资源。必须从 CDN 读取图像并调整其大小,而无需将其本地保存在服务器中。我们如何使用上述帖子中给出的方法来实现这一点。

谢谢, S.

【问题讨论】:

  • 您缺少该帖子的链接。可以加吗?

标签: c# asp.net-mvc file file-io


【解决方案1】:

如果你使用 ASP.Net MVC3,你可以试试新的 helper - WebImage。

这是我的测试代码。

    public ActionResult GetImg(float rate)
    {
        WebClient client = new WebClient();
        byte[] imgContent = client.DownloadData("ImgUrl");
        WebImage img = new WebImage(imgContent);
        img.Resize((int)(img.Width * rate), (int)(img.Height * rate));
        img.Write();

        return null;
    }

【讨论】:

    【解决方案2】:

    您可以在 System.Drawing 命名空间中使用 GDI+ 功能

    Bitmap newBitmap = new Bitmap(destWidth, destHeight);
    Graphics g = Graphics.FromImage((Image)newBitmap);
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    
    g.DrawImage(sourceImage, 0, 0, destWidth, destHeight);
    g.Dispose();
    

    【讨论】:

      【解决方案3】:

      这是我使用的。效果很好。

          private static Image ResizeImage(Image imgToResize, Size size)
          {
              int sourceWidth = imgToResize.Width;
              int sourceHeight = imgToResize.Height;
      
              float nPercent = 0;
              float nPercentW = 0;
              float nPercentH = 0;
      
              nPercentW = ((float)size.Width / (float)sourceWidth);
              nPercentH = ((float)size.Height / (float)sourceHeight);
      
              if (nPercentH < nPercentW)
                  nPercent = nPercentH;
              else
                  nPercent = nPercentW;
      
              int destWidth = (int)(sourceWidth * nPercent);
              int destHeight = (int)(sourceHeight * nPercent);
      
              Bitmap b = new Bitmap(destWidth, destHeight);
              Graphics g = Graphics.FromImage((Image)b);
              g.InterpolationMode = InterpolationMode.HighQualityBicubic;
      
              g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
              g.Dispose();
      
              return (Image)b;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-12
        • 1970-01-01
        • 2014-02-17
        • 1970-01-01
        相关资源
        最近更新 更多