【问题标题】:ImageResizer - Resize all Images within a FolderImageResizer - 调整文件夹中所有图像的大小
【发布时间】:2014-11-06 19:12:45
【问题描述】:

我正在尝试重新调整文件夹中每个图像的大小。我试图找到一种方法来指定文件夹,我可以重新调整其中的每个图像的大小,可能使用一个插件(类似于 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;
    }
}

谢谢

【问题讨论】:

  • 您是否打算在网站中使用不同的文件大小? ImageResizer 的美妙之处在于它会动态创建不同的图像大小,然后缓存它们,以便常用的图像快速。 ex> somewebsite/Content/myimage?width=100 这样您就不需要手动调整每个文件的大小。您保留大图像,ImageResizer 会即时生成所需的任何大小的图像。
  • 如果这是一次性的事情,我建议使用为此构建的程序,例如 lifehacker.com/5147153/…
  • 我不知道我可以使用多少次。我有一个包含图像的文件夹,我可能想将它们全部调整为 300x300 并将它们放在一个新文件夹或 250x200 并将它们放在另一个新文件夹中以备将来使用。
  • 您要么想要在您的网站上使用/安装 ImageResizer,然后保留原始文件,这些文件可以即时调整为所需的任何大小,或者您想要使用类似于lifehacker.com/5147153/… 的程序将执行此转换,然后您不需要调整大小。以下链接是一个网站,帮助我实现了ImageResizer的正确使用方法hanselman.com/blog/…

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


【解决方案1】:

已解决:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-02-26
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-10
      • 1970-01-01
      相关资源
      最近更新 更多