简体   繁体   中英

Saving an image that's been rotated

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

在此处输入图片说明

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.

  private void listImage_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                pictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone); 
                pictureBox1.Refresh();
                btnGuardarFoto.Visible = true;                
            }
            catch (Exception ex)
            {
                oCOM.MsgError("Error en listImage_MouseDoubleClick() " + ex.Message);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM