I want to resize an image and save this image multiple times with different sizes into a folder. I have tried ImageResizer or CoreCompat.System.Drawing but these libraries not compatible with .Net core 2. I have searched a lot of about this but i can't find any proper solution. like in MVC4 i have used as:
public ActionResult Upload(HttpPostedFileBase file)
{
if (file != null)
{
var versions = new Dictionary<string, string>();
var path = Server.MapPath("~/Images/");
//Define the versions to generate
versions.Add("_small", "maxwidth=600&maxheight=600&format=jpg";);
versions.Add("_medium", "maxwidth=900&maxheight=900&format=jpg");
versions.Add("_large", "maxwidth=1200&maxheight=1200&format=jpg");
//Generate each version
foreach (var suffix in versions.Keys)
{
file.InputStream.Seek(0, SeekOrigin.Begin);
//Let the image builder add the correct extension based on the output file type
ImageBuilder.Current.Build(
new ImageJob(
file.InputStream,
path + file.FileName + suffix,
new Instructions(versions[suffix]),
false,
true));
}
}
return RedirectToAction("Index");
}
but in Asp.Net core 2.0 i am stuck. i have no idea how can i implement this in .Net core 2. Any one please can help me.
Resizing an Image can be done in a range of ways. The easiest method is to create a new Bitmap object from the in-memory image. When creating the Bitmap object, you assign the new dimension in the Size parameter. Finally, the resized Bitmap is written to a byte array.
.NET Core 2.0 ships with System.Drawing.Common, which is the official implementation of System.Drawing for .NET Core.
Instead of CoreCompat.System.Drawing, can you try to install System.Drawing.Common and check whether that works?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With