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

时间:2014-11-06 19:12:46

标签: c# asp.net image-resizing imageresizer

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

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;
    }
}

由于

2 个答案:

答案 0 :(得分:0)

解决:

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;
    }
}

答案 1 :(得分:0)

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

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