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#.