Search code examples
c#.netasp.net-mvcimageresizer

ImageResizer worked in ConsoleApp, now does not work in ASP.NET


I have been using ImageResizer (https://imageresizing.net/) to resize Images. I wrote a console app that was resizing images in a specified directory on my disk.

The console app was working fine, but then i added my project to a ASP.NET MVC app, and it doesnt work. I didnt just move the code, i moved the whole project and just call a start method i created that is the same as my previous main from console app. I did install resizer from NuGet in my WebApp.

When my code gets to the point where i resize images, i get an error "Request is not available in this context" This is the part where i try to resize my image:

var resize = new ResizeSettings(resizeParameters);
ImageBuilder.Current.Build(inFile, outFile, resize);

This is the stackTrace output.

at System.Web.HttpContext.get_Request()
at ImageResizer.Configuration.Performance.GlobalPerf.JobComplete(ImageBuilder builder, ImageJob job)
at ImageResizer.ImageBuilder.BuildInternal(ImageJob job)
at ImageResizer.ImageBuilder.BuildInQueue(ImageJob job, Boolean useSemaphore, Int32 maxQueuingMilliseconds, CancellationToken cancel)
at ImageResizer.ImageBuilder.Build(ImageJob job)
at ImageResizer.ImageBuilder.Build(Object source, Object dest, ResizeSettings settings, Boolean disposeSource, Boolean addFileExtension)
at ImageResizer.ImageBuilder.Build(Object source, Object dest, ResizeSettings settings, Boolean disposeSource)
at ImageResizer.ImageBuilder.Build(Object source, Object dest, ResizeSettings settings)
at ImageOptimizerCode.ResizeAndOptimize.ResizeImage(String inFile, String outFile, String resizeParameters) in
E:\\Vermis\\ImageOptimizer\\ImageOptimizerCode\\ResizeAndOptimize.cs:line
176

If someone can point me in the right direction, on what i am doing wrong here that would be great.


Solution

  • Because you have to start another project (Here is MVC) you have to configure the plugin for the project. So make sure plugin configured to be start up via:

    A. add <add name="PluginName" /> in <plugins /> section of web.config file

    OR

    B. In Application_Start method of Global.asax create an instance of the plugin

        new PluginName().Install(ImageResizer.Configuration.Config.Current);
    

    There are some code explain of how to use this plugin:

    Image Resize In ASP.NET MVC Using Image Resizer

    ImageResizer enables clean, clear image resizing in ASP.NET