.netasp.net-mvcimageasp.net-core-webapiimageresizer

ImageResizer.ImageBuilder giving error of Could not load type 'System.Web.Hosting.HostingEnvironment' .Net core MVC 6


I am trying to resizing my image if its extension is among .jpg, .jpeg, .png or .webp. below is my code

if (System.IO.Path.GetExtension(newFileName).ToLower() == ".jpg" || System.IO.Path.GetExtension(newFileName).ToLower() == ".png" || System.IO.Path.GetExtension(newFileName).ToLower() == ".jpeg" || System.IO.Path.GetExtension(newFileName).ToLower() == ".webp")
                {
                    var path = Path.Combine(_webHostEnvironment.WebRootPath, CMSUserAreaDir, "content_media/raw/" + newFileName);
                    var image = new ImageResizer.ImageJob(file, path, new ImageResizer.Instructions("maxwidth=1920"), false, false);
                    string a = ImageResizer.ImageBuilder.Current.Build(image).FinalPath;
                }

and I'm getting this error enter image description here Anyone please help me to know what I'm doing wrong. Thanks in advance.

I am not sure if I am missing any packages in my setup for this to happen, or I am not supposed to use ImageJob in this manner.

When I'm looking at packages for ImageResizer its shows message like this enter image description hereenter image description here


Solution

  • above issue is resolved. note that ImageResizer is not compatible with .net 6 yet, maybe it will in future. Till then there are many alternatives available, I've used SixLabors.Imagesharp to resolved my issue. here is updated code :

    var path = Path.Combine(_webHostEnvironment.WebRootPath, CMSUserAreaDir, "content_media/raw/" + newFileName);
                    //var image = new ImageResizer.ImageJob(file, path, new ImageResizer.Instructions("maxwidth=1920"), false, false);
                    //string a = ImageResizer.ImageBuilder.Current.Build(image).FinalPath;
                    using (var image = Image.Load(file.OpenReadStream()))
                    {
                        if (image.Width > 1920)
                        {
                            image.Mutate(x => x.Resize(new ResizeOptions
                            {
                                Size = new Size(1920, 0),
                                Mode = ResizeMode.Max
                            }));
                        }
    
                        image.Save(path);
                    }
    

    you need to add these packages:

    <PackageReference Include="SixLabors.ImageSharp" Version="3.0.0" />