BrettKB
BrettKB

Reputation: 205

ImageResizer - Save Image as a Larger Size

I am trying to use the .net ImageResizer to save an image to a specified size. I am unable to save a smaller image to larger dimensions with the current code I am using. For example, I upload an image that is 200x200, the _thumb version will be saved as 100x100 but the _medium and _large versions will still be 200x200.

How can I save the uploaded image to the specified larger images? Such as versions.Add("_extraLarge", "width=2000&height=2000&crop=auto&format=jpg"); or versions.Add("_XXL", "width=auto&height=3000&crop=auto&format=jpg");

//GenerateVersions(FileUpload1.PostedFile.FileName);
Dictionary<string, string> versions = new Dictionary<string, string>();
//Define the versions to generate
versions.Add("_thumb", "width=100&height=100&crop=auto&format=jpg"); //Crop to square thumbnail
versions.Add("_medium", "maxwidth=400&maxheight=400&format=jpg"); //Fit inside 400x400 area, jpeg
versions.Add("_large", "maxwidth=1900&maxheight=1900&format=jpg"); //Fit inside 1900x1200 area

//Loop through each uploaded file
foreach (string fileKey in HttpContext.Current.Request.Files.Keys)
{
    HttpPostedFile file = HttpContext.Current.Request.Files[fileKey];
    if (file.ContentLength <= 0) continue; //Skip unused file controls.

    //Get the physical path for the uploads folder and make sure it exists
    string uploadFolder = MapPath("~/uploadsAIM");
    if (!Directory.Exists(uploadFolder)) Directory.CreateDirectory(uploadFolder);

    //Generate each version
    foreach (string suffix in versions.Keys)
    {
        ////Generate a filename (GUIDs are best).
        //string fileName = Path.Combine(uploadFolder, System.Guid.NewGuid().ToString() + suffix);
        string fileName = Path.Combine(uploadFolder, file.FileName + suffix);

        //Let the image builder add the correct extension based on the output file type
        //fileName = ImageBuilder.Current.Build(file, fileName, new ResizeSettings(versions[suffix]), false, true); //deprecated fileName
        fileName = ImageBuilder.Current.Build(new ImageJob(file, fileName, new Instructions(versions[suffix]), false, true)).FinalPath;
    }
}

Upvotes: 0

Views: 684

Answers (1)

John Koerner
John Koerner

Reputation: 38079

By default the scaling mode is set to DownscaleOnly. Set the Scale on the instructions to either up or both depending on your need.

Just note that upscaling an image tends to create blurry images.

Upvotes: 2

Related Questions