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

C# ImageResizer-调整文件夹中所有图像的大小,c#,asp.net,image-resizing,imageresizer,C#,Asp.net,Image Resizing,Imageresizer,我正在尝试重新调整文件夹中每个图像的大小。我试图找到一种方法来指定文件夹,我可以重新调整其中的每个图像的大小,可能使用插件(类似于FolderResizeSyntax,尽管我还不能100%确定它是如何工作的)。另一种方法是循环我的SQL server表,获取文件路径,使用该文件路径在我的计算机上打开文件,然后重新调整其大小。后者似乎效率不高。目前的尺码代码如下所示。如果您能帮助调整文件夹中所有图像的大小,我们将不胜感激 Dictionary<string, string> versi

我正在尝试重新调整文件夹中每个图像的大小。我试图找到一种方法来指定文件夹,我可以重新调整其中的每个图像的大小,可能使用插件(类似于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;
    }
}
字典版本=新字典();
//定义要生成的版本
版本。添加(“_原件”,”)//原始图像
版本。添加(“_1000”,“宽度=1000,高度=1000,裁剪=自动”)//适用于1000x1000区域
版本。添加(“_500”,“宽度=500,高度=500,裁剪=自动”)//适合500x500区域
版本。添加(“_250”,“宽度=250,高度=250,裁剪=自动”)//安装到250x250区域
//循环浏览每个上传的文件
foreach(HttpContext.Current.Request.Files.Keys中的字符串fileKey)
{
//生成每个版本
foreach(versions.Keys中的字符串后缀)
{
HttpPostedFile=HttpContext.Current.Request.Files[fileKey];
如果(file.ContentLength已解决:

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;
    }
}
字典版本=新字典();
//定义要生成的版本
versions.Add(“_Raw”,”);//原始图像
版本。添加(“_1000x1000”,“宽度=1000&高度=1000&裁剪=自动”);//适合1000x1000区域
版本。添加(“_500x500”,“宽度=500&高度=500&裁剪=自动”);//适合500x500区域
字符串[]路径2=目录.GetFiles(@“C:\myPictures\TestImages”);
//生成每个版本
foreach(路径2中的字符串dirFile)
{
foreach(versions.Keys中的字符串后缀)
{
//根据文件类型创建目录/路径(如原始、1000等)
string uploadFolder=MapPath(“~/TestImages/”+后缀。替换(“,”);
//获取uploads文件夹的物理路径,并确保它存在
如果(!Directory.Exists(uploadFolder))Directory.CreateDirectory(uploadFolder);
////生成一个文件名。
字符串filePath=Path.GetFileName(dirFile);
字符串fileName=Path.Combine(上传文件夹,文件路径+后缀);
//让图像生成器根据输出文件类型添加正确的扩展名
fileName=ImageBuilder.Current.Build(新的ImageJob(dirFile,fileName,新指令(版本[后缀]),false,true)).FinalPath;
}
}

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


您是否计划在网站中使用不同大小的文件?ImageResizer的优点在于,它可以动态创建不同大小的图像,然后缓存它们,以使常用的图像更快。ex>这样您就不需要手动调整每个文件的大小。您可以保留较大的图像,ImageResizer可以生成任何大小的图像正在运行中。如果这是一次性的,我建议使用为此而构建的程序,比如我不知道可以使用多少次。我有一个包含图像的文件夹,我可能希望将所有图像调整为300x300,然后将其放入新文件夹或250x200,然后将其放入其他新文件夹中,以备将来使用。您可以选择在您的网站上安装e/ImageResizer,然后保留原始文件,这些文件可以动态地重新调整到所需的任何大小,或者您希望使用类似的程序来进行此转换,然后您就不需要resizer。以下链接指向帮助我实现正确使用ImageResizer方法的网站