MVC3/Razor thumbnail/resize image ideas?

You could use the built-in System. Drawing assembly and the Image class to achieve this. You may write a controller action which would be passed as arguments the image name and the desired new size and this controller action would perform the resize and return the new image For example: 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. Graphics.

PixelOffsetMode = PixelOffsetMode. DrawImage(srcImage, new Rectangle(0, 0, width, height)); newImage. Save(stream, ImageFormat.

Png); return File(stream.ToArray(), "image/png"); } } Now go ahead and include this action in your view: img src="@Url. Action("Thumbnail", "SomeController", new { width = 100, height = 50 })" alt="thumb.

You could use the built-in System. Drawing assembly and the Image class to achieve this. You may write a controller action which would be passed as arguments the image name and the desired new size and this controller action would perform the resize and return the new image.

For example: 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.

Graphics. PixelOffsetMode = PixelOffsetMode. DrawImage(srcImage, new Rectangle(0, 0, width, height)); newImage.

Save(stream, ImageFormat. Png); return File(stream.ToArray(), "image/png"); } } Now go ahead and include this action in your view.

Clean and simple. Thanks! – Kasper Skov Sep 6 at 12:34 Youve used Url.Action.

Shouldnt it me Html. Action? – Kasper Skov Sep 7 at 10:55 1 @Kasper Skov, no, I don't want to render the execution of a given action, I want to get the url of this action, so the correct is to use Url.Action.To read more about Html.

Action you may take a look at the following blog post: haacked. Com/archive/2009/11/18/aspnetmvc2-render-action. Aspx – Darin Dimitrov Sep 7 at 10:58 Never mind.

Its Url. Thats why: OutputStream is not available when a custom TextWriter is used. – Kasper Skov Sep 7 at 11:00.

There's a library for it - it's MVC3 compatible, and it's implemented as an HttpModule, so it gets great performance. It's also free (although some plugins require a 1-time developer or business license). You can download it at imageresizing.net Although it's tempting to just write an action for it, there are a lot of GDI bugs that you'll have to deal with, one, by one, over the years.

Using a library frees you from tracking and avoiding them. Google "Image resizing pitfalls", the first result is an article that will help if you write your own decoding/resizing/encoding system.

Sweet! Thanks!........ – Kasper Skov Sep 22 at 17:42.

It would be nice, if I somehow could manage the size of the images from the controller. Or even in razorview. Example: In the index view I want the images to be a certain size, but in the details view I want them to be full size.

I know this question is vague, but I really couldnt find anything on google/stackoverflow other than old mvc1 thingos. How do you guys normally deal with this?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions