C'è un modo semplice e dinamico per creare miniature e ridimensionare le immagini in MVC3/Razor? Un aiuto, una liberia, qualcosa?
È possibile utilizzare l'assembly System.Drawing incorporato e la classe Image per ottenere ciò. È possibile scrivere un'azione del controllore che verrà passata come argomenti il nome dell'immagine e la nuova dimensione desiderata e questa azione del controller eseguirà il ridimensionamento e restituirà la nuova immagine.
Ad esempio:
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");
}
}
Ora andare avanti per inserire questa azione a suo parere:
<img src="@Url.Action("Thumbnail", "SomeController", new { width = 100, height = 50 })" alt="thumb" />
fonte
2011-09-06 12:24:01
lo proverò. E buon tempismo. Perché presto avrò bisogno di uno buono. –
Grande. L'ho aggiunto solo oggi ma puoi trovare il sito di esempio mvc 4 nel repository github se hai problemi: https://github.com/terjetyl/Simple.ImageResizer – terjetyl
Grazie amico .... –