【问题标题】:How to crop an image using C# with the WebImage class?如何使用带有 WebImage 类的 C# 裁剪图像?
【发布时间】:2018-11-19 18:47:24
【问题描述】:

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

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

【问题讨论】:

  • 我打赌 image.Crop 想要宽度和高度,而不是底部和右侧位置。免责声明,我不使用 webimage,但这是相当行业标准...
  • 我应该在哪里提供宽度和高度?这是裁剪图像docs.microsoft.com/en-us/dotnet/api/… 的签名定义
  • 看起来下面的两个例子显示了我在说什么:-)

标签: c# jcrop webimage


【解决方案1】:

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

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

    image.Crop(Y1, X1, image.height - Y2, image.width - X2);
    

【讨论】:

    猜你喜欢
    • 2010-10-18
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多