保存已旋转的图像

时间:2015-11-18 10:50:48

标签: c# asp.net-mvc image-processing image-rotation croppic

我使用Croppic裁剪,旋转和保存图像。但我不确定如果图像被旋转,如何保存图像。当我保存图像时,在旋转90度后,它仍然保存在原始方向中。我怀疑它是如何保存图像的,但不确定。

以下是保存裁剪/旋转图像的方法。我猜测我是如何保存图片的,因为我可以在文件夹中看到裁剪后的图像没有正确旋转,但是已经应用了裁剪。

[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;

}

这里定义了控制器输入,如果我旋转,我看到imgX1和imgY1有不同的值。我只是不知道如何在C#中正确保存它。

enter image description here

2 个答案:

答案 0 :(得分:1)

裁剪Croppic文档中缺少的图像时,会向服务器发布一个旋转参数。这将告诉您图像旋转了多少度。

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

}

并且,您可以使用此answer中的代码来旋转图像。

答案 1 :(得分:0)

  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);
            }
        }
相关问题