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();
请注意,您可以自由选择DrawImage
位置的任何地方,包括在原点左侧和顶部的负区域。
另外请注意,对于“真正的”裁剪,您的一些(至少4个)点应该命中目标Bitmap
的边界!或者您可以使用GraphicsPath
来获取其边界框:
RectangleF rect = gp.GetBounds();
Bitmap bmp1 = new Bitmap((int)Math.Round(rect.Width, 0),
(int)Math.Round(rect.Height,0));
..