1

好的,我正在尝试在我的网络应用程序中使用ImageResizer 组件。我有以下代码:

var versions = new Dictionary<string, string>();
//Define the versions to generate
versions.Add("_001", "maxwidth=300&maxheight=300&format=jpg");
versions.Add("_002", "maxwidth=600&maxheight=600&format=jpg");
versions.Add("_003", "maxwidth=1920&maxheight=1080&format=jpg&process=no"); // I expect it not to resave the image if original is smaller

string uploadFolder = "...my folder path...";
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, DEFAULT_IMAGE_NAME + 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);
}

file.SaveAs(uploadFolder + DEFAULT_IMAGE_NAME + "_000.jpg");

如您所知,我正在保存一个图像 + 原始图像的 3 个版本。但是,如果需要调整大小,我只希望重新编码和重新保存图像。因此,如果我上传 1000x1000 图像,我希望 main_000.jpg 和 main_003.jpg 是相同的。但是,情况并非如此(ImageResizer 也会调整该图像的大小,并且通常保存的文件大小大于 main_000.jpg)。

我尝试添加 process=no 作为参数,但它不起作用。任何人都知道是否支持这种情况以及我需要添加哪个参数?

4

2 回答 2

2
//it may need to be improved
    Dictionary<string, SavingSettings> SaveVersions = new Dictionary<string, SavingSettings>();
    public void page_load(object sender, EventArgs e) {
        //set versions:
        SaveVersions.Add("xxl", new SavingSettings("xxl", new ImageResizer.ResizeSettings())); //original size
        SaveVersions.Add("600px", new SavingSettings("600px", new ImageResizer.ResizeSettings(600, 600, ImageResizer.FitMode.Max, "jpg"))); //big
        SaveVersions.Add("80px", new SavingSettings("80px", new ImageResizer.ResizeSettings(80, 80, ImageResizer.FitMode.Max, "jpg"))); //80 px thumb
        SaveVersions.Add("260w", new SavingSettings("260w", new ImageResizer.ResizeSettings(260, 0, ImageResizer.FitMode.Max, "jpg"))); //260 px width thumb

    }

    public void SaveIt(string SourceFile,string TargetFileName) {
        using(System.Drawing.Bitmap bmp = ImageResizer.ImageBuilder.Current.LoadImage(SourceFile, new ImageResizer.ResizeSettings())) {
            foreach(System.Collections.Generic.KeyValuePair<string, SavingSettings> k in SaveVersions) {
                string TargetFilePath = Server.MapPath("../img/" + k.Value.VersionName + "/" + TargetFileName + ".jpg");
                string TargetFolder = Server.MapPath("../img/" + k.Value.VersionName);
                if(!System.IO.Directory.Exists(TargetFolder)) System.IO.Directory.CreateDirectory(TargetFolder);
                if(bmp.Width > k.Value.ResizeSetting.Width || bmp.Height > k.Value.ResizeSetting.Height) {
                    //you may need to resize
                    ImageResizer.ImageBuilder.Current.Build(bmp, TargetFilePath, k.Value.ResizeSetting, false);
                } else {
                    //just copy it
                    //or in your example you can save uploaded file
                    System.IO.File.Copy(SourceFile, TargetFilePath);
                }
            }
        }
    }

    struct SavingSettings {
        public string VersionName;
        public ImageResizer.ResizeSettings ResizeSetting;
        public SavingSettings(string VersionName, ImageResizer.ResizeSettings ResizeSetting) {
            this.VersionName = VersionName;
            this.ResizeSetting = ResizeSetting;
        }
    }
于 2013-03-01T23:01:01.137 回答
1

您需要使用 URL API 而不是托管 API 来执行动态图像大小调整。

只需摆脱预调整大小的代码,并将上传文件保存到磁盘(但是,请确保清理文件名或使用 GUID)。

然后,像这样使用 URL API:

<img src="/uploads/original.jpg?maxwidth=300&maxheight=300&format=jpg" />
于 2013-03-01T22:58:43.673 回答