Я пытаюсь изменить размер каждого изображения в папке. Я пытался найти способ указать папку, и я мог бы изменить размер каждого изображения внутри нее, возможно, используя плагин (что-то вроде 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. Это о применении водяного знака ко всем изображениям в папке с определенным размером, но приложение для каждой папки одинаково, и, вероятно, для этого потребуется меньше кода, чем приведенный выше ответ.
http://imageresizing.net/docs/howto/watermark-by-folder-or-size
Решено:
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;
}
}