ImageResizer-将图像另存为较大大小

本文关键字:另存为 图像 ImageResizer- | 更新日期: 2023-09-27 18:28:59

我正在尝试使用.net ImageResizer将图像保存到指定的大小。我无法使用当前使用的代码将较小的图像保存为较大的尺寸。例如,我上传了一个200x200的图像,_thumb版本将保存为100x100,但_mediam和_large版本仍然是200x200。

如何将上传的图像保存到指定的较大图像?例如版本。添加("_extraLarge","width=2000&height=2000&crop=auto&format.jpg");或版本。添加("_XXL","width=auto&height=3000&crop=auto&aamp;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;
    }
}

ImageResizer-将图像另存为较大大小

默认情况下,缩放模式设置为DownscaleOnly。根据需要,将说明中的Scale设置为upboth

请注意,放大图像往往会产生模糊的图像。