我正在制作一个图像裁剪工具,但我终究无法弄清楚为什么它会生成它正在生成的图像...
我正在关注已接受的答案 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);
}
请帮帮我。我正在附上我得到的图像...
更新
对于@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/