图像裁剪c语言,使用c从图像裁剪十字矩形#

需要做的是向矩阵对齐添加偏移量。在这种情况下,我从每边(总共9个矩形)取出一个额外长度的矩形,并每次偏移矩阵。

请注意,有必要将抵消0(原始作物)放在最后,否则您将得到错误的结果。

另请注意,如果您指定的矩形大于旋转后的图片,您仍然会看到空白区域。

public static Bitmap CropRotatedRect(Bitmap source, Rectangle rect, float angle, bool HighQuality)

{

int[] offsets = { -1, 1, 0 }; //place 0 last!

Bitmap result = new Bitmap(rect.Width, rect.Height);

using (Graphics g = Graphics.FromImage(result))

{

g.InterpolationMode = HighQuality ? InterpolationMode.HighQualityBicubic : InterpolationMode.Default;

foreach (int x in offsets)

{

foreach (int y in offsets)

{

using (Matrix mat = new Matrix())

{

//create the appropriate filler offset according to x,y

//resulting in offsets (-1,-1), (-1, 0), (-1,1) ... (0,0)

mat.Translate(-rect.Location.X - rect.Width * x, -rect.Location.Y - rect.Height * y);

mat.RotateAt(angle, rect.Location);

g.Transform = mat;

g.DrawImage(source, new Point(0, 0));

}

}

}

}

return result;

}

要重新创建例如:

Bitmap source = new Bitmap("C:\\mjexample.jpg");

Bitmap dest = CropRotatedRect(source, new Rectangle(86, 182, 87, 228), -45, true);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值