c# - 这个图像裁剪器有什么问题?

标签 c# image-processing

我正在制作一个图像裁剪工具,但我终究无法弄清楚为什么它会生成它正在生成的图像...

我正在关注已接受的答案 here ,但它仍然很奇怪......这是我的代码:

public void Crop(
    string FileName,
    Crop Crop) {
    using (Bitmap Source = ((Bitmap)Image.FromFile(FileName))) {
        using (Bitmap Target = new Bitmap(Crop.Width, Crop.Height)) {
            using (Graphics Graphics = Graphics.FromImage(Target)) {
                Graphics.DrawImage(Source, new Rectangle(new Point(Crop.Left, Crop.Top), new Size(Crop.Width, Crop.Height)), new Rectangle(new Point(0, 0), new Size(Target.Width, Target.Height)), GraphicsUnit.Pixel);
            };

            Target.Save((FileName + ".temp"), JpegCodecInfo, HighQualityEncoder);
        };
    };


    this.NormalizeFileName(FileName);
}

请帮帮我。我正在附上我得到的图像... alt text

更新

对于@Aaronontheweb,这是 Crop 类及其填充方式:

public class Crop {
    [Required]
    public short Height { get; set; }

    [Required]
    public short Left { get; set; }

    [Required]
    public short Top { get; set; }

    [Required]
    public short Width { get; set; }
}

以及填充它的 jQuery:

$("#Image input:submit").bind("click", function () {
    $("#Crop\\.Height").val(Crop.height());
    $("#Crop\\.Left").val(Crop.position().left);
    $("#Crop\\.Top").val(Crop.position().top);
    $("#Crop\\.Width").val(Crop.width());
});

更新 2

没关系,我明白了。我问完问题后小睡了一下,只是为了清醒一下,当我再次看它时,我决定交换两个矩形,看看会发生什么。好吧,你猜怎么着,这解决了问题。

在这一点上,我不得不说 API 文档中给出的名称具有欺骗性。例如,文档将输出图像称为,将输入图像称为显示。也许应该更新 API 以获得更好的命名?

最佳答案

在下一行中:

Graphics.DrawImage(Source, new Rectangle(new Point(Crop.Left, Crop.Top), new Size(Crop.Width, Crop.Height)), new Rectangle(new Point(0, 0), new Size (Target.Width, Target.Height)), GraphicsUnit.Pixel);

确保 Crop.Left 和 Crop.Top 设置为 0,否则它会开始将像素设置为您得到的奇怪偏移。您也可以将其替换为以下行进行测试:

Graphics.DrawImage(Source, new Rectangle(new Point(0,0), new Size(Crop.Width, Crop.Height)), new Rectangle(new Point(0, 0), new Size(Target.Width) , Target.Height)), GraphicsUnit.Pixel);

关于c# - 这个图像裁剪器有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4436075/

相关文章:

python-3.x - 检测黑白文档上的图章形状

image - 彩色图像的中值滤波器

c# - 我怎样才能使这个 lambda 起作用?

c# - 如何使用EMGU或OpenCv获取图片区域中的直方图?

c# - 四指阵法统一

perl - 在数码照片中,如何检测一座山是否被云层遮挡?

c# - 将 KeyDown 键转换为一个字符串 C#

c# - Attributes.Remove 不起作用

algorithm - 使用大半径/标准差对图像进行高斯模糊时的奇怪行为

image-processing - 使用 Swift 4 在 Metal Compute Kernel 中传递参数