如何从WinForm pictureBox中的图像裁剪多边形区域? [英] How to crop a polygonal area from an image in a WinForm pictureBox?

查看:712
本文介绍了如何从WinForm pictureBox中的图像裁剪多边形区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I crop a part of an image with a polygon? For example I have 6 coordinates and I would like to cut this part of an image.

推荐答案

您可以将设为点数进入Polygon,然后进入 GraphicsPath 然后进入 Region 并在之后Graphics.Clip(Region)你可以 Graphics.DrawImage 并完成..:

You can make the List of Points into a Polygon, then into a GraphicsPath and then into a Region and after Graphics.Clip(Region) you can Graphics.DrawImage and are done..:

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 位置,包括位于原点左侧和顶部的负区域..

Note that you are free to choose the DrawImage location anywhere, including in the negative area to the left and top of the origin..

另请注意'真实'裁剪一些(至少4个)你的积分应该达到目标位图的边界! - 或者您可以使用 GraphicsPath 来获取其边界框:

Also note that for 'real' cropping some (at least 4) of your points should hit the target Bitmap's borders! - Or you can use the GraphicsPath to get its bounding box:

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

这篇关于如何从WinForm pictureBox中的图像裁剪多边形区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆