Git Product home page Git Product logo

simple.imageresizer's Introduction

Simple C# ImageResizer library using wpf classes

Demo: http://imageresizer.apphb.com/

Available on NuGet: PM> Install-Package Simple.ImageResizer

Please note that this library is not related to the http://imageresizing.net/ project

Usage:

ImageResizer.ImageResizer resizer = new ImageResizer.ImageResizer(@"c:\path\to\image.jpg");

// resize to 400 px, jpg quality 90
var byteArray1 = resizer.Resize(400, ImageEncoding.Jpg90);

// resize to 400 px, height 200, ScaleToFill, png
var byteArray2 = resizer.Resize(400, 200, true, ImageEncoding.Png);

// resize to 400 px, height 200, ScaleToFit, gif
var byteArray3 = resizer.Resize(400, 200, false, ImageEncoding.Gif);

// save last resized image to file
resizer.SaveToFile(@"c:\path\to\image_resized.gif");

Or

public ActionResult Upload(HttpPostedFileBase file)
{
      var imageResizer = new ImageResizer.ImageResizer(file.InputStream.ToByteArray());
      imageResizer.Resize(800, ImageEncoding.Jpg100);
      imageResizer.SaveToFile(Path.Combine(Server.MapPath("~/upload"), file.FileName));
      return View();
}

Now also with a MvcExtensions package making it super easy to add dynamic image resizing into your asp.net mvc site.

Step 1: PM> Install-Package Simple.ImageResizer.MvcExtensions

Step 2: Add a ImagesController. Notice the ImageResult class which inherits FileResult and lets you add a custom width and height in the constructor. Setting both height and width to 0 will skip resizing of the image. Note: Your app will need write permission to the image folder specified as it caches resized images on disk in a subfolder.

public class ImagesController : Controller
{
        [OutputCache(VaryByParam = "*", Duration = 60 * 60 * 24 * 365)]
        public ImageResult Index(string filename, int w = 0, int h = 0)
        {
            string filepath = Path.Combine(Server.MapPath("~/images2"), filename);
            return new ImageResult(filepath, w, h);
        }
}

Step 3: Add a custom route for images allowing requests to images to be handled by our controller action.

routes.MapRoute(
        "Image", "images/{filename}",
        new { controller = "Images", action = "Index", filename = "" }
);

Step 4: Add the following to web.config in the system.webserver section to allow calls to static files to be handled by the mvc pipeline.

<modules runAllManagedModulesForAllRequests="true" />

simple.imageresizer's People

Contributors

terjetyl avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.