ImageResizer-调整文件夹中所有图像的大小

本文关键字:图像 调整 文件夹 ImageResizer- | 更新日期: 2023-09-27 18:29:31

我正在尝试重新调整文件夹中每个图像的大小。我试图找到一种方法来指定文件夹,我可以重新调整其中的每个图像的大小,可能使用插件(类似FolderResizeSyntax的东西,尽管我还不能100%确定它是如何工作的)。另一种方法是循环浏览SQL server表,获取文件路径,使用该文件路径在我的计算机上打开文件,然后重新调整大小。后者似乎效率不高。当前重新调整大小的代码发布在下面。如有关于如何重新调整文件夹中所有图像大小的帮助,不胜感激。

Dictionary<string, string> versions = new Dictionary<string, string>();
//Define the versions to generate
versions.Add("_Original", ""); //Original Image
versions.Add("_1000", "width=1000&height=1000&crop=auto"); //Fit to 1000x1000 area
versions.Add("_500", "width=500&height=500&crop=auto"); //Fit to 500x500 area
versions.Add("_250", "width=250&height=250&crop=auto"); //Fit to 250x250 area
//Loop through each uploaded file
foreach (string fileKey in HttpContext.Current.Request.Files.Keys)
{
    //Generate each version
    foreach (string suffix in versions.Keys)
    {
        HttpPostedFile file = HttpContext.Current.Request.Files[fileKey];
        if (file.ContentLength <= 0) continue; //Skip unused file controls.
        //Create directory/path based on file type (ex. _Raw, _1000, etc.)
        string uploadFolder = MapPath("~/myImages/" + suffix);
        //Get the physical path for the uploads folder and make sure it exists
        if (!Directory.Exists(uploadFolder)) Directory.CreateDirectory(uploadFolder);
        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(new ImageJob(file, fileName, new Instructions(versions[suffix]), false, true)).FinalPath;
    }
}

感谢

ImageResizer-调整文件夹中所有图像的大小

已解决:

Dictionary<string, string> versions = new Dictionary<string, string>();
//Define the versions to generate
versions.Add("_Raw", ""); //Original Image
versions.Add("_1000x1000", "width=1000&height=1000&crop=auto"); //Fit to 1000x1000 area
versions.Add("_500x500", "width=500&height=500&crop=auto"); //Fit to 500x500 area
string[] path2 = Directory.GetFiles(@"C:'myPictures'TestImages");
//Generate each version
foreach (string dirFile in path2)
{
    foreach (string suffix in versions.Keys)
    {
        //Create directory/path based on file type (ex. _Raw, _1000, etc.)
        string uploadFolder = MapPath("~/TestImages/" + suffix.Replace("_", ""));
        //Get the physical path for the uploads folder and make sure it exists
        if (!Directory.Exists(uploadFolder)) Directory.CreateDirectory(uploadFolder);
        ////Generate a filename.
        string filePath = Path.GetFileName(dirFile);
        string fileName = Path.Combine(uploadFolder, filePath + suffix);
        //Let the image builder add the correct extension based on the output file type
        fileName = ImageBuilder.Current.Build(new ImageJob(dirFile, fileName, new Instructions(versions[suffix]), false, true)).FinalPath;
    }
}

您可能还应该在ImageResizer的网站上查看此页面。这是关于将水印应用于文件夹中特定大小的所有图像,但每个文件夹的应用程序是相同的,可能需要比上面的答案更少的代码。

http://imageresizing.net/docs/howto/watermark-by-folder-or-size