如何从WinForm pictureBox中的图像裁剪多边形区域?

时间:2015-06-20 13:25:48

标签: c# image winforms crop

如何使用多边形裁剪图像的一部分?例如,我有6个坐标,我想剪切这部分图像。

enter image description here

1 个答案:

答案 0 :(得分:10)

您可以将List Points变成多边形,然后变成GraphicsPath然后变成Region,然后Graphics.Clip(Region)变成Graphics.DrawImage using System.Drawing.Drawing2D; GraphicsPath gp = new GraphicsPath(); // a Graphicspath gp.AddPolygon(points.ToArray()); // with one Polygon Bitmap bmp1 = new Bitmap(555,555); // ..some new Bitmap // and some old one..: using (Bitmap bmp0 = (Bitmap)Bitmap.FromFile("D:\\test_xxx.png")) using (Graphics G = Graphics.FromImage(bmp1)) { G.Clip = new Region(gp); // restrict drawing region G.DrawImage(bmp0, 0, 0); // draw clipped pictureBox1.Image = bmp1; // show maybe in a PictureBox } gp.Dispose(); 1}}并完成..:

DrawImage

请注意,您可以随意选择Bitmap位置,包括位于原点左侧和顶部的负区域。

另请注意,对于“真实”裁剪,一些(至少4个)分数应该会达到目标GraphicsPath的边界! - 或者您可以使用RectangleF rect = gp.GetBounds(); Bitmap bmp1 = new Bitmap((int)Math.Round(rect.Width, 0), (int)Math.Round(rect.Height,0)); .. 获取其边界框:

{{1}}