2

I'm using Croppic to crop, rotate and save an image. But I'm not sure how to save the image if it's been rotated. When I save the image, after I rotate 90 degs, it still gets saved in it's original orientation. I suspect it's how I'm saving the image, but not sure.

Here are my methods for saving the cropped/rotated image. I'm guessing that it's how I'm saving the image because I can see on my file folder that the cropped image isn't rotated correctly, but the cropping has been applied.

[HttpPost]
public string CroppedImage(string imgUrl, int imgInitW, int imgInitH, double imgW, double imgH, int imgY1, int imgX1, int cropH, int cropW)
{
    var originalFilePath = Server.MapPath(imgUrl);
    var fileName = CropImage(originalFilePath, imgInitW, imgInitH, (int)imgW, (int)imgH, imgY1, imgX1, cropH, cropW);
    var result = new
    {
        status = "success",
        url = "../Cropped/" + fileName
    };

    return JsonConvert.SerializeObject(result);
}

private string CropImage(string originalFilePath, int origW, int origH, int targetW, int targetH, int cropStartY, int cropStartX, int cropW, int cropH)
{
    var originalImage = Image.FromFile(originalFilePath);

    var resizedOriginalImage = new Bitmap(originalImage, targetW, targetH);
    var targetImage = new Bitmap(cropW, cropH);

    using (var g = Graphics.FromImage(targetImage))
    {
        g.DrawImage(resizedOriginalImage, new Rectangle(0, 0, cropW, cropH), new Rectangle(cropStartX, cropStartY, cropW, cropH), GraphicsUnit.Pixel);
    }
    string fileName = Path.GetFileName(originalFilePath);
    var folder = Server.MapPath("~/Cropped");
    string croppedPath = Path.Combine(folder, fileName);
    targetImage.Save(croppedPath);

    return fileName;

}

The controller inputs are defined here and I see that imgX1 and imgY1 have different values if I rotate. I just don't know how to save it correctly in C#.

enter image description here

ataravati
  • 8,891
  • 9
  • 57
  • 89
chuckd
  • 13,460
  • 29
  • 152
  • 331
  • You said is saved in it's original orientation. What about the crop? Is the crop applied? – Ionut Necula Nov 18 '15 at 10:54
  • yes the crop is applied. But the rotation is not. I'm trying to determine if its the way I'm saving it or if it's something with Croppic? – chuckd Nov 18 '15 at 10:58
  • I see a g.RotateTransform(90); but how do I know if the image has been rotated? The only difference in data coming into the controller is the imgX1/imgY1 data and if I rotate 90 degs I get a value of 140/101, if I don't rotate I get values of 15/70. – chuckd Nov 18 '15 at 11:03

1 Answers1

1

There's a rotation parameter posted to the server when you crop the image that's missing from Croppic docs. That will tell you how many degrees the image has been rotated.

[HttpPost]
public string CroppedImage(string imgUrl, int imgInitW, int imgInitH, 
   double imgW, double imgH, int imgY1, int imgX1, 
   int cropH, int cropW, int rotation)
{

}

And, you can use the code in this answer to rotate the image.

Community
  • 1
  • 1
ataravati
  • 8,891
  • 9
  • 57
  • 89
  • great that's awesome, but trying g.RotateTransform(rotation); still doesn't rotate the image when it's saved. – chuckd Nov 18 '15 at 19:45
  • I don't know what `g.RotateTransform()` you're referring to. Did you use the code in the link in my answer? – ataravati Nov 18 '15 at 20:00