I am obtaining the directory of the temp or tmp folder, and creating a folder called "TemporaryImages" from it, which as the name says, I am only going to use it to save images with low resolution temporarily.
The problem is that "TemporaryImages" is being created successfully, but the images are not being saved within it, and I don't understand where I'm screwing up.
I'm using C# ASP.NET, on a Linux server.
This code receives a list of images fetched from a database. With "TemporalFolder()" I create or get the directory of the temporary folder, and with the function "SaveImage()" I am sending it the name of the image, the directory of the folder, and the new size of the image.
private void ChangeImageSize(List<OrderViewModel> orderViewModels)
{
const int thumbnailWidth = 500;
string tempFolder;
try
{
tempFolder = TemporaryFolder();
foreach (var item in orderViewModels)
{
var imageResult = Image.Load("wwwroot/" + item.getImages());
this.SaveImage(imageResult, tempFolder, thumbnailWidth);
}
}
catch (Exception ex)
{
ViewBag.error = ex.Message.ToString();
throw new Exception(ex.Message);
}
}
With this function I seek to save the image inside the temporary folder:
private void SaveImage(Image image, string name, int resizeWidth)
{
var width = image.Width;
var height = image.Height;
if (width > resizeWidth)
{
height = (int)((double)resizeWidth / width * height);
width = resizeWidth;
}
picture
.Mutate(i => i.Resize(new Size(width, height)));
image.Metadata.ExifProfile = null;
image.SaveAsJpeg(name, new JpegEncoder
{
Quality = 100
});
}
This is the function to create and get the temporary folder:
private string TemporaryFolder()
{
DirectoryInfo result = Directory.CreateDirectory(Path.GetTempPath() + "TemporaryImages/");
return result.FullName;
}