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

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

在此处输入图像描述

你可以将Points List变成多边形,然后变成一个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 } 

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

另请注意,对于“真实”裁剪,一些(至少4个)点应该击中目标Bitmap的边界! – 或者您可以使用GraphicsPath获取其边界框:

 RectangleF rect = gp.GetBounds(); Bitmap bmp1 = new Bitmap((int)Math.Round(rect.Width, 0), (int)Math.Round(rect.Height,0)); ..