Determining the current image size using ImageResizer.net

We recently started using ImageResizer.Net through GDI + to dynamically resize images in our ASP.NET MVC 4 application.

Is there a way, using only ImageResizer, to determine the actual resolution (DPI, PPI, whatever you want to name), the image (which is read as an array of bytes). We currently have such a workflow that, if necessary, we can resize the image to a given lower resolution:

//pseudo-code
var image = (Bitmap)Bitmap.FromStream(contentStream)
var resX = image.HorizontalResolution;
var resY = image.VerticalResolution;
//calculate scale factor
//determine newHeight and newWidth from scale
var settings = new ResizeSettings("width={newWidth}&height={newHeight}")
var newImage = ImageBuilder.Current.Build(image, someNewImage, settings);

This works great, but its mixing GDI + and ImageResizer and has a lot of threads opening and closing the same data (the actual code is a bit more verbose, with many operators using).

Is there a way to determine horizontal and vertical resolution using only ImageResizer? I could not immediately find anything in the documentation.

At the moment, we used a managed api, but in the end we will use MVC routing.

+5
source share
2 answers

This is a rather atypical scenario - typically incoming DPI values ​​are useless.

However, since you seem to be managing these values ​​and need them to perform calibration calculations, I suggest a plugin. They are lightweight and offer perfect performance since you are not duplicating efforts.

public class CustomSizing:BuilderExtension, IPlugin {

    public CustomSizing() { }

    public IPlugin Install(Configuration.Config c) {
        c.Plugins.add_plugin(this);
        return this;
    }

    public bool Uninstall(Configuration.Config c) {
        c.Plugins.remove_plugin(this);
        return true;
    }
    //Executes right after the bitmap has been loaded and rotated/paged
    protected override RequestedAction PostPrepareSourceBitmap(ImageState s) {
        //I suggest only activating this logic if you get a particular querystring command.
        if (!"true".Equals(s.settings["customsizing"], 
            StringComparison.OrdinalIgnoreCase)) return RequestedAction.None;

        //s.sourceBitmap.HorizontalResolution
        //s.sourceBitmap.VerticalResolution

        //Set output pixel dimensions and fit mode
        //s.settings.Width = X;
        //s.settings.Height = Y;
        //s.settings.Mode = FitMode.Max;

        //Set output res.
        //s.settings["dpi"] = "96";
        return RequestedAction.None;
    }
 }

Installation can be done using code or via Web.Config .

new CustomSize (). Install (Config.Current);

:

   <plugins>
     <add name="MyNamespace.CustomSizing" />
   </plugins>
+3

All Articles