It's my first time resizing images in asp.net core, so after a bit of research I found this approach to be the easiest and most efficient and I implemented it as shown below. However, I am not sure if that approach is the most efficient one since there are two issues with it that go as follows
- Images lose a lot of their quality
- I get this 'warning' in Visual Studio that is fine as long as I am the only one who develops it, however that will not always be the case if someone else does -
This call site is reachable on all platforms. 'Bitmap' is only supported on windows.
Therefore I wonder what are other approaches more decent that exist and I can implement to at least fix the first of both issues.
public async Task<IActionResult> Add(AddCardFormModel card, List<IFormFile> ImageFile)
{
...
foreach (var image in ImageFile)
{
if (image.Length > 0 || image.Length <= (2 * 1024 * 1024))
{
var imagesToBeResized = Image.FromStream(image.OpenReadStream());
var resized = new Bitmap(imagesToBeResized, new Size(250, 350));
using (var stream = new MemoryStream())
{
resized.Save(stream, ImageFormat.Jpeg);
var cardData = new Card
{
Title = card.Title,
Description = card.Description,
ImageUrl = card.ImageUrl,
CategoryId = card.CategoryId,
ConditionId = card.ConditionId,
Price = card.Price,
DealerId = dealerId,
Image = stream.ToArray()
};
this.data.Cards.Add(cardData);
this.data.SaveChanges();
}
}
}
...
}
CodePudding user response:
Check this library for image resizing.
but I suggest don't store images on the database, because the database size and log size will be increased.
you have 2 options:
Option 1: store resized images on directories and save names in the database.
Option 2: store the original size on directories and resize it in real-time.
CodePudding user response:
The System.Drawing.Common
package is only supported on Windows.
If you want your application to work cross-platform then Microsoft suggests to migrate to one of the following libraries:
You should first decide which image processing library you will use in your project and after that look for the best algorithm for resizing images using that specific library.