1

C#我有一个使用ASP.NET MVC 5 框架顶部编写的应用程序。

我正在尝试使用WebImage裁剪磁盘上的图像。在 GUI 上,我使用Jcrop插件来允许用户标记他们想要裁剪的区域,也就是保留。如您所见,Jcrop 插件为我提供了 X1、X2、Y1 和 Y2,它们标识了要保留的最终图像的位置。Jcrop 还为我提供了最终图像的最终高度和宽度,尽管它们也可以使用以下公式计算W = X2 - X1H = Y2 - Y1

在此处输入图像描述

这是我裁剪给定图像并覆盖它的方法。

/// <summary>
/// Crop the given image and overrides it with the cropped image
/// </summary>
/// <param name="filename">The full path of the image and the location of the new image</param>
/// <param name="top">Y or Y1</param>
/// <param name="left">X or X1</param>
/// <param name="bottom">Y2</param>
/// <param name="right">X2</param>
/// <returns></returns>
public WebImage CropAndSave(string sourcePath, int top, int left, int bottom, int right)
{
    byte[] imageBytes = File.ReadAllBytes(sourcePath);

    var image = new WebImage(imageBytes)
    {
        FileName = ExtractFileName(sourcePath)
    };

    WebImage croppedImage = image.Crop(top, left, bottom, right);
    croppedImage.Save(sourcePath, "jpg", true);

    return croppedImage;
}

但是,裁剪后的图像不是我所期望的。这是一个非常小的图像,与用户想要保留的图像不同。

如何使用WebImage.Crop(...)正确裁剪图像?

4

1 回答 1

2

阅读您提供的定义Crop期待以下内容;

  • 从顶部Y1移除的像素数
  • 从左侧移除的像素数X1
  • 原件全高减去 Y2
  • 原稿全宽减去 X2

    image.Crop(Y1, X1, image.height - Y2, image.width - X2);
    
于 2018-11-19T20:04:11.497 回答