I have a gallery of images that are loaded dynamically from the database. The original images weigh approximately 3Mb each, so I am making a Thumbnail of each one so I can optimize my web page. Thanks to the thumbnail, the images now weigh approximately between 300kb and 500kb.
The images when I get them from the database, I save them in a list and this is passed through the thumbnail function, and then sent to the view.
My problem is the following: I want each image to be downloadable either all together or one by one. I have been able to complete this task successfully, but the images that are downloaded are the thumbnails, when the ones I want to download are the originals.
How could I load the thumbnail images in the gallery and download the original ones?
I have tried to save the copies in a folder both in TMP/Temp, create a new directory such as wwwroot/TemporalImages, or save them in memory. The problem with the folders is that I don't have write permissions. And with saving the images in memory I did not find something that worked, since most of them I saw use Bitmap or Encoder but I get a warning that they are only functional for Windows, and my project is hosted on a Linux server.
I need your help, I've had this problem for a few days and I can't find a way around it.
// I recover the images with the Query, then pass the list to "Chang Image Size"
private List<OrderViewModel> ViewOrder(string identi)
{
List<OrderViewModel> OrderList;
string query;
try
{
OrderList = new List<OrderViewModel>();
query = "query";
OrderList = SelectMySqlConnection(query);
ChangeImageSize(orderList);
return OrderList;
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
// I create the temporary folder (tempFolder), do a foreach loop for the List and save each 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);
}
}
//Resize the image and then save it
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;
}
image
.Mutate(i => i.Resize(new Size(width, height)));
image.Metadata.ExifProfile = null;
image.SaveAsJpeg(name, new JpegEncoder
{
Quality = 100
});
}
//Create or recover temporalFolder
private string TemporalFolder()
{
DirectoryInfo result = Directory.CreateDirectory(@"wwwroot/imgAndroid/TemporalImages/");
return result.FullName;
}
In addition to all that, when I try it locally or in production, I have this error:
System.Exception: Access to the path 'C:\Users\Users\source\repos\DevelopmentWebLogistica\WebLogistica\wwwroot\TemporalImages' is denied. at WebLogistica.Controllers.HomeController.ChangeImageSize(List`1 orderViewModels) in C:\Users\Users\source\repos\DevelopmentWebLogistica\WebLogistica\Controllers\HomeController.cs:line 289 at WebLogistica.Controllers.HomeController.VerOrder(String identi) in C:\Users\Users\source\repos\DevelopmentWebLogistics\WebLogistics\Controllers\HomeController.cs:line 157
Line 157 is ChangeImageSize(orderList);