'Uploading image as thumbnail asp .net core web api
I'm made a post api that takes an image and upload it as original version in a folder called images, and as a thumbnail image in folder called thumbnail, I'm working with Bitmap to resize the image resolution, but when I tried my api, the two versions of the same uploaded image were the same size, so it seems Bitmap isn't resizing my image, here is my code:
[HttpPost, DisableRequestSizeLimit]
[Route("add-image/{id}")]
public async Task<IActionResult> uploadImage([FromRoute]long id)
{
try
{
var formCollection = await Request.ReadFormAsync();
var file = formCollection.Files.First();
var folderName = Path.Combine("Resources", "Images");
var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);
if (file.Length > 0)
{
var fileName = DateTime.Now.Second.ToString().Trim(' ', '/')+ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
var fullPath = Path.Combine(pathToSave, fileName);
var dbPath = Path.Combine(folderName, fileName);
using (var stream = new FileStream(fullPath, FileMode.Create))
{
file.CopyTo(stream);
}
var ThumbFolder = Path.Combine("Resources", "Thumbnails");
var pathToSaveThumb = Path.Combine(Directory.GetCurrentDirectory(), ThumbFolder);
var ThumbFilName = $"thumb{fileName}";
var fullPathThumb = Path.Combine(pathToSaveThumb, ThumbFilName);
var DbThumbpath = Path.Combine(ThumbFolder, ThumbFilName);
Image img = Image.FromFile(fullPath);
var newWidth = img.Width / 10;
var newHeight = img.Height / 10;
Bitmap bmp = new Bitmap(img);
var graphic = Graphics.FromImage(bmp);
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
graphic.DrawImage(img, 0, 0, 100, 50);
bmp.Save(fullPathThumb);
var imageToSave = bmp;
ClientImage dbImage = new ClientImage()
{
ClientId = id,
ImageUrl = dbPath
};
await _trainingContext.AddAsync(dbImage);
await _trainingContext.SaveChangesAsync();
return Ok(dbImage.ImageUrl);
}
else
{
return BadRequest();
}
}
catch (Exception e)
{
return StatusCode(500, $"Internal Server Error {e.Message}");
}
}
What am I doing wrong?
any answer will be very appreciated, Thank you!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
