2

I have the following code to crop the image :

   $.ajax({
        type: "POST",
        url: root("CropController/CropImage"),
        data: {
            imagePath: imagesrc,
            cropPointX: parseInt(PointX),
            cropPointY: parseInt(PointY),
            imageCropWidth: parseInt(CropWidth),
            imageCropHeight: parseInt(CropHeight)
        }
    }).done(function (dt) {
       alert(dt.photo);
    });

And the controller code is :

public JsonResult CropImage(string imagePath, int? cropPointX, int? cropPointY, int? imageCropWidth, int? imageCropHeight)
    {
        string output = imagePath.Substring(imagePath.IndexOf(',') + 1);
        byte[] imageBytes = Convert.FromBase64String(output);

            //croppedImage will have the cropped part of the image.
        byte[] croppedImage = ImageCroping.CropImage(imageBytes, cropPointX.Value, cropPointY.Value, imageCropWidth.Value, imageCropHeight.Value);

        string photo = "data:image/jpeg;base64," + Convert.ToBase64String(croppedImage);
        return Json(new { photoPath = photo }, JsonRequestBehavior.AllowGet);

    }

If the cropping area is small then the alert in done function will be called but it throws the error when the cropping area is large and the done function is not fired. Can anyone please help me with this. Thanks in advance

Shareef
  • 35
  • 2
  • 8

1 Answers1

2

Try using ContentResult instead of JSON. It seems json has problem with its length or some wrong character in it Or may be with its GET allowed on return :

public ActionResult CropImage(string imagePath, int? cropPointX, int? cropPointY, int? imageCropWidth, int? imageCropHeight)
    {
        string output = imagePath.Substring(imagePath.IndexOf(',') + 1);
        byte[] imageBytes = Convert.FromBase64String(output);

            //croppedImage will have the cropped part of the image.
        byte[] croppedImage = ImageCroping.CropImage(imageBytes, cropPointX.Value, cropPointY.Value, imageCropWidth.Value, imageCropHeight.Value);

        string photo = "data:image/jpeg;base64," + Convert.ToBase64String(croppedImage);
        return Content(photo);

    }
Siamak Ferdos
  • 3,181
  • 5
  • 29
  • 56
  • The error is ContentResult is a type but used like a variable. I was not sure of the error but solved it by returning as follows : public stringCropImage(string imagePath, int? cropPointX, int? cropPointY, int? imageCropWidth, int? imageCropHeight) { .................. return photo; } – Shareef Feb 01 '17 at 11:11
  • ContentResult is not correct. Sorry for it, but it should be only Content. Try it now. – Siamak Ferdos Feb 01 '17 at 11:23