asp.net-mvc-3 - MVC3/Razor 缩略图/调整图像大小的想法?

标签 asp.net-mvc-3 image razor thumbnails image-resizing

在 MVC3/Razor 中是否有一种简单而动态的方法来创建缩略图和调整图像大小? helper ,图书馆,什么的?

如果我能以某种方式从 Controller 管理图像的大小,那就太好了。甚至在 Razor View 中。示例:在索引 View 中,我希望图像具有特定大小,但在详细信息 View 中,我希望它们是完整大小。

我知道这个问题很模糊,但除了旧的 mvc1 thingos 之外,我真的在 google/stackoverflow 上找不到任何东西。

大家平时都是怎么处理的?

最佳答案

Is there an easy and dynamic way to create thumbnails and resize images in MVC3/Razor? A helper, libary, anything?



您可以使用内置的 System.Drawing 程序集和 Image 类来实现这一点。您可以编写一个 Controller 操作,该操作将作为参数传递图像名称和所需的新大小,并且此 Controller 操作将执行调整大小并返回新图像。

例如:
public ActionResult Thumbnail(int width, int height)
{
    // TODO: the filename could be passed as argument of course
    var imageFile = Path.Combine(Server.MapPath("~/app_data"), "test.png");
    using (var srcImage = Image.FromFile(imageFile))
    using (var newImage = new Bitmap(width, height))
    using (var graphics = Graphics.FromImage(newImage))
    using (var stream = new MemoryStream())
    {
        graphics.SmoothingMode = SmoothingMode.AntiAlias;
        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
        graphics.DrawImage(srcImage, new Rectangle(0, 0, width, height));
        newImage.Save(stream, ImageFormat.Png);
        return File(stream.ToArray(), "image/png");
    }
}

现在继续并在您的 View 中包含此操作:
<img src="@Url.Action("Thumbnail", "SomeController", new { width = 100, height = 50 })" alt="thumb" />

关于asp.net-mvc-3 - MVC3/Razor 缩略图/调整图像大小的想法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7319842/

相关文章:

c# - 在 C# razor 中使用@event

asp.net-mvc-3 - 什么是 ASP.NET MVC 中服务层的好例子?

Jquery从MVC url获取参数Id?

ios - UIButton 中图像下的标签

python - 将一个图像粘贴到另一个图像上

c# - Func<int,string> 从 Url.Action 返回

c# - 如何在 asp.net mvc3 中进行搜索

asp.net-mvc - Razor 嵌套 WebGrid

Python:读写TIFF 16位,三 channel ,彩色图像

c# - 在 View 中显示 Tuple<int, int> 值