在图像上定义多边形 [英] Define polygon over image

查看:56
本文介绍了在图像上定义多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我正在尝试在Image上动态定义多边形..即用户将在单击鼠标时定义多边形点,然后定义的区域的颜色将改变..

请请帮我...

在此先感谢

Hello,


I am trying to define polygon over Image Dynamically....i.e. User will define polygon points on mouse click and after that defined area''s color will change..

please please help me ...

Thanks in advance

推荐答案

创建一个类级别的GraphicsPath对象:
Create a class level GraphicsPath object:
GraphicsPath path = new GraphicsPath();


在鼠标Down事件中,在路径中添加以下行:


In your mouse Down event, add lines to the path:

path.AddLine(oldPoint, newPoint);

其中0old点是上一次鼠标单击的位置.
处理任何保存图像的对象的Paint事件,并从GraphicsPath构造一个Region.然后,您可以根据需要在该区域内绘制:

Where 0old point is the previous mouse click location.
Handle the Paint event for whatever object holds your image, and construct a Region from the GraphicsPath. You can then draw as needed within the region:

Region r = new Region(path);
e.Graphics.FillRegion(Brushes.Red, r);



绘图是C#的基本编程;

首先创建一个Windows窗体示例,然后在其上放置图片框.

并将代码替换为应用程序

*这是针对总表格类的.

*并添加必需的事件(pictureBox1_MouseClick).


****************************************************** **************
公共局部类Form1:Form
{
int iNumberofClicks = 0;
Point [] pointArray = new Point [100];
公共Form1()
{
InitializeComponent();
}
私有空pictureBox1_MouseClick(对象发送者,MouseEventArgs e)
{
pointArray [iNumberofClicks] .X = e.X;
pointArray [iNumberofClicks] .Y = e.Y;
笔PenSpikes =新笔(颜色:绿色);
SolidBrush solidBrush =新的SolidBrush(Color.Blue);
iNumberofClicks ++;
如果(iNumberofClicks> 1)
{
Point [] CurrentpointArray = new Point [iNumberofClicks];

for(int i = 0; i< iNumberofClicks; i ++)
{
CurrentpointArray [i] .X = pointArray [i] .X;
CurrentpointArray [i] .Y = pointArray [i] .Y;
}

位图画布=新位图(pictureBox1.Width,pictureBox1.Height);
图形关闭ScreenDC = Graphics.FromImage(canvas);
//offScreenDC.DrawPolygon(PenSpikes,CurrentpointArray);
offScreenDC.FillPolygon(solidBrush,CurrentpointArray);
pictureBox1.Image = canvas;
offScreenDC.Dispose();
}
}
}

****************************************************** ********
问候

Vinay
Hi,
drawing is the basic programming for C#;

First create a sample of windows form, and drop the picture box on that.

and replace the code to application

* this is for total form class.

* and add the required events (pictureBox1_MouseClick).


****************************************************************
public partial class Form1 : Form
{
int iNumberofClicks = 0;
Point[] pointArray = new Point[100];
public Form1()
{
InitializeComponent();
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
pointArray[iNumberofClicks].X = e.X;
pointArray[iNumberofClicks].Y = e.Y;
Pen PenSpikes = new Pen(Color.Green);
SolidBrush solidBrush = new SolidBrush(Color.Blue);
iNumberofClicks++;
if (iNumberofClicks > 1)
{
Point[] CurrentpointArray = new Point[iNumberofClicks];

for (int i = 0; i < iNumberofClicks; i++)
{
CurrentpointArray[i].X = pointArray[i].X;
CurrentpointArray[i].Y = pointArray[i].Y;
}

Bitmap canvas = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics offScreenDC = Graphics.FromImage(canvas);
// offScreenDC.DrawPolygon(PenSpikes, CurrentpointArray);
offScreenDC.FillPolygon(solidBrush, CurrentpointArray);
pictureBox1.Image = canvas;
offScreenDC.Dispose();
}
}
}

**********************************************************
Regards

Vinay


这篇关于在图像上定义多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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