Could not load type 'ImageProcessor.Imaging.CropLayer' from assembly 'ImageProcessor'

296 Views Asked by At

I'm working on a web-based image processing tool using cropper.js and ImageProcessor. I have an AJAX call that passes the cropper.js data to a C# handler, where that data is then passed to ImageProcessor and - in theory - used to crop the image and save it.

What's actually happening, when I run it locally through Visual Studio, is that the AJAX call returns a 500 exception and I see the following error message in the Diagnostics panel, under the "Events" tab:

Could not load type 'ImageProcessor.Imaging.CropLayer' from assembly 'ImageProcessor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

ImageProcessor was installed correctly, via NuGet Package Manager, in both projects of my solution - the actual web application, and a class library named Middle that's referenced in the web app project. The handler is contained within Middle. The handler does not throw an exception if I remove all the code that references ImageProcessor.

I have no idea what could be causing this. I've tried Googling the issue and none of the answers I could find helped me. This question suggests typing the complete namespaces, but I tried that and it didn't fix the issue. The comments on this one suggest clearing out my bin and obj folders, cleaning and rebuilding the solution, but that didn't work either.

Here's what my C# handler looks like so far (I can't finish it until I know whether what I have so far works, but I can't test it properly due to the issue I'm having).

using ImageProcessor;
using ImageProcessor.Imaging;
using System;
using System.IO;
using System.Web;

namespace Middle
{
    public class CropImage : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            var x = float.Parse(context.Request["x"]);
            var y = float.Parse(context.Request["y"]);
            var width = float.Parse(context.Request["width"]);
            var height = float.Parse(context.Request["height"]);
            var cropLayer = new CropLayer(x, y, width, height);

            var filePath = AppDomain.CurrentDomain.BaseDirectory + context.Request["fileName"];
            var imageData = File.ReadAllBytes(filePath);

            using (MemoryStream inStream = new MemoryStream(imageData))
            {
                using (MemoryStream outStream = new MemoryStream())
                {
                    // Initialize the ImageFactory using the overload to preserve EXIF metadata.
                    using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true))
                    {
                        // Load, resize, set the format and quality and save an image.
                        imageFactory.Load(inStream)
                                    .Crop(cropLayer)
                                    .Save(outStream);
                    }
                }
            }
        }
    }
}

If there's any other information you need, please do let me know.

How can I resolve this issue?

0

There are 0 best solutions below